R中的冲积地块:如何划分地层?
Posted
技术标签:
【中文标题】R中的冲积地块:如何划分地层?【英文标题】:Alluvial plot in R: how to space the strata? 【发布时间】:2021-07-10 19:27:17 【问题描述】:背景
我一直致力于使用ggplot
和ggalluvial 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"))
问题
但是,我无法清楚地区分这些地层。现在,它们都彼此相邻,从而形成一个完全“填充”的矩形。
如何使用R
中的ggalluvial
包在冲积地块中划分地层/冲积层?
【问题讨论】:
检查matthewdharris.com/2017/11/11/… 【参考方案1】:ggalluvial 包的作者将冲积地块定义为:
Alluvial plots are parallel sets plots in which classes are ordered consistently across dimensions and stacked without gaps at each dimension.
你可能想做一个 sankey plot,一个合理的包是:ggsankey
【讨论】:
【参考方案2】:使用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 创建
【讨论】:
以上是关于R中的冲积地块:如何划分地层?的主要内容,如果未能解决你的问题,请参考以下文章