ggsankey icon indicating copy to clipboard operation
ggsankey copied to clipboard

Ordering of the nodes

Open moleps opened this issue 3 years ago • 3 comments

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.

moleps avatar Jun 20 '21 21:06 moleps

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

dmongin avatar Aug 30 '21 12:08 dmongin

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

MPietzke avatar Oct 22 '21 10:10 MPietzke

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

mweberr avatar Feb 15 '22 08:02 mweberr