简体   繁体   English

R 中的冲积层 plot:如何分隔地层?

[英]Alluvial plot in R: how to space the strata?

Background背景

I have been working on creating an alluvial plot (kind of Sankey diagram) using ggplot and the ggalluvial package to visualize frequency differences over time and their origins.我一直在努力使用ggplotggalluvial package创建冲积 plot(一种桑基图),以可视化随时间变化的频率差异及其起源。

As example, I have created a simple dataset of 100 imaginary patients that are screened for COVID-19.例如,我创建了一个包含 100 名假想患者的简单数据集,这些患者接受了 COVID-19 筛查。 At baseline, all patients are negative for COVID-19.在基线时,所有患者的 COVID-19 均为阴性。 After let's say 1 week, all patients are tested again: now, 30 patients are positive, 65 are negative and 5 have an inconclusive result.假设 1 周后,再次对所有患者进行检测:现在,30 名患者为阳性,65 名患者为阴性,5 名结果不确定。 Yet another week later, the 30 positive patients remain positive, 10 patients go from negative to positive, and the others are negative.又过了一周,30名阳性患者仍为阳性,10名患者go由阴性转为阳性,其余均为阴性。

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

Goal目标

The goal is to create an alluvial plot to visualize the 'tracks' (ie, alluvia) of these patients over time and, thereby, visualize the origin of the results after two weeks.目标是创建一个冲积层 plot 以可视化这些患者随着时间的推移的“轨迹”(即冲积层),从而在两周后可视化结果的来源。 Something like:就像是:

在此处输入图像描述

Attempt试图

I managed to make the major part of the figure:我设法制作了该图的主要部分:

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

在此处输入图像描述

Question问题

However, I am not able to distinguish the strata from one another clearly.但是,我无法清楚地区分这些地层。 Now, they are all adjacent to one another, which leads to a completely 'filled' rectangle.现在,它们都彼此相邻,这导致了一个完全“填充”的矩形。

How do you space the strata/alluvia in an alluvial plot using the ggalluvial package in R ?如何使用 R 中的ggalluvial package 在冲积层R中间隔地层/冲积层?

The author of the ggalluvial package defines alluvial plots as: ggalluvial package 的作者将冲积地块定义为:

Alluvial plots are parallel sets plots in which classes are ordered consistently across dimensions and stacked without gaps at each dimension.冲积地块是平行集地块,其中类在各个维度上一致排序,并且在每个维度上没有间隙地堆叠。

You probably want to do a sankey plot, a reasonable package is: ggsankey您可能想做一个 sankey plot,一个合理的 package 是: ggsankey

With ggalluvial you could do this:使用ggalluvial你可以这样做:

The thing with alluvial plots is that you do not get separation between the "lodes" on the stratum.冲积地块的问题是,地层上的“矿脉”之间没有分离。


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

Created on 2021-04-18 by the reprex package (v2.0.0)reprex package (v2.0.0) 于 2021 年 4 月 18 日创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM