9.11 Alluvial Diagrams

  • When examining the relationship among categorical variables, alluvial diagrams can serve as alternatives to mosaic plots.

  • In an alluvial diagram, blocks represent clusters of observations, and stream fields between the blocks represent changes to the composition of the clusters over time.

  • Using the titanic dataset we’ll create alluvial diagrams with the ggalluvial package, which generates ggplot2 graphs.

library(readr); library(dplyr)
titanic <- read_csv("data/titanic.csv") # input data

titanic_table <- titanic %>%
  group_by(Class, Sex, Survived) %>%
  count()

titanic_table$Survived <- factor(titanic_table$Survived, 
                                 levels = c("Yes", "No"))

head(titanic_table, n=3)
## # A tibble: 3 × 4
## # Groups:   Class, Sex, Survived [3]
##   Class Sex    Survived     n
##   <chr> <chr>  <fct>    <int>
## 1 1st   Female No           4
## 2 1st   Female Yes        141
## 3 1st   Male   No         118