如何使用 hjust 仅在一个图上移动标签?

How to use hjust to move the label on only one plot?

我正在使用 plot_grid 亚马逊河流域流量和表面积与物种丰富度之间的关系创建多面板图。这是我的数据:

riverssub<-structure(list(richness = c(127L, 110L, 89L, 74L, 62L, 18L, 22L, 
71L, 38L, 91L, 56L, 39L, 90L, 37L, 147L, 53L, 92L, 207L, 52L, 
126L, 79L, 32L, 100L, 181L, 83L, 690L), surface = c(33490, 4410, 
770, 164.7, 288.5, 9.85, 33.1, 750, 46.9, 970, 85.2, 39.2, 780, 
97.3, 3983.71, 220, 500, 11250, 115, 1350, 278, 23.05, 310, 2050, 
560, 34570), disch = c(2640L, 687L, 170L, 353L, 384L, 16L, 31L, 
513L, 32L, 392L, 50L, 32L, 206L, 81L, 1260L, 104L, 220L, 6100L, 
308L, 2060L, 443L, 102L, 348L, 4758L, 913L, 40487L)), class = "data.frame", row.names = c(NA, 
-26L))

这是我的图表和多图的代码:

library(cowplot)
a <- ggplot(data = riverssub, aes(x = surface , y = richness)) + 
  geom_point() + 
  scale_y_log10() +
  scale_x_log10() + 
  labs(x='Surface Area (100 km\u00b2)', y="Fish Species Richness") +
  theme_bw()
b <- ggplot(data = riverssub, aes(x = disch , y = richness)) + 
  geom_point() + 
  scale_y_log10() +
  scale_x_log10() + 
  labs(x=bquote('Mean Annual Discharge'~(m^3 * s^-1)), y=" ") +
  theme_bw()
plot_grid(a, b + theme(axis.text.y = element_blank()),
          nrow = 1, align = 'h', labels="AUTO", label_y=0.97, label_x=0.1)

我希望“A”标签在第一个图上的位置与“B”标签在第二个图上的位置相同。我知道我可以在 plot_grid() 中使用 hjust() 来实现这一点,尽管我不确定该怎么做。谁能帮忙?提前致谢。

与其摆弄 hjust 来放置标签,我建议在通过 plot_grid 对齐之前在图上添加标签,正如@Guillaume 在他的评论中已经建议的那样。这样做并确保标签将放在相同的相对位置的一种选择是使用 annotation_custom:

library(cowplot)
library(ggplot2)
library(magrittr)

a <- ggplot(data = riverssub, aes(x = surface, y = richness)) +
  geom_point() +
  scale_y_log10() +
  scale_x_log10() +
  labs(x = "Surface Area (100 km\u00b2)", y = "Fish Species Richness") +
  theme_bw()

b <- ggplot(data = riverssub, aes(x = disch, y = richness)) +
  geom_point() +
  scale_y_log10() +
  scale_x_log10() +
  labs(x = bquote("Mean Annual Discharge" ~ (m^3 * s^-1)), y = " ") +
  theme_bw() +
  theme(axis.text.y = element_blank())

list(A = a, B = b) %>%
  purrr::imap(function(x, y) x + annotation_custom(grid::textGrob(label = y, x = .05, y = .97, gp = grid::gpar(fontface = "bold")))) %>%
  plot_grid(plotlist = ., nrow = 1, align = "h")