1

I'm trying to reproduce a mobility flow diagram and don't really know how to add additional colour transparency to the fill argument based on axis2 categories. Or whether that's even the way to go about solving this problem!

Any suggestions would be greatly appreciated, thanks!

What I'm trying to achieve: Mobility flow diagram

What I have: My mobility flow diagram example

Code for my mobility flow diagram example:

library(ggplot2)
library(ggalluvial)

oclass <- c("1st", "1st", "1st", "2nd", "2nd", "2nd", "3rd", "3rd", "3rd")
dclass <- c("1st", "2nd", "3rd", "1st", "2nd", "3rd", "1st", "2nd", "3rd")
Freq  <- c(700, 200, 100, 200, 600, 200, 50, 250, 700)

odclass <- data.frame(oclass, dclass, Freq)

ggplot(odclass, aes(y = Freq, axis1 = oclass, axis2 = dclass)) + 
       geom_alluvium(aes(fill = oclass), width = 1/6, reverse = TRUE) +
       geom_stratum(width = 1/6, alpha = 0, reverse = TRUE, color = "black") +
       geom_text(aes(label = after_stat(stratum)), stat = "stratum", reverse = TRUE, size=5) +
       scale_fill_manual(values = c("darkcyan", "darkgoldenrod2", "mediumorchid")) +
       theme_minimal() +
       theme(axis.title.y = element_blank(), axis.text.y= element_blank(), legend.position = "none", 
             plot.title = element_text(hjust=0.5, size=18), axis.text.x = element_blank())
  • Depending on what you're trying to emphasize, I think this isn't a bad way to represent the data, but that's a question for a different forum. StackOverflow is more about 'how' than 'if/why' when it comes to statistics and data viz. See below for answer to 'how'. – Dan Adams Mar 29 '21 at 14:27

1 Answers1

1

It's pretty easy to add what you're after. You just need to map alpha to dclass and then set the values you want using scale_alpha_manual().

library(tidyverse)
library(ggalluvial)
#> Warning: package 'ggalluvial' was built under R version 4.0.4

oclass <- c("1st", "1st", "1st", "2nd", "2nd", "2nd", "3rd", "3rd", "3rd")
dclass <- c("1st", "2nd", "3rd", "1st", "2nd", "3rd", "1st", "2nd", "3rd")
Freq  <- c(700, 200, 100, 200, 600, 200, 50, 250, 700)

odclass <- data.frame(oclass, dclass, Freq)

ggplot(odclass, aes(y = Freq, axis1 = oclass, axis2 = dclass)) + 
  geom_alluvium(aes(fill = oclass, alpha = dclass), width = 1/6, reverse = TRUE) +
  geom_stratum(width = 1/6, alpha = 0, reverse = TRUE, color = "black") +
  geom_text(aes(label = after_stat(stratum)), stat = "stratum", reverse = TRUE, size=5) +
  scale_fill_manual(values = c("darkcyan", "darkgoldenrod2", "mediumorchid")) +
  scale_alpha_manual(values = c(0.9, 0.7, 0.5)) +
  theme_minimal() +
  theme(axis.title.y = element_blank(), axis.text.y= element_blank(), legend.position = "none", 
        plot.title = element_text(hjust=0.5, size=18), axis.text.x = element_blank())
#> Warning in to_lodes_form(data = data, axes = axis_ind, discern =
#> params$discern): Some strata appear at multiple axes.
#> Warning in to_lodes_form(data = data, axes = axis_ind, discern =
#> params$discern): Some strata appear at multiple axes.

#> Warning in to_lodes_form(data = data, axes = axis_ind, discern =
#> params$discern): Some strata appear at multiple axes.

Created on 2021-03-29 by the reprex package (v1.0.0)

Dan Adams
  • 4,971
  • 9
  • 28