ggalluvial:基于层内变量的订单流
ggalluvial: Order flow of lines based on a variable within stratum
我使用的是通用糖尿病数据,
处理数据(连续到离散)
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
我想这样绘制,当葡萄糖为 "Positive" 且 BMI 为 "High" 时,它应该是一条红线,而不是我的情况下的 5 条线。
我是 R 编程的新手,我正在探索不同的库来创建此流程图。我尝试使用具有此功能 "layer" 的 "alluvial" 库进行一些尝试,然后在我的情况下,所有内容都按某个值进行排序,我确实为 Daibetes=="Negative"
进行了排序,并且绘图看起来像这样 plot using alluvial library, sorted like all red lines are above blue line in each case
我想使用 ggalluvial 做类似的事情。期待线索。提前致谢。
您需要在 geom_alluvium()
中设置 aes.bind = TRUE
,它会传递给 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()
我使用的是通用糖尿病数据, 处理数据(连续到离散)
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
我想这样绘制,当葡萄糖为 "Positive" 且 BMI 为 "High" 时,它应该是一条红线,而不是我的情况下的 5 条线。
我是 R 编程的新手,我正在探索不同的库来创建此流程图。我尝试使用具有此功能 "layer" 的 "alluvial" 库进行一些尝试,然后在我的情况下,所有内容都按某个值进行排序,我确实为 Daibetes=="Negative"
进行了排序,并且绘图看起来像这样 plot using alluvial library, sorted like all red lines are above blue line in each case
我想使用 ggalluvial 做类似的事情。期待线索。提前致谢。
您需要在 geom_alluvium()
中设置 aes.bind = TRUE
,它会传递给 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()