Skip to content

Instantly share code, notes, and snippets.

@abikoushi
Last active November 11, 2025 03:50
Show Gist options
  • Select an option

  • Save abikoushi/e10ff2f6eb94f0d29aea1ca67e006fe8 to your computer and use it in GitHub Desktop.

Select an option

Save abikoushi/e10ff2f6eb94f0d29aea1ca67e006fe8 to your computer and use it in GitHub Desktop.
Try ggalluvial (Sankey Chart)
library(ggalluvial)
library(dplyr)
titanic_wide <- data.frame(Titanic)
titanic1 = group_by(titanic_wide,Class, Age, Survived) %>%
summarise(Freq = sum(Freq)) %>%
ungroup() %>%
mutate(dummy=1L)
titanic2 = group_by(titanic_wide,Class, Sex, Survived) %>%
summarise(Freq = sum(Freq)) %>%
ungroup()%>%
mutate(dummy=2L)
titanic_d = bind_rows(rename(titanic1, D=Age),
rename(titanic2, D=Sex)) %>%
mutate(dummy = factor(dummy))
ggplot(data = titanic_d,
aes(axis1 = Class, axis2 = D, axis3 = Survived,
y = Freq)) +
geom_alluvium(aes(fill=Survived)) +
geom_stratum() +
geom_text(stat = "stratum", aes(label = after_stat(stratum))) +
facet_grid(rows=vars(dummy))+
theme_minimal() +
scale_x_discrete(limits = c("Class","demographic", "Survived"), expand = c(.2, .05))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment