pepijn-devries / ggsankeyfier

Add alluvial / sankey diagram layers to a ggplot
https://pepijn-devries.github.io/ggsankeyfier/
GNU General Public License v3.0
11 stars 1 forks source link

Feature request: Make usable with facets #14

Open BAndrews22 opened 2 months ago

BAndrews22 commented 2 months ago

Currently, when trying to plot multiple Sankeys at once with facet_grid(), the connectors are much too thin and do not take up the entire space of the node.

pepijn-devries commented 2 months ago

Hi @BAndrews22,

I'm not sure if this is a bug report, a feature request or even an issue. It sounds as if you are trying something that is either not feasible or possible. In order to help you or to prepare a fix (if needed) I need a more detailed report with a reprex. I'm looking forward to your report.

Cheers,

Pepijn

BAndrews22 commented 2 months ago

Hi Pepijn, Thanks a mil for solving my other issue with printing to PDF. I can confirm that this issue is related, the area of the facets gets too narrow. By resizing the plot area, also the facetted plot works. But one really has to fiddle with the width and height to get it work.

I have done the same plot with the ggsankey package and it seems much less sensitive to the aspect ratio. However, the output of your package looks better and its syntax is cleaner, so I will rather adjust the aspect ratio.

Just for completeness, below a repex. Depending on the size of the plot window, the output looks correct or not. Many thanks!

library(ggplot2)
library(ggsankeyfier)
theme_set(theme_light())
data("ecosystem_services")

es_sub <-
  ecosystem_services |>
  subset(RCSES > quantile(RCSES, 0.99)) |>
  pivot_stages_longer(c("activity_realm", "biotic_realm", "service_section"), "RCSES", "service_section")

es2 <- cbind(es_sub, pl=rep(1:10, each=nrow(es_sub)))
pos <- position_sankey(v_space = "auto", order = "ascending")

ggplot(es2, aes(x = stage, y = RCSES, group = node, connector = connector, edge_id = edge_id)) + 
  geom_sankeyedge(aes(fill = service_section), position = pos) +   
  geom_sankeynode(position = pos) + 
  facet_wrap(~pl, nrow=2)
pepijn-devries commented 2 months ago

This is a trade-off of how the plotting routine for the edges is implemented. I'm not sure if this can be fixed. If I do succeed, I will report back here...

BAndrews22 commented 2 months ago

That's great. Thanks a mil!