This is aggplot2extension for alluvial plots.
The alluvial plots implemented here can be used to visualizefrequency distributions over time or frequency tables involving severalcategorical variables. The design is inspired by thealluvialpackage, but theggplot2 framework induced severalconspicuous differences:
Additionally,ggalluvial arranges these layersvertically without gaps, so that the secondary plotting axis indicatesthe cumulative values of the strata at each dimension.
The latest stable release can be installed from CRAN:
install.packages("ggalluvial")Thecranbranch will contain the version most recently submitted toCRAN. It isduplicated in themaster branch, from which source thewebsite isbuilt.
The development version can be installed from the (default)main branch on GitHub:
remotes::install_github("corybrunson/ggalluvial@main",build_vignettes =TRUE)Note that, in order to build the vignettes, the imported packagesalluvial,ggfittext, andggrepel must be installed. To skip this step, leavebuild_vignettes unspecified or set it toFALSE.
Theoptimizationbranch contains a development version with experimental functions toreduce the number or area of alluvial overlaps (see issue#6).Install it as follows:
remotes::install_github("corybrunson/ggalluvial",ref ="optimization")Note, however, that this branch has not kept pace with themain branch or with recent upgrades on CRAN.
Here is how to generate an alluvial plot representation of themulti-dimensional categorical dataset of passengers on the Titanic:
titanic_wide<-data.frame(Titanic)head(titanic_wide)#> Class Sex Age Survived Freq#> 1 1st Male Child No 0#> 2 2nd Male Child No 0#> 3 3rd Male Child No 35#> 4 Crew Male Child No 0#> 5 1st Female Child No 0#> 6 2nd Female Child No 0ggplot(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))+geom_stratum()+geom_text(stat ="stratum",aes(label =after_stat(stratum)))+theme_minimal()+ggtitle("passengers on the maiden voyage of the Titanic","stratified by demographics and survival")
The data is in “wide” format, butggalluvial alsorecognizes data in “long” format and can convert between the two:
titanic_long<-to_lodes_form(data.frame(Titanic),key ="Demographic",axes =1:3)head(titanic_long)#> Survived Freq alluvium Demographic stratum#> 1 No 0 1 Class 1st#> 2 No 0 2 Class 2nd#> 3 No 35 3 Class 3rd#> 4 No 0 4 Class Crew#> 5 No 0 5 Class 1st#> 6 No 0 6 Class 2ndggplot(data = titanic_long,aes(x = Demographic,stratum = stratum,alluvium = alluvium,y = Freq,label = stratum))+geom_alluvium(aes(fill = Survived))+geom_stratum()+geom_text(stat ="stratum")+theme_minimal()+ggtitle("passengers on the maiden voyage of the Titanic","stratified by demographics and survival")
For detailed discussion of the data formats recognized byggalluvial and several examples that illustrate itsflexibility and limitations, read the technical vignette:
vignette(topic ="ggalluvial",package ="ggalluvial")Several additional vignettes offer detailed solutions to specificneeds:
"labels") for how to elegantlylabel strata of a wide range of heights in an alluvial plot;"order-rectangles") forhow to control the positioning of strata and lodes in an alluvial plot;and"shiny")for how to incorporate alluvial plots into interactive apps.The object documentation includes several more examples. Usehelp() to call forth more detail on
stat_* orgeom_*),to_*_form), andvaccinationsandmajors).For some more digestible guidance on usingggalluvial, check out three cheat sheets and demos bystudents in Joyce Robbins’sExploratory DataAnalysis and Visualization Community Contribution Project:
Development of this package benefitted from the use of equipment andthe support of colleagues atUConnHealth and atUF Health.
Contributions in any form are more than welcome! Pretty much everyfix and feature of this package derives from a problem or question posedby someone with datasets or design goals i hadn’t anticipated. See theCONTRIBUTINGfile for guidance, and please respect theCodeof Conduct.
If you useggalluvial-generated figures inpublication, i’d be grateful to hear about it! You can also cite thepackage according tocitation("ggalluvial").