在轴 geom_alluvium 上方添加标题

发布于 2025-01-16 06:09:02 字数 855 浏览 7 评论 0原文

如何在geom_alluvium中的axis1和axis2上方添加标题?

输入图片此处描述

类似于

在此处输入图像描述

当前代码:

library(ggplot2)
library(ggalluvial)



df = data.frame(
  before = factor(c(4,2,3,1,1,1,2,4,2,2,1,4,3), labels = c("a","b","c","d")),
  after  = factor(c(3,3,2,1,3,4,4,1,1,2,2,4,3), labels = c("a","b","c","d")),
  N      = c(4,1,1,2,1,2,1,1,1,1,1,1,1)
)

ggplot(df, aes(y = N, axis1 = before, axis2 = after))  + 
  geom_alluvium(aes(fill = before)) +
  geom_stratum() +
  geom_text(stat = "stratum", mapping = aes(label = after_stat(stratum)))

How to add a title above axis1 and axis2 in geom_alluvium?

enter image description here

Similar to

enter image description here

Current code:

library(ggplot2)
library(ggalluvial)



df = data.frame(
  before = factor(c(4,2,3,1,1,1,2,4,2,2,1,4,3), labels = c("a","b","c","d")),
  after  = factor(c(3,3,2,1,3,4,4,1,1,2,2,4,3), labels = c("a","b","c","d")),
  N      = c(4,1,1,2,1,2,1,1,1,1,1,1,1)
)

ggplot(df, aes(y = N, axis1 = before, axis2 = after))  + 
  geom_alluvium(aes(fill = before)) +
  geom_stratum() +
  geom_text(stat = "stratum", mapping = aes(label = after_stat(stratum)))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聊慰 2025-01-23 06:09:02

您可以添加一个普通的旧geom_label

ggplot(df, aes(y = N, axis1 = before, axis2 = after))  + 
  geom_alluvium(aes(fill = before)) +
  geom_stratum() +
  geom_text(stat = "stratum", mapping = aes(label = after_stat(stratum))) +
  geom_label(inherit.aes = FALSE,
            data = data.frame(x = c(1, 2), y = c(19, 19), 
                              label = c('Before', 'After')),
            aes(label = label, x = x, y = y))

在此处输入图像描述

或者,如果您想让标签看起来像在绘图“上方”,您可以这样做:

ggplot(df, aes(y = N, axis1 = before, axis2 = after))  + 
  geom_alluvium(aes(fill = before)) +
  geom_stratum() +
  geom_text(stat = "stratum", mapping = aes(label = after_stat(stratum))) +
  annotate('rect', xmin = -Inf, xmax = Inf, ymax = Inf, ymin = 18.5,
           fill = 'white') +
  geom_text(inherit.aes = FALSE,
            data = data.frame(x = c(1, 2), y = c(19, 19), 
                              label = c('Before', 'After')),
            aes(label = label, x = x, y = y))

在此处输入图像描述

或者,如果这感觉像是作弊,您可以使用annotate

ggplot(df, aes(y = N, axis1 = before, axis2 = after))  + 
  geom_alluvium(aes(fill = before)) +
  geom_stratum() +
  geom_text(stat = "stratum", mapping = aes(label = after_stat(stratum))) +
  coord_cartesian(clip = 'off', ylim = c(0, 18)) +
  annotate('text', y = c(20, 20), x = c(1, 2), label = c('Before', 'After')) +
  theme(plot.margin = margin(30, 10, 10, 10))

在此处输入图像描述

You could add a plain old geom_label:

ggplot(df, aes(y = N, axis1 = before, axis2 = after))  + 
  geom_alluvium(aes(fill = before)) +
  geom_stratum() +
  geom_text(stat = "stratum", mapping = aes(label = after_stat(stratum))) +
  geom_label(inherit.aes = FALSE,
            data = data.frame(x = c(1, 2), y = c(19, 19), 
                              label = c('Before', 'After')),
            aes(label = label, x = x, y = y))

enter image description here

Or, if you want to make it look like the labels are 'above' the plot, you could do:

ggplot(df, aes(y = N, axis1 = before, axis2 = after))  + 
  geom_alluvium(aes(fill = before)) +
  geom_stratum() +
  geom_text(stat = "stratum", mapping = aes(label = after_stat(stratum))) +
  annotate('rect', xmin = -Inf, xmax = Inf, ymax = Inf, ymin = 18.5,
           fill = 'white') +
  geom_text(inherit.aes = FALSE,
            data = data.frame(x = c(1, 2), y = c(19, 19), 
                              label = c('Before', 'After')),
            aes(label = label, x = x, y = y))

enter image description here

Or, if that feels like cheating, you can use annotate:

ggplot(df, aes(y = N, axis1 = before, axis2 = after))  + 
  geom_alluvium(aes(fill = before)) +
  geom_stratum() +
  geom_text(stat = "stratum", mapping = aes(label = after_stat(stratum))) +
  coord_cartesian(clip = 'off', ylim = c(0, 18)) +
  annotate('text', y = c(20, 20), x = c(1, 2), label = c('Before', 'After')) +
  theme(plot.margin = margin(30, 10, 10, 10))

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文