2

I am using generic Diabetes data, Processing data(continuous to discrete)

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

I want to plot such that when Glucose is "Positive" and BMI is "High", it should one single red line and Not 5 lines as in my case.

I am pretty new to R programming and i am exploring different libraries to create this flow diagram. I tried something with "alluvial" library which has this function "layer", then everything is sorted on some value in my case i did sort it for Daibetes=="Negative" and plot looked like thisplot using alluvial library, sorted like all red lines are above blue line in each case

I want to do something similar using ggalluvial. Look forward to leads. Thanks in advance.

Trupti
  • 33
  • 6

1 Answers1

1

You need to set aes.bind = TRUE in the geom_alluvium() which gets passed to stat_flow() which prioritizes the aesthetics over the axis lodes when plotting.

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

emilliman5
  • 5,816
  • 3
  • 27
  • 37
  • Thank you so much thats exactly what i was looking for. I have a follow up question, I am not sure if should be a separate question. I want to change the color of links from Glucose to BMI. Link from Diabetes to Glucose are red and blue. i want to change the color of links from Glucose to BMI (orange and green). Is it possible using the same library? Thanks, Trupti – Trupti Oct 25 '18 at 09:52