ggforce icon indicating copy to clipboard operation
ggforce copied to clipboard

Order y-axis categories on ggforce::geom_parallel_sets

Open ssp3nc3r opened this issue 5 years ago • 5 comments

Is it possible to change the default ordering (alphabetic) of the parallel set categories?

Someone else posted the same question on stackoverflow:

https://stackoverflow.com/questions/47761815/order-y-axis-on-ggforcegeom-parallel-sets

ssp3nc3r avatar Mar 15 '19 04:03 ssp3nc3r

Google lead me to this issue, maybe relevant, I can't force my "NA"s appear always on top or always at the bottom.

NAs are coded as string "NA", and all columns are factor with "NA"s coming last. But still NAs show up at the top or at the bottom, see image below. Maybe it is to do with the order they are found in the data?

image

I could provide repro if needed, hoping there is an easy fix/suggestion to achieve this.

zx8754 avatar Mar 16 '19 20:03 zx8754

Sorting would be great. In its current state this implementation unfeasible for publication. Switching from alphabetically to factor order would be enough for most applications.

cleberecht avatar Jan 14 '20 07:01 cleberecht

I came across the Stackoverflow question and this issue having the same problem. In my case I have a specific use-case where i only have two items on the x axis having the same labels/categories for the y axis. In this use-case I have been able to sort the y categories using ggplot(data, aes(x, id = id, split = factor(y, levels = c('a', 'b')), value = value)) + ... (taken from the question on stack).

Although I haven't spent the time to fully grasp the code, I have a feeling that line 64 in R/parallel_sets.R could be involved in that it appears to change the order of the y categories to something different than expected. Could it by that setting split to a factor is not "overruled" anymore by the as.factor() in this line??

paullemmens avatar Feb 03 '20 19:02 paullemmens

It's an ugly and rough fix, but I found that putting a space in front of the string I want to be on top works wells...

marilotte avatar May 17 '21 13:05 marilotte

It'd be nice to have a few options to sort the layout:

  • Alphabetical (as-is)
  • By flatness (2nd chart) I'm not sure how to prioritize this but visually I can eyeball it so it must be programmable.
  • By size (3rd chart). This is close to what I'd like if it could prioritize size of the set axis (grey) then for flatness. If that could happen, D-G would sit below E-G, not sure if anything would change from groups leaving A.
  • There could be a 4th option in which the axes are sorted (like chart 3 currently), then the parallel sets are also sorted: C-F and D-G would move to the bottom of their respective groups (F and G) since they are the biggest.

image

library(tidyverse)
library(ggforce)

df <-
  tibble::tribble(
    ~round_1, ~round_2, ~round_3, ~n,
    "A",      "D",      "G",       2,
    "A",      "E",      "G",       3,
    "B",      "D",      "F",       2,
    "B",      "D",      "G",       2,
    "B",      "C",      "F",       3
  ) |> 
  mutate(fill = round_2)


make_plot <- function(title, ...) {
  df %>%
    ggforce::gather_set_data(1:3) |>
    mutate(y = fct_relevel(y, ...)) |> 
    ggplot(aes(x, id = id, split = y, value = n)) +
    geom_parallel_sets(aes(fill = fill), alpha = 0.3, sep = 0.01) +
    geom_parallel_sets_axes(fill = "grey80", axis.width = 0.15, sep = 0.01) +
    geom_parallel_sets_labels(angle = 0, sep = 0.01) +
    theme_void() +
    labs(title = title) +
    theme(
      legend.position = "none",
      plot.title = element_text(hjust = 0.5)
    )
}

gridExtra::grid.arrange(
  make_plot("Default order\n(alphabetical)"),
  make_plot("Prioritize\nFlatness", c("B", "A", "C", "D", "E", "F", "G")),
  make_plot("Prioritize height\n(biggest on bottom)", c("A", "B", "C", "E", "D", "F", "G")),
  nrow = 1
)

rjake avatar Nov 11 '21 02:11 rjake