【问题标题】:R how to place text on ggplot relative to axisR如何相对于轴在ggplot上放置文本
【发布时间】:2020-09-23 20:49:58
【问题描述】:

我正在尝试在 ggplot 中使用 geom_text() 将样本大小放置在条形图上,并由 ggplot 确定定义的限制。对于我的大部分地块,我已经能够将文本放置的 y 值指定为 0。但是,对于一些地块,CI 条延伸到 0 以下。

为了防止在 CI 条上打印样本大小,我想生成代码,指定 geom_text() 的 y 值与定义 x 轴的绘图边距相关。这个想法将生成相对于 x 轴的通用代码,因此无论 y 的最小值在图中的哪个位置,该代码都可以应用于我的所有图。理想情况下,如果可能的话,我想调整我的绘图代码来实现这一点。提前感谢您的意见!

绘图数据库

data <- structure(list(Site_long = structure(c(2L, 2L, 1L, 1L), .Label = c("Hanauma Bay", 
"Waikiki"), class = "factor"), Shelter = structure(c(1L, 2L, 
1L, 2L), .Label = c("Low", "High"), class = c("ordered", "factor"
)), mean = c(0.096818329015544, 0.140368187765273, 0.364490168863912, 
0.452663275851282), sd = c(0.358269615215823, 0.442381836749624, 
0.431417057945072, 0.461599742148954), lower = c(-0.13725115292546, 
-0.148654612244481, 0.198658790631641, 0.337761753133984), upper = c(0.330887810956549, 
0.429390987775027, 0.530321547096184, 0.56756479856858), sample_size = c(9L, 
9L, 26L, 62L)), row.names = c(NA, -4L), groups = structure(list(
    Site_long = structure(1:2, .Label = c("Hanauma Bay", "Waikiki"
    ), class = "factor"), .rows = structure(list(3:4, 1:2), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = 1:2, class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

绘图代码

mult_compare_growth_all <- c("AB", "B", "A", "AB")

growth_plot <- ggplot(data = data, aes(fill=Shelter, y=mean, x=Site_long)) + 
  geom_bar(position = "dodge", stat="identity", width = .8) +
  scale_x_discrete(limits = position) +
  geom_errorbar(aes(ymin = lower, ymax = upper), position = position_dodge(.8), width = .1) +
  geom_text(aes(label = mult_compare_growth_all, y = data$upper), vjust = -.5, position = position_dodge(width = 0.8), size = 4) +
  scale_fill_grey(name = "Shelter", start = .8, end = .2) +
  labs(x = "Site", y = expression(paste("Coral growth (cm"^"2","/quarter)"))) +
  theme_classic(base_size = 14.5) +
  theme(text = element_text(size = 18), legend.position = "none", 
        axis.title.x = element_blank(),
        axis.text.y = element_text(angle = 90),
        axis.text.x = element_blank())

【问题讨论】:

  • 您希望将样本大小放在哪里?在黑色竖线下方?
  • 是的,我想让值刚好在 x 轴上方,而 x 轴应该总是低于 CI 条
  • 不错!我想我有一个简单的解决方案..我已经发布了答案,请查看。

标签: r ggplot2 axis geom-text


【解决方案1】:

您可以使用y = -Inf 来定位文本位置。为了将文本保留在情节中,您必须将vjust 因子与Inf 耦合,否则,文本将掉出情节。

代码如下:

ggplot(data = data, aes(fill=Shelter, y=mean, x=Site_long)) + 
  geom_bar(position = "dodge", stat = "identity", width = .8) +
  geom_errorbar(aes(ymin = lower, ymax = upper), position = position_dodge(.8), width = .1) +
  geom_text(aes(label = mult_compare_growth_all, y = data$upper), vjust = -.5,
            position = position_dodge(width = .8), size = 4) +
  geom_text(aes(label = sample_size, y = -Inf), vjust = -.3,
            position = position_dodge(width = .8)) +
  scale_fill_grey(name = "Shelter", start = .8, end = .2) +
  labs(x = "Site", y = expression(paste("Coral growth (cm"^"2","/quarter)"))) +
  theme_classic(base_size = 14.5) +
  theme(text = element_text(size = 18), legend.position = "none", 
        axis.title.x = element_blank(),
        axis.text.y = element_text(angle = 90),
        axis.text.x = element_blank())

还有输出:

如果这就是你要找的,请告诉我。

【讨论】:

  • 您是否知道在每个数字前添加“n =”的方法?我正在尝试 paste() 但我无法让它工作
  • 想通了!能够使用 paste() 函数来做到这一点[r] geom_text(aes(label = paste("n =",sample_size), y = -Inf), vjust = -.3, position = position_dodge(width = .8)) +
【解决方案2】:

也许作为一个建议,您可以根据 lower 变量的最小值计算放置因子 pf 并添加一些调整量,如下所示:

library(ggplot2)
#Plot
mult_compare_growth_all <- c("AB", "B", "A", "AB")
#Factor for placement
pf <- min(data$lower)-0.02
#Code
ggplot(data = data, aes(fill=Shelter, y=mean, x=Site_long)) + 
  geom_bar(position = "dodge", stat="identity", width = .8) +
  geom_text(aes(y=pf,label=sample_size),position = position_dodge(0.8))+
  geom_errorbar(aes(ymin = lower, ymax = upper), position = position_dodge(.8), width = .1) +
  geom_text(aes(label = mult_compare_growth_all, y = upper),
            vjust = -.5, position = position_dodge(width = 0.8), size = 4)+
  scale_fill_grey(name = "Shelter", start = .8, end = .2) +
  labs(x = "Site", y = expression(paste("Coral growth (cm"^"2","/quarter)"))) +
  theme_classic(base_size = 14.5) +
  theme(text = element_text(size = 18), legend.position = "none",
        axis.title.x = element_blank(),
        axis.text.y = element_text(angle = 90),
        axis.text.x = element_blank())

输出:

【讨论】:

    【解决方案3】:

    由于某种原因,当我绘制这个时,我的被颠倒了(可能是因子顺序),但我认为这就是你想要的?

    ggplot(data = data, aes(fill=Shelter, y=mean, x=Site_long)) + 
      geom_col(aes(fill=Shelter), position = "dodge", width = 0.8) + 
      geom_text(aes(y=lower, group=Shelter, label = sample_size), 
                position = position_dodge(width = 0.8), vjust=1.5) +
      geom_errorbar(aes(ymin = lower, ymax = upper), 
                    position = position_dodge(.8), width = .1) +
      geom_text(aes(label = mult_compare_growth_all, y = data$upper), vjust = -.5, 
                position = position_dodge(width = 0.8), size = 4) +
      scale_fill_grey(name = "Shelter", start = .8, end = .2) +
      labs(x = "Site", y = expression(paste("Coral growth (cm"^"2","/quarter)"))) +
      theme_classic(base_size = 14.5) +
      theme(text = element_text(size = 18), legend.position = "none", 
            axis.title.x = element_blank(),
            axis.text.y = element_text(angle = 90),
            axis.text.x = element_blank())
    

    结果:

    让我稍微分解一下:

    我使用了geom_col(),因为它是为这种条形图构建的,其高度由单个值指定。也可以按照您的方式使用geom_bar() 完成。

    然后在geom_text() 中,您需要group 美学来将标签放在正确的条上,并且您必须指定y=lower 将标签准确地(令人讨厌)放在底部错误栏,但vjust 将其向下移动。值 (1.5) 可能有一些含义,但我不知道它是什么,我不得不摆弄它,这样那部分就不是自动的了。

    【讨论】:

      猜你喜欢
      • 2021-06-14
      • 1970-01-01
      • 1970-01-01
      • 2011-05-01
      • 2021-03-03
      • 2021-03-06
      • 2017-07-26
      • 2021-04-08
      • 2014-02-24
      相关资源
      最近更新 更多