ggalluvial是依赖于ggplot做alluvial plot的一个R包。官方文档:https://cran.r-project.org/web/packages/ggalluvial/vignettes/ggalluvial.html
- 下载
install.packages('ggalluvial')
- 简单作图 以下用Titanic数据集作为例子。数据集的内容:
library(ggalluvial)
data(Titanic)
head(as.data.frame(Titanic))
做出泰坦尼克号的生还率与性别和舱位分类的关系图:
代码语言:javascript复制ggplot(as.data.frame(Titanic),
aes(y = Freq,
axis1 = Survived, axis2 = Sex, axis3 = Class))
geom_alluvium(aes(fill = Class),
width = 0, knot.pos = 0, reverse = FALSE)
guides(fill = FALSE)
geom_stratum(width = 1/8, reverse = FALSE)
geom_text(stat = "stratum", infer.label = TRUE, reverse = FALSE)
scale_x_continuous(breaks = 1:3, labels = c("Survived", "Sex", "Class"))
coord_flip()
ggtitle("Titanic survival by class and sex")
去掉cood_flip():
geom_alluvium是指的图上的颜色分组,如果修改为aes(fill = Survived):