davidsjoberg / ggsankey

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

size of the flow #6

Closed dmongin closed 3 years ago

dmongin commented 3 years ago

Is there a way in geom_sankey to specify an aesthetic that provides directly the size of the flow, i.e. the number of connections between the nodes?

For example:

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

I would like something like

df %>%
make_long(Var1, Var2)%>%
  ggplot( aes(x = x, 
                 next_x = next_x, 
                 node = node, 
                 next_node = next_node,
                 fill = factor(node))) +
  geom_sankey()

image

But with the flows given by N. A hack would be to repeat each row by N before the make_long, but I am sure there is a proper way.

davidsjoberg commented 3 years ago

Thanks for your issue! I have added the argument value to make long so that it also creates a weight column to be passed to ggsankey where the aesthetic values can be used. Reinstall ggsankey and try this reprex:

library(tidyverse)
library(ggsankey)

df <- data.frame(expand.grid(LETTERS[1:3],LETTERS[1:3]))
df$N <- sample(1:10,size = nrow(df),replace = T)
df <- df %>% 
  make_long(Var1, Var2, value = N)

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

Created on 2021-05-25 by the reprex package (v2.0.0)

dmongin commented 3 years ago

Whaou, perfect. Thank you for your reactivity and your work!

matteloca commented 1 year ago

Great! How can I plot the values? Like under Node.

the below method works, what do you think?

df %>% count(x,node,next_x,next_node) %>% group_by(x,node) %>% mutate(cnt2 = sum(n)) %>% ggplot( aes(x = x, next_x = next_x, node = node, next_node = next_node, fill = factor(node), value = value, label = paste0(node,'\n',cnt2)) + #doesn't work geom_sankey()