R 中的冲积地块:如何 space 地层?

Alluvial plot in R: how to space the strata?

背景

我一直致力于使用 ggplotggalluvial package 创建冲积层图(一种桑基图),以可视化随时间变化的频率差异及其来源。

例如,我创建了一个简单的数据集,其中包含 100 名接受 COVID-19 筛查的假想患者。 在基线时,所有患者的 COVID-19 均为阴性。假设 1 周后,所有患者再次接受检测:现在,30 名患者呈阳性,65 名患者呈阴性,5 名患者的结果不确定。又过了一周,30名阳性患者仍为阳性,10名患者由阴性转阳性,其余为阴性。

data <- data.frame(analysis = as.factor(rep(c("time0", "time1", "time2"), each = 4)), 
                   freq = rep(c(30, 10, 55, 5), 3), 
                   track = rep(1:4, 3),  
                   response = c("neg","neg","neg","neg", "pos", "neg", "neg", "inconc", "pos", "pos", "neg", "neg"))

#   analysis freq track response
#1     time0   30     1      neg
#2     time0   10     2      neg
#3     time0   55     3      neg
#4     time0    5     4      neg
#5     time1   30     1      pos
#6     time1   10     2      neg
#7     time1   55     3      neg
#8     time1    5     4   inconc
#9     time2   30     1      pos
#10    time2   10     2      pos
#11    time2   55     3      neg
#12    time2    5     4      neg

目标

目标是创建一个冲积图,以可视化这些患者随时间推移的“轨迹”(即冲积层),从而可视化两周后结果的来源。类似于:

尝试

我成功完成了图中的主要部分:

library(tidyverse)
library(ggalluvial)

ggplot(data, aes(x = analysis, stratum = response, alluvium = track, y = freq, fill = response), col = "black") +
    geom_flow(stat = "alluvium") +
    geom_stratum(alpha = .5) +
    scale_fill_manual(values = c("grey", "green", "red"))

问题

但是,我无法清楚地区分地层。现在,它们彼此相邻,从而形成一个完整的 'filled' 矩形。

如何使用 R 中的 ggalluvial 包在冲积地块中 space strata/alluvia?

ggalluvial 包的作者将冲积地块定义为:

Alluvial plots are parallel sets plots in which classes are ordered consistently across dimensions and stacked without gaps at each dimension.

你可能想做一个桑基图,一个合理的包是:ggsankey

使用 ggalluvial 你可以这样做:

冲积地块的问题是您不会在地层上的“矿脉”之间分离。


library(ggplot2)
library(ggalluvial)

data$track <- factor(data$track)


ggplot(data, aes(x = analysis, y = freq, stratum = response, alluvium = track)) +
  geom_flow(aes(fill = track), stat = "alluvium") +
  geom_lode(aes(fill = response))+
  geom_text(stat = "stratum", aes(label = response)) +
  scale_fill_manual("Track",
                    breaks = c("1", "2", "3", "4", "neg", "pos", "inconc" ),
                    labels = c("1", "2", "3", "4", "", "", ""),
                    values = c("grey15", "grey40", "grey65", "grey90", "red", "green", "orange"))+
  guides(fill = guide_legend(override.aes = list(alpha = c(`1` = 1, `2` = 1, `3` = 1, `4` = 1,
                                                          neg = 0, pos = 0, inconc = 0))))+
  theme_minimal()

reprex package (v2.0.0)

于 2021-04-18 创建