ggalluvial:基于层内变量的线序流

时间:2018-10-24 13:29:05

标签: r sankey-diagram

我正在使用通用糖尿病数据, 处理数据(连续到离散)

library("ggalluvial")
dat$Glucose_cat<- cut(dat$Glucose,breaks=c(1,100,125,max(dat$Glucose)), labels = c("Low","Normal","High"))
dat$BMI_cat <- cut(dat$BMI, breaks= c(17,25,30,35,40,max(dat$Age)), labels = c("18-25", "25-30", "30-35", "35-40", "40+"))
dat$Outcome_cat<-cut(dat$Outcome, breaks = c(-Inf,0,Inf), labels = c("Negative", "Positive"))
dat$freq <- 1`

dat3d <- dat[, .(freq3d = .N, freq = sum(freq)), by=list(Glucose_cat, 
BMI_cat, Outcome_cat)]
dat3d<- dat3d[!(is.na(dat3d$BMI_cat))]
dat3d<- dat3d[!(is.na(dat3d$Glucose_cat))]
setnames(dat3d, old = c('Glucose_cat', 'BMI_cat','Outcome_cat'), new = c('Glucose', 'BMI','Diabetes'))

ggplot(dat3d,aes(axis1= Diabetes, axis2=Glucose, axis3 = BMI, y = freq))+
geom_alluvium(aes(fill=Diabetes), reverse = FALSE)+
scale_fill_manual(labels = c("Negative", "Positive"), values = c("blue", "red"))+
scale_x_discrete(limits = c("Glucose", "BMI"), expand = c(.001, .001))+
geom_stratum(alpha=0.6, reverse = FALSE)+
geom_text(stat="stratum", label.strata= TRUE, reverse = FALSE)+
ylab("Frequency")+xlab("Features")+
theme(legend.title = element_text(size=12))+
theme_minimal()

following plot is displayed with the above code

我要绘制图形,以使当葡萄糖为“正”且BMI为“高”时,它应像我的情况一样是一条红线而不是5条线。

我对R编程非常陌生,我正在探索不同的库来创建此流程图。我尝试使用具有此功能“图层”的“冲积”库进行某些操作,然后在我的情况下将所有内容都按某个值进行了排序,我对Daibetes=="Negative"进行了排序,并绘制了如下图plot using alluvial library, sorted like all red lines are above blue line in each case

我想用ggalluvial做类似的事情。期待潜在客户。预先感谢。

1 个答案:

答案 0 :(得分:1)

您需要在aes.bind = TRUE中设置geom_alluvium(),该参数会传递到stat_flow(),从而在绘制时优先考虑美学而不是轴滑移。

ggplot(dat3d,aes(axis1= Diabetes, axis2=Glucose, axis3 = BMI, y = freq3d)) +
  geom_alluvium(aes(fill=Diabetes),aes.bind=TRUE, reverse = FALSE) +
  scale_fill_manual(labels = c("Negative", "Positive"), values = c("blue", "red")) +
  scale_x_discrete(limits = c("Diabetes", "Glucose", "BMI"), expand = c(.001, .001)) +
  geom_stratum(alpha=0.6, reverse = FALSE) +
  geom_text(stat="stratum", label.strata= TRUE, reverse = FALSE) +
  ylab("Frequency")+xlab("Features") +
  theme(legend.title = element_text(size=12)) +
  theme_minimal()

enter image description here