将轴添加到 ggplots 的网格

Add axes to grid of ggplots

我有一个由几个 ggplots 组成的网格,我想添加一个 x 轴,在绘图之间添加轴刻度和注释。我想不出比为轴创建自定义绘图并在下方添加 arrangeGrob 更好的解决方案。但它们与图不一致(我在数字应该画的地方画了箭头)。下面还有一个大白space我不要

我还需要一个 y 轴的模拟。

library(ggplot2)
library(gridExtra)
library(ggpubr)
library(grid)

# Create a grid with several ggplots
p <- 
  ggplot(mtcars, aes(wt, mpg)) + 
  geom_point() + 
  theme_transparent() +
  theme(plot.background = element_rect(color = "black"))

main.plot <- arrangeGrob(p, p, p, p, p, p, p, p, ncol = 4, nrow = 2)
# grid.draw(main.plot)

# Now add an x axis to the main plot
x.breaks <- c(0, 1, 2.5, 8, 10)
p.axis <- ggplot() + 
  ylim(-0.1, 0) +
  xlim(1, length(x.breaks)) +
  ggpubr::theme_transparent()

for (i in seq_along(x.breaks)) {
  p.axis <- p.axis +
    geom_text(aes_(x = i, y = -0.01, label = as.character(x.breaks[i])), color = "red")
}
# p.axis

final.plot <- arrangeGrob(main.plot, p.axis, nrow = 2)
grid.draw(final.plot)

感谢任何帮助。

注意:在下面的代码中,我假设您的网格中的每个图都具有相等的宽度/高度,并且使用等间距的标签位置。如果不是这样,你就得自己调整位置了。

将 x 轴添加到 main.plot:

library(gtable)

# create additional row below main plot
# height may vary, depending on your actual plot dimensions
main.plot.x <- gtable_add_rows(main.plot, heights = unit(20, "points"))

# optional: check results to verify position of the new row
dev.off(); gtable_show_layout(main.plot.x)

# create x-axis labels as a text grob
x.axis.grob <- textGrob(label = x.breaks,
                        x = unit(seq(0, 1, length.out = length(x.breaks)), "npc"),
                        y = unit(0.75, "npc"),
                        just = "top")

# insert text grob
main.plot.x <- gtable_add_grob(main.plot.x,
                               x.axis.grob,
                               t = nrow(main.plot.x), 
                               l = 1, 
                               r = ncol(main.plot.x),
                               clip = "off")

# check results
dev.off(); grid.draw(main.plot.x)

您可以对 y 轴执行相同的操作:

# create additional col
main.plot.xy <- gtable_add_cols(main.plot.x, widths = unit(20, "points"), pos = 0)

# create y-axis labels as a text grob
y.breaks <- c("a", "b", "c") # placeholder, since this wasn't specified in the question
y.axis.grob <- textGrob(label = y.breaks,
                        x = unit(0.75, "npc"),
                        y = unit(seq(0, 1, length.out = length(y.breaks)), "npc"),
                        just = "right")

# add text grob into main plot's gtable
main.plot.xy <- gtable_add_grob(main.plot.xy,
                               y.axis.grob,
                               t = 1, 
                               l = 1, 
                               b = nrow(main.plot.xy) - 1,
                               clip = "off")

# check results
dev.off(); grid.draw(main.plot.xy)

(注意上面x轴后y轴的顺序不能盲目调换,如果是加行/列,经常使用gtable_show_layout()查看最新的gtable是个好习惯对象尺寸,并确保将新的 grob 插入正确的单元格。)

最后,让我们在所有边上添加一些缓冲区,这样标签和绘图边框就不会被切断:

final.plot <- gtable_add_padding(main.plot.xy,
                                 padding = unit(20, "points"))

dev.off(); grid.draw(final.plot)