corybrunson / ggalluvial

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

Hiding alluvia in ggalluvial #59

Closed doctorspruce closed 4 years ago

doctorspruce commented 4 years ago

I have been working on fine-tuning a figure showing landcover change between two time periods using ggalluvial. In reading about some of the options to customize alluvial plots in R, I have found tutorials indicating that with alluvial(), there is an argument ('hide') allowing the user to omit plotting some of the alluvia that are unimportant to de-clutter the figure.

From what I can tell, this is not a feature that is available in ggalluvial yet. It would be great if it could be incorporated into future updates, as I generally have found the implementation of ggalluvial to be excellent, and having this flexibility would be beneficial to the package.

Here is the link to the vignette about hiding alluvia with alluvial() https://cran.r-project.org/web/packages/alluvial/vignettes/alluvial.html

Thank you,

-Ana Sniderhan (DoctorSpruce)

corybrunson commented 4 years ago

Hi, and thank you for the praise! It is valued. And ggalluvial owes a great debt to alluvial for getting it flowing, so to speak.

There do exist some ways to omit or "hide" alluvia with ggalluvial, though i don't know if they'll meet your needs. One way is to use a custom color palette with the fill (or color) aesthetic that includes the completely transparent color "#00000000", and to assign that color to the data elements you want to omit. Alternatively, pass a logical variable to the alpha aesthetic and use a custom transparency scale. Here's an example, yanked from the README, using the latter approach:

library(ggalluvial)
#> Loading required package: ggplot2
titanic_wide <- data.frame(Titanic)
ggplot(data = titanic_wide,
       aes(axis1 = Class, axis2 = Sex, axis3 = Age,
           y = Freq)) +
  scale_x_discrete(limits = c("Class", "Sex", "Age"), expand = c(.2, .05)) +
  xlab("Demographic") +
  geom_alluvium(aes(fill = Survived, alpha = Class != "Crew")) +
  geom_stratum() + geom_text(stat = "stratum", infer.label = TRUE) +
  theme_minimal() +
  scale_alpha_manual(values = c(0, .5), guide = FALSE) +
  ggtitle("passengers on the maiden voyage of the Titanic",
          "stratified by demographics and survival")

Created on 2020-05-26 by the reprex package (v0.3.0)

These options are general to ggplot2; they should work with extensions as well. There are also a couple of stat_*() parameters that omit graphical elements outside certain limits: min.y and max.y. They're illustrated in the documentation for stat_flow(), for example.

If there's a use case you have in mind that these tricks aren't suitable for, please do share—there are at least a couple more releases planned before v1.0.0.

corybrunson commented 4 years ago

@doctorspruce i'm closing this issue on the assumption that current functionality meets your needs, but please do reopen it and clarify if it does not!