我正在寻找最方便的方式添加圆角百分比标签到冲积层的地块。在下面的例子中有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()不同。
发布于 2020-04-12 16:48:16
不幸的是,我认为如果不计算初始数据框架中的百分比列,就无法做到这一点。但是,这样做很容易,同时也为标签提供了更大的灵活性:
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
按照stratum_list的建议,增加了永豪的还原
https://stackoverflow.com/questions/60725388
复制相似问题