davidsjoberg / ggsankey

Make sankey, alluvial and sankey bump plots in ggplot
Other
262 stars 30 forks source link

Ordering of the nodes #7

Open moleps opened 3 years ago

moleps commented 3 years ago

I am unable to change the ordering of the nodes. Would be great if it is possible when I do ordinal variables and pre/post visualizations.

dmongin commented 3 years ago

I am facing the same problem, and I found a partial solution: You can order the node and next_node variables of your long format table by setting them as factor and order the associated levels. Example:

df <- data.frame(expand.grid(LETTERS[1:3],LETTERS[1:3]))
set.seed(125)
df$N <- sample(1:10,size = nrow(df),replace = T)

dflong <- df %>%
  # arrange(Var1)%>%
  make_long(Var1, Var2,value = N)

dflong %>%
  ggplot( aes(x = x, 
              next_x = next_x, 
              node = node, 
              next_node = next_node,
              fill = factor(node),
              value = value)) +
  geom_sankey()

image


dflong$node <- factor(dflong$node,levels = c("C","B","A"))
dflong$next_node <- factor(dflong$next_node,levels = c("C","B","A"))

dflong %>%
  ggplot( aes(x = x, 
              next_x = next_x, 
              node = node, 
              next_node = next_node,
              fill = factor(node),
              value = value)) +
  geom_sankey()

image

You can then tweak the colors if you want

MPietzke commented 2 years ago

Based on this: Would it be possible to minimize overlap by arranging the nodes based on their shared proportions? So that the biggest flows are just beside each other?

See this example: https://github.com/davidsjoberg/ggsankey/issues/15

Here "Stocks" share the biggest amount with "Giro" and should be better on the bottom, "Rent" shares the biggest amount with "Shared" and should be on top. Here the expected, following the factoring-approach:

image

mweberr commented 2 years ago

I also have the same issue, that the sorted order of the node labels needs to be changed to minimise crossing of flows. Here is the same issue for dhe D3 package : https://stackoverflow.com/questions/37302958/d3-sankey-minimize-link-crossing