Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Left and right shift of node labels to put them beside the nodes #40

Open
BAndrews22 opened this issue Apr 21, 2023 · 2 comments
Open

Comments

@BAndrews22
Copy link

I am trying to have the labels left and right besides the start and end nodes, respectively.
I am using the following to adjust the alignment of the labels but how can I shift them horizontally so that they move out of the nodes, i.e. the start ones to the left, the end ones to the right? Is there any aesthetic useful for that?

geom_sankey_text(aes(label=after_stat(node), hjust=ifelse(after_stat(x)==1, 1, 0)))

Otherwise, is there any stat returning the y midpoint of the nodes allowing then to use geom_text directly?

@BoardshortsBen
Copy link

I achieved this by adding a data argument to geom_text/geom_label and filtering that for one value of x and then adding a second set of labels for another value of x. I also added a paste0 function to add the numbers for each node directly.

Here was my example which worked:

geom_sankey_label(data=df2 %>% filter(x == "Delivery.category"), aes(label = paste0(node, " = ", n)), size = 3, fill = "white", hjust = 0) + geom_sankey_label(data=df2 %>% filter(x == "Collection.method"), aes(label = paste0(node, " = ", n)), size = 3, fill = "white", hjust = 1)

@hrdawson
Copy link

hrdawson commented May 7, 2024

Thanks for the fix, @BoardshortsBen. I've standardised the code to work with the README example based on mtcars (without the numbers for each node because I couldn't get that portion to work). It would be awesome to have a native version of this as well.

ggplot(df, aes(x = x, 
               next_x = next_x, 
               node = node, 
               next_node = next_node,
               fill = factor(node))) +
  geom_sankey() +
  geom_sankey_label(data=df %>% filter(x == "cyl"), aes(label = paste0(node)), size = 3, fill = "white", hjust = 0) +  
  geom_sankey_label(data=df %>% filter(x == "carb"), aes(label = paste0(node)), size = 3, fill = "white", hjust = 1) +
  scale_fill_discrete(drop=FALSE)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants