我希望在R上标注冲积层/桑基图的“流”部分。
可以很容易地标记层(柱),但不能标记连接它们的流。我所有的阅读文档和实验的尝试都是徒劳的。
在下面的示例中,"freq“应标记在流量连接部件上。

library(ggplot2)
library(ggalluvial)
data(vaccinations)
levels(vaccinations$response) <- rev(levels(vaccinations$response))
ggplot(vaccinations,
aes(x = survey, stratum = response, alluvium = subject,
y = freq,
fill = response, label = freq)) +
scale_x_discrete(expand = c(.1, .1)) +
geom_flow() +
geom_stratum(alpha = .5) +
geom_text(stat = "stratum", size = 3) +
theme(legend.position = "bottom") +
ggtitle("vaccination survey responses at three points in time")发布于 2019-09-01 21:04:05
有一个选项可以获取原始数字并将其用作流部分的标签:
ggplot(vaccinations,
aes(x = survey, stratum = response, alluvium = subject,
y = freq,
fill = response, label = freq)) +
scale_x_discrete(expand = c(.1, .1)) +
geom_flow() +
geom_stratum(alpha = .5) +
geom_text(stat = "stratum", size = 3) +
geom_text(stat = "flow", nudge_x = 0.2) +
theme(legend.position = "bottom") +
ggtitle("vaccination survey responses at three points in time")

如果想要更多地控制如何标记这些点,可以提取图层数据并对其进行计算。例如,我们可以只计算起始位置的分数,如下所示:
# Assume 'g' is the previous plot object saved under a variable
newdat <- layer_data(g)
newdat <- newdat[newdat$side == "start", ]
split <- split(newdat, interaction(newdat$stratum, newdat$x))
split <- lapply(split, function(dat) {
dat$label <- dat$label / sum(dat$label)
dat
})
newdat <- do.call(rbind, split)
ggplot(vaccinations,
aes(x = survey, stratum = response, alluvium = subject,
y = freq,
fill = response, label = freq)) +
scale_x_discrete(expand = c(.1, .1)) +
geom_flow() +
geom_stratum(alpha = .5) +
geom_text(stat = "stratum", size = 3) +
geom_text(data = newdat, aes(x = xmin + 0.4, y = y, label = format(label, digits = 1)),
inherit.aes = FALSE) +
theme(legend.position = "bottom") +
ggtitle("vaccination survey responses at three points in time")

这仍然是一种判断,关于你到底想要把标签放在哪里。在开始时这样做是最简单的方法,但如果您希望这些标签大致位于中间并彼此闪避,则需要进行一些处理。
https://stackoverflow.com/questions/57745314
复制相似问题