R中的冲积积层:如何间隔地层? [英] Alluvial plot in R: how to space the strata?

查看:60
本文介绍了R中的冲积积层:如何间隔地层?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

我一直在使用 ggplot

尝试

我设法制作了该图的主要部分:

 库(tidyverse)图书馆ggplot(数据,aes(x =分析,地层=响应,冲积层=轨迹,y =频率,填充=响应),col =黑色")+geom_flow(stat ="alluvium")+geom_stratum(alpha = .5)+scale_fill_manual(值= c(灰色",绿色",红色"))) 

问题

但是,我无法清楚地区分彼此.现在,它们都彼此相邻,从而形成一个完全填充"的矩形.

您如何使用 R 中的 ggalluvial 包在冲积图中将地层/高地分隔开来?

解决方案

ggalluvial软件包的作者将冲积图定义为:

冲积图是平行集图,其中类在维度上的顺序一致并在每个方向上没有间隙地堆叠.

您可能想绘制sankey图,一个合理的软件包是: ggsankey

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.

As example, I have created a simple dataset of 100 imaginary patients that are screened for COVID-19. At baseline, all patients are negative for 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. Yet another week later, the 30 positive patients remain positive, 10 patients go from negative to positive, and the others are negative.

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’ (i.e., alluvia) of these patients over time and, thereby, visualize the origin of the results after two weeks. 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?

解决方案

The author of the ggalluvial package defines alluvial plots as:

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

这篇关于R中的冲积积层:如何间隔地层?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆