MyNixOS website logo
Description

Alluvial Plots in 'ggplot2'.

Alluvial plots use variable-width ribbons and stacked bar plots to represent multi-dimensional or repeated-measures data with categorical or ordinal variables; see Riehmann, Hanfler, and Froehlich (2005) <doi:10.1109/INFVIS.2005.1532152> and Rosvall and Bergstrom (2010) <doi:10.1371/journal.pone.0008694>. Alluvial plots are statistical graphics in the sense of Wilkinson (2006) <doi:10.1007/0-387-28695-0>; they share elements with Sankey diagrams and parallel sets plots but are uniquely determined from the data and a small set of parameters. This package extends Wickham's (2010) <doi:10.1198/jcgs.2009.07098> layered grammar of graphics to generate alluvial plots from tidy data.

ggalluvial

Travis CRAN downloads DOI JOSS

This is a ggplot2 extension for alluvial plots.

Design

The alluvial plots implemented here can be used to visualize frequency distributions over time or frequency tables involving several categorical variables. The design is inspired by the alluvial package, but the ggplot2 framework induced several conspicuous differences:

  • alluvial understands a variety of inputs (vectors, lists, data frames), whereas ggalluvial requires a single data frame;
  • alluvial uses each variable of these inputs as a dimension of the data, whereas ggalluvial requires the user to specify the dimensions, either as separate aesthetics or as key-value pairs;
  • alluvial produces both the alluvia, which link cohorts across multiple dimensions, and (what are here called) the strata, which partition the data along each dimension, in a single function; whereas ggalluvial relies on separate layers (stats and geoms) to produce strata, alluvia, and alluvial segments called lodes and flows.

Additionally, ggalluvial arranges these layers vertically without gaps, so that the secondary plotting axis indicates the cumulative values of the strata at each dimension.

Installation

The latest stable release can be installed from CRAN:

install.packages("ggalluvial")

The cran branch will contain the version most recently submitted to CRAN. It is duplicated in the master branch, from which source the website is built.

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 packages alluvial, ggfittext, and ggrepel must be installed. To skip this step, leave build_vignettes unspecified or set it to FALSE.

The optimization branch contains a development version with experimental functions to reduce 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 the main branch or with recent upgrades on CRAN.

Usage

Example

Here is how to generate an alluvial plot representation of the multi-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    0
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)) +
  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, but ggalluvial also recognizes 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     2nd
ggplot(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")

Documentation

For detailed discussion of the data formats recognized by ggalluvial and several examples that illustrate its flexibility and limitations, read the technical vignette:

vignette(topic = "ggalluvial", package = "ggalluvial")

Several additional vignettes offer detailed solutions to specific needs:

  • “Labeling small strata” ("labels") for how to elegantly label strata of a wide range of heights in an alluvial plot;
  • “The Order of the Rectangles” ("order-rectangles") for how to control the positioning of strata and lodes in an alluvial plot; and
  • “Tooltips for ggalluvial plots in Shiny apps” ("shiny") for how to incorporate alluvial plots into interactive apps.

The object documentation includes several more examples. Use help() to call forth more detail on

  • any layer (stat_* or geom_*),
  • the conversion functions (to_*_form), and
  • the data sets installed with the package (vaccinations and majors).

Short form

For some more digestible guidance on using ggalluvial, check out three cheat sheets and demos by students in Joyce Robbins’s Exploratory Data Analysis and Visualization Community Contribution Project:

Acknowledgments

Resources

Development of this package benefitted from the use of equipment and the support of colleagues at UConn Health and at UF Health.

Contribute

Contributions in any form are more than welcome! Pretty much every fix and feature of this package derives from a problem or question posed by someone with datasets or design goals i hadn’t anticipated. See the CONTRIBUTING file for guidance, and please respect the Code of Conduct.

Cite

If you use ggalluvial-generated figures in publication, i’d be grateful to hear about it! You can also cite the package according to citation("ggalluvial").

Metadata

Version

0.12.5

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows