Alluvium Plot
Alluvium Plot with ggplot2
- Use ggplot2 and ggalluvial package, plus ggrepel package
library(data.table)
library(ggplot2)
library(ggalluvial)
library(ggrepel)
dt <- data.table(
id = 1:150
, col1 = factor(sample(c("I", "II"), size = 150, replace = TRUE, prob = c(0.4, 0.6)))
, col2 = factor(sample(c("A", "B"), size = 150, replace = TRUE, prob = c(0.3, 0.7)))
, col3 = factor(sample(c("alpha", "beta", "gamma", "lambda"), size = 150, replace = TRUE, prob = c(0.3, 0.5, 0.1, 0.1)))
, group = rep(c("T", "C"), 75)
)
fq <- dt[, .(count = .N), by = .(col1, col2, col3, group)]
ggplot(as.data.frame(fq),
aes(y = count
, axis1 = col1
, axis2 = col2
, axis3 = col3
)) +
geom_alluvium(aes(fill = group), width=1/3) +
geom_stratum(width = 1/6) +
geom_text(stat = "stratum", aes(label = after_stat(stratum))) +
## geom_label_repel(stat = "stratum", infer.label = TRUE, label.strata=TRUE) +
scale_x_discrete(limits = c("Col 1", "Col 2"
, "Col 3"
)
, expand = c(.0, .0)) +
## theme_bw() +
theme(panel.grid.major = element_blank()
, panel.grid.minor = element_blank()
, panel.background = element_blank()
, panel.border = element_blank())
geom_flow
ggplot(data = fq
, aes(
y = count
, axis1 = col1
, axis2 = col2
, axis3 = col3
)) +
geom_flow(
aes(fill = group), stat = "alluvium"
, lode.guidance = "rightleft"
, color = "white"
, na.rm = TRUE
) +
scale_x_discrete(limits = c("col1", "col2", "col3")) +
geom_stratum(alpha = .5, na.rm = TRUE, color = "grey") +
geom_text(stat = "stratum", size = 3, aes(label = after_stat(stratum))) +
ggtitle("") +
xlab("") +
theme_minimal()
R sessionInfo
R version 4.2.0 (2022-04-22) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 20.04.3 LTS
Matrix products: default BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0 LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0
locale: [1] LC_CTYPE=C.UTF-8 LC_NUMERIC=C LC_TIME=C.UTF-8
[4] LC_COLLATE=C.UTF-8 LC_MONETARY=C.UTF-8 LC_MESSAGES=C.UTF-8
[7] LC_PAPER=C.UTF-8 LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C
attached base packages: [1] stats graphics grDevices utils datasets methods base
other attached packages: [1] ggrepel_0.9.1 ggalluvial_0.12.3 Wu_0.0.0.9000
[4] flexdashboard_0.5.2 lme4_1.1-29 Matrix_1.4-0
[7] mgcv_1.8-38 nlme_3.1-152 png_0.1-7
[10] scales_1.2.0 nnet_7.3-16 labelled_2.9.1
[13] kableExtra_1.3.4 plotly_4.10.0 gridExtra_2.3
[16] ggplot2_3.3.6 DT_0.23 tableone_0.13.2
[19] magrittr_2.0.3 lubridate_1.8.0 dplyr_1.0.9
[22] plyr_1.8.7 data.table_1.14.2 rmdformats_1.0.4
[25] knitr_1.39
loaded via a namespace (and not attached): [1] httr_1.4.3 sass_0.4.1 tidyr_1.2.0 jsonlite_1.8.0
[5] viridisLite_0.4.0 splines_4.2.0 bslib_0.3.1 assertthat_0.2.1 [9] highr_0.9 yaml_2.3.5 pillar_1.7.0 lattice_0.20-45
[13] glue_1.6.2 digest_0.6.29 rvest_1.0.2 minqa_1.2.4
[17] colorspace_2.0-3 htmltools_0.5.3 survey_4.1-1 pkgconfig_2.0.3
[21] haven_2.5.0 bookdown_0.27 purrr_0.3.4 webshot_0.5.3
[25] svglite_2.1.0 tibble_3.1.7 farver_2.1.0 generics_0.1.2
[29] ellipsis_0.3.2 withr_2.5.0 klippy_0.0.0.9500 lazyeval_0.2.2
[33] cli_3.3.0 survival_3.2-13 crayon_1.5.1 evaluate_0.15
[37] fansi_1.0.3 MASS_7.3-54 forcats_0.5.1 xml2_1.3.3
[41] tools_4.2.0 hms_1.1.1 mitools_2.4 lifecycle_1.0.1
[45] stringr_1.4.0 munsell_0.5.0 compiler_4.2.0 jquerylib_0.1.4
[49] systemfonts_1.0.4 rlang_1.0.4 grid_4.2.0 nloptr_2.0.3
[53] rstudioapi_0.13 htmlwidgets_1.5.4 labeling_0.4.2 rmarkdown_2.14
[57] boot_1.3-28 gtable_0.3.0 DBI_1.1.2 R6_2.5.1
[61] fastmap_1.1.0 utf8_1.2.2 stringi_1.7.8 Rcpp_1.0.8.3
[65] vctrs_0.4.1 tidyselect_1.1.2 xfun_0.31