ggalluvial做图

2020-04-23 16:46:08 浏览数 (1)

ggalluvial是依赖于ggplot做alluvial plot的一个R包。官方文档:https://cran.r-project.org/web/packages/ggalluvial/vignettes/ggalluvial.html

  1. 下载
代码语言:javascript复制
install.packages('ggalluvial')
  1. 简单作图 以下用Titanic数据集作为例子。数据集的内容:
代码语言:javascript复制
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):

0 人点赞