如何使用 ggalluvial 向冲积地块中的地层添加百分比值?
How to add percentage values to strata in alluvial plot with ggalluvial?
我正在寻找将四舍五入百分比标签添加到冲积地层的最方便的方法。
以下示例中有 50 个案例。独立于第 1 或第 2 阶段,每个案例都属于一组 A、B 或 C。我想显示每个阶段的相关组隶属关系。
library(ggplot2)
library(ggalluvial)
df <- data.frame('id' = rep(1:50,2),
'stage' = c(rep(1,50), rep(2,50)),
'group' = sample(c('A','B','C'), 100, replace = TRUE))
ggplot(df,
aes(x = stage, stratum = group, alluvium = id, fill = group)) +
scale_x_discrete(expand = c(.1, .1)) +
geom_flow() +
geom_stratum(alpha = .5)
有没有办法在不计算初始数据框中的百分比列的情况下向层(条形段)添加四舍五入的百分比标签(包括“%”)?如果我没有完全弄错的话,geom_text 在这里的工作方式与 geom_bar() 中的工作方式不同。
遗憾的是,我认为您还不能在不计算初始数据框中的百分比列的情况下执行此操作。但这可以很容易地完成,并且还为标签提供了更大的灵活性:
library(ggplot2)
library(ggalluvial)
df <- data.frame('id' = rep(1:50,2),
'stage' = c(rep(1,50), rep(2,50)),
'group' = sample(c('A','B','C'), 100, replace = TRUE))
# the list needs to be reversed, as stratums are displayed reversed in the alluvial by default
stratum_list <- df %>%
group_by(stage, group) %>%
summarize(s = n()) %>%
group_by(stage) %>%
mutate(s = percent(s/sum(s), accuracy=0.1)) %>%
arrange(stage, -as.numeric(group)) %>%
.$s
ggplot(df,
aes(x = stage, stratum = group, alluvium = id, fill = group)) +
scale_x_discrete(expand = c(.1, .1)) +
geom_flow() +
geom_stratum(alpha = .5) +
geom_text(stat = "stratum", label=stratum_list)
更新 [13/04/2020]
按照Yonghao的建议添加了stratum_list
还原
这个问题的标准 ggplot2 解决方案是使用“计算美学”。这些美学规范不是来自传递给 ggplot()
的数据,而是来自统计转换的输出(stat_*()
),用于呈现图形元素(geom_*()
) .此输出的列(用户很少看到)称为“计算变量”。 The documentation on this topic 是有限的并且有点过时,使用 stat()
而不是 after_stat()
来调用它们。由于ggalluvial不支持计算变量,当时@bencekd的回答是正确的。
截至今天,v0.12.0 已在 CRAN 上提供计算变量的支持和文档。特别是,三个计算变量可用,它们对应于 stat_bin()
或 stat_count()
使用的同名变量:n
、count
([=18= 的加权版本]), 和 prop
(根据 count
计算的轴内比例)。您似乎想要使用 prop
,如下图所示:
library(ggplot2)
library(scales)
library(ggalluvial)
df <- data.frame('id' = rep(1:50,2),
'stage' = c(rep(1,50), rep(2,50)),
'group' = sample(c('A','B','C'), 100, replace = TRUE))
ggplot(df,
aes(x = stage, stratum = group, alluvium = id, fill = group)) +
scale_x_discrete(expand = c(.1, .1)) +
geom_flow() +
geom_stratum(alpha = .5) +
geom_text(stat = "stratum",
aes(label = percent(after_stat(prop), accuracy = .1)))
由 reprex package (v0.3.0)
于 2020-07-14 创建
我正在寻找将四舍五入百分比标签添加到冲积地层的最方便的方法。 以下示例中有 50 个案例。独立于第 1 或第 2 阶段,每个案例都属于一组 A、B 或 C。我想显示每个阶段的相关组隶属关系。
library(ggplot2)
library(ggalluvial)
df <- data.frame('id' = rep(1:50,2),
'stage' = c(rep(1,50), rep(2,50)),
'group' = sample(c('A','B','C'), 100, replace = TRUE))
ggplot(df,
aes(x = stage, stratum = group, alluvium = id, fill = group)) +
scale_x_discrete(expand = c(.1, .1)) +
geom_flow() +
geom_stratum(alpha = .5)
有没有办法在不计算初始数据框中的百分比列的情况下向层(条形段)添加四舍五入的百分比标签(包括“%”)?如果我没有完全弄错的话,geom_text 在这里的工作方式与 geom_bar() 中的工作方式不同。
遗憾的是,我认为您还不能在不计算初始数据框中的百分比列的情况下执行此操作。但这可以很容易地完成,并且还为标签提供了更大的灵活性:
library(ggplot2)
library(ggalluvial)
df <- data.frame('id' = rep(1:50,2),
'stage' = c(rep(1,50), rep(2,50)),
'group' = sample(c('A','B','C'), 100, replace = TRUE))
# the list needs to be reversed, as stratums are displayed reversed in the alluvial by default
stratum_list <- df %>%
group_by(stage, group) %>%
summarize(s = n()) %>%
group_by(stage) %>%
mutate(s = percent(s/sum(s), accuracy=0.1)) %>%
arrange(stage, -as.numeric(group)) %>%
.$s
ggplot(df,
aes(x = stage, stratum = group, alluvium = id, fill = group)) +
scale_x_discrete(expand = c(.1, .1)) +
geom_flow() +
geom_stratum(alpha = .5) +
geom_text(stat = "stratum", label=stratum_list)
更新 [13/04/2020]
按照Yonghao的建议添加了stratum_list
还原
这个问题的标准 ggplot2 解决方案是使用“计算美学”。这些美学规范不是来自传递给 ggplot()
的数据,而是来自统计转换的输出(stat_*()
),用于呈现图形元素(geom_*()
) .此输出的列(用户很少看到)称为“计算变量”。 The documentation on this topic 是有限的并且有点过时,使用 stat()
而不是 after_stat()
来调用它们。由于ggalluvial不支持计算变量,当时@bencekd的回答是正确的。
截至今天,v0.12.0 已在 CRAN 上提供计算变量的支持和文档。特别是,三个计算变量可用,它们对应于 stat_bin()
或 stat_count()
使用的同名变量:n
、count
([=18= 的加权版本]), 和 prop
(根据 count
计算的轴内比例)。您似乎想要使用 prop
,如下图所示:
library(ggplot2)
library(scales)
library(ggalluvial)
df <- data.frame('id' = rep(1:50,2),
'stage' = c(rep(1,50), rep(2,50)),
'group' = sample(c('A','B','C'), 100, replace = TRUE))
ggplot(df,
aes(x = stage, stratum = group, alluvium = id, fill = group)) +
scale_x_discrete(expand = c(.1, .1)) +
geom_flow() +
geom_stratum(alpha = .5) +
geom_text(stat = "stratum",
aes(label = percent(after_stat(prop), accuracy = .1)))
由 reprex package (v0.3.0)
于 2020-07-14 创建