corybrunson / ggalluvial

ggplot2 extension for alluvial plots
http://corybrunson.github.io/ggalluvial/
GNU General Public License v3.0
497 stars 34 forks source link

strata label positions #27

Closed Tomasz-Olczyk closed 5 years ago

Tomasz-Olczyk commented 5 years ago

Description of the issue

After subsetting text labels of the strata in order to lessen the number of labels they are changing positions.

Reproducible example (preferably using reprex::reprex())

ggplot(data = titanic_wide, aes(axis1 = Class, axis2 = Sex, axis3 = Age, y = Freq)) + scale_x_discrete(limits = c("Class", "Sex", "Age"), expand = c(.1, .05)) + xlab("Demographic") + geom_alluvium(aes(fill = Survived)) + geom_stratum() + geom_text(data = subset(titanic_wide, Freq > 200), stat = "stratum", label.strata = TRUE) + theme_minimal() + ggtitle("passengers on the maiden voyage of the Titanic", "stratified by demographics and survival")

image

corybrunson commented 5 years ago

The root problem here is that the stat_*() functions use aggregative functions, namely cumsum(), to convert frequency or count data into alluvium and stratum positions. This means that subsetting the data produces not a diagram with some elements removed and the others in their original positions, but an entirely different diagram. When this is only applied to one layer (in this case, the text labels), then that layer doesn't align with the other layers.

At present, i think the only way to do what you're after is to manipulate the data frame itself to create a new column of labels, some of which are blank...and this would only work for data in long format. But it seems like a general enough need to make it a feature. I'll try to get to this within the next couple of weeks.

Thanks for pointing this out!

Tomasz-Olczyk commented 5 years ago

Thank you for fast replay and this great package. I'll try this solution.

corybrunson commented 5 years ago

v0.9.2 introduces the stat_stratum() parameters min.height and max.height that allow the user to restrict which strata (or, if used with geom_text(), which labels) to render. See the vignette on labeling small strata for an example. Thanks for the prompt!