rsankey-diagramdataflow-diagram

Group alluvia in the R alluvial diagram


In the alluvial package, is it possible to combine those alluvia that have the same source and target nodes? For example the two dark alluvia in the image below, that both go through AB and 3.

enter image description here

Edit: Here is an example using the Titanic dataset, which shows the same behaviour:

# Titanic data
tit <- as.data.frame(Titanic)
tit3d <- aggregate( Freq ~ Class + Sex + Survived, data=tit, sum)
ord <- list(NULL, with(tit3d, order(Sex, Survived)), NULL)
alluvial(tit3d[,1:3], freq=tit3d$Freq, alpha=1, xw=0.2,
         col=ifelse( tit3d$Survived == "No", "red", "gray"),
         layer = tit3d$Sex != "Female",
         border="white", ordering=ord)

Solution

  • It looks like the ggalluvial package as a geom_flow which resets at each category break. That might be more of what you want. For example

    # reshape data
    library(dplyr)
    library(tidyr)
    dd <- tit3d %>% mutate(id=1:n(), sc=Survived) %>% 
      gather("category", "value", -c(id, Freq, sc))
    
    # draw plot
    ggplot(dd, aes(x=category, stratum=value, alluvium = id, 
                   label=value))+ 
     geom_flow(aes(fill=sc))  + 
     geom_stratum(alpha = .5) + geom_text(stat = "stratum", size = 3) + 
      theme_minimal()
    

    enter image description here