1

I am using generic Diabetes data,

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

ggplot(datGluBMIAge,aes(axis1= Diabetes, axis2=Glucose, axis3 = BMI, axis4 = Age, y = freq)) +
geom_alluvium(aes(fill=Diabetes),aes.bind=TRUE, reverse = FALSE, alpha=0.9) +
scale_fill_manual(labels = c("Negative", "Positive"), values = c("#0066BA", "#FF9400")) +
scale_x_discrete(limits = c("Diabetes", "Glucose","BMI", "Age"), expand = c(0, 0)) +
scale_y_continuous(labels = NULL, expand = c(0,0))+
theme(axis.text.x=element_text(margin = margin(t = 0, unit='pt')),
    axis.title.x = element_text(vjust = 0),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank(),
    axis.line = element_blank(),
    axis.ticks = element_blank(),
    legend.position = "none")+
geom_stratum(alpha=1, reverse = FALSE) +
geom_text(stat="stratum", label.strata= TRUE, reverse = FALSE) +
ylab(NULL)+xlab(NULL) +
geom_vline(xintercept = 0)

Above code produces this plot:

I have two question based on the above plot

  1. there are various links going from Glucose="Normal" to BMI='30-35', how do i arrange them such a way that i see only one link going from Glucose="Normal" to BMI='30-35' and all the others

  2. How can I change the color scheme between axis?for example if I want different colors between Glucose and BMI,and even different color between BMI and Age? How can I do it using the ggalluvial library?

any leads would be appreciated. Thank you for your time. Regards,Trupti

Jaap
  • 81,064
  • 34
  • 182
  • 193
Trupti
  • 33
  • 6
  • @MrFlick could you please take time to address my concern? I see it very similar to [link](https://stackoverflow.com/questions/51362907/group-alluvia-in-the-r-alluvial-diagram) – Trupti Oct 26 '18 at 09:28

2 Answers2

0

Instead of using geom_alluvium using geom_flow solves the question 1. I am still exploring to resolve question 2. Thanks, Trupti

Trupti
  • 33
  • 6
0

Since the strata are unique to each axis, you can change the color scheme for the flows between each pair of axes by first converting to long form and then using geom_flow(aes(fill = stratum)). Here's an example derived from the cover illustration to ggalluvial:

ggplot(data = titanic_long,
       aes(x = Demographic, stratum = stratum, alluvium = alluvium,
           y = Freq, label = stratum)) +
  geom_flow(aes(fill = stratum)) +
  geom_stratum() + geom_text(stat = "stratum") +
  theme_minimal() +
  ggtitle("passengers on the maiden voyage of the Titanic",
          "stratified by demographics and survival")

enter image description here

If, on the other hand, you have some strata that appear at multiple axes, you'll need to take the additional step of distinguishing these as separate factor levels. You can do this manually after converting to long form, or by setting discern = TRUE in to_lodes_form() when converting to long form.

Cory Brunson
  • 668
  • 4
  • 10