Group alluvia in the R alluvial diagram

浪子不回头ぞ 提交于 2019-12-14 01:21:46

问题


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.

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)

回答1:


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()



来源:https://stackoverflow.com/questions/51362907/group-alluvia-in-the-r-alluvial-diagram

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!