首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在冲积/桑基图的flows项上添加值标签(在R ggalluvia图上)?

如何在冲积/桑基图的flows项上添加值标签(在R ggalluvia图上)?
EN

Stack Overflow用户
提问于 2019-09-01 18:46:02
回答 1查看 2.4K关注 0票数 1

我希望在R上标注冲积层/桑基图的“流”部分。

可以很容易地标记层(柱),但不能标记连接它们的流。我所有的阅读文档和实验的尝试都是徒劳的。

在下面的示例中,"freq“应标记在流量连接部件上。

代码语言:javascript
复制
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")
EN

回答 1

Stack Overflow用户

发布于 2019-09-01 21:04:05

有一个选项可以获取原始数字并将其用作流部分的标签:

代码语言:javascript
复制
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")

如果想要更多地控制如何标记这些点,可以提取图层数据并对其进行计算。例如,我们可以只计算起始位置的分数,如下所示:

代码语言:javascript
复制
# 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")

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57745314

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档