R如何相对于轴在ggplot上放置文本

R how to place text on ggplot relative to axis

我正在尝试在 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())

也许作为建议,您可以根据 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())

输出:

您可以使用 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())

并且输出:

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

出于某种原因,当我绘制这个时,我的是相反的(可能是因子顺序),但我认为这就是你想要的?

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) 可能有一些含义,但我不知道它是什么,我不得不 fiddle 使用它,所以这部分不是自动的。