在 ggalluvial 中为 alluvia 添加标签

cap*_*oma 1 r ggplot2 sankey-diagram

是否有可能为冲积土添加标签ggalluvial

示例图:

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 = response)) +
  scale_x_discrete(expand = c(.1, .1)) +
  geom_flow() +
  geom_stratum(alpha = .5) +
  geom_text(stat = "stratum", size = 3) +
  theme(legend.position = "none") +
  ggtitle("vaccination survey responses at three points in time")
Run Code Online (Sandbox Code Playgroud)

现在我想将主题 id/nrs 作为标签添加到 alluvia(而不是盒子)。有可能这样做吗? 我的原始数据中每个冲积层的主题要少得多(例如 2-5)。

在此输入图像描述

Cor*_*son 5

我很想知道更好的方法来实现这一目标!这是我遇到过的最不糟糕的把戏的一个例子。您的代码的主要更改如下:

  • 美观映射label = response从调用移至ggplot()(第一)geom_text()层,以便它不会干扰其他文本层。
  • 附加geom_text()层与流量统计配对,以便标记每个“流量”图形元素。请注意,此类元素的数量超出了视觉上可区分的范围。
  • 新的文本图层使用新的美学映射label = as.character(subject);主题(实际上是队列)ID 是数字的,因此省略对角色的强制将导致它们毫无意义地添加在一起。(也许这种行为应该可以通过新参数来控制?)
  • 新的文本图层使用美学映射color = survey == "ms153_NSA"来将第一个时间点与后两个时间点分开,并且手动色标使一个不可见(注意:NA比 更合适"00000000")而另一个为黑色。尽管请参阅下面的注释。(设置data = subset(vaccinations, survey != "ms_153_NSA")看起来具有相同的效果,但实际上它可能会混淆标签,因为当删除第一个时间点时,流统计数据对流进行了不同的划分。)
  • 标签从地层到流上的偏移量为nudge_x = -.25

注意:类似这样的东西可以通过冲积层统计产生所需的行为,但流量统计会导致中间时间点(或除第一个和最后一个之外的任何轴)的重叠标签,这并不总是一致,因为流量是左右两侧的排列方式不同。在有关地层和矿脉排序的小插图中,有一些关于两者的玩具示例。我认为目前没有任何办法解决这个问题。

library(ggalluvial)
#> Loading required package: ggplot2
data(vaccinations)
levels(vaccinations$response) <- rev(levels(vaccinations$response))
ggplot(vaccinations,
       aes(x = survey, stratum = response, alluvium = subject,
           y = freq,
           fill = response)) +
  scale_x_discrete(expand = c(.1, .1)) +
  geom_flow() +
  geom_stratum(alpha = .5) +
  geom_text(aes(label = response),
            stat = "stratum", size = 3) +
  geom_text(aes(label = as.character(subject), color = survey == "ms153_NSA"),
            stat = "flow", size = 3, nudge_x = -.25) +
  scale_colour_manual(values = c("#000000ff", "#00000000")) +
  theme(legend.position = "none") +
  ggtitle("vaccination survey responses at three points in time")
Run Code Online (Sandbox Code Playgroud)

由reprex 包(v0.3.0)于 2020-02-19 创建