将轴添加到ggplots网格

时间:2018-10-19 13:19:31

标签: r ggplot2 r-grid

我有一个由多个ggplots组成的网格,想要添加一个x轴,其中在图表之间添加了轴刻度和注释。除了为轴创建自定义图并在下方添加arrangeGrob之外,我想出了一个更好的解决方案。但是它们与图不对齐(我在数字应位于的位置绘制箭头)。还有一个很大的空白,我不想在下面空白。

我还将需要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)

enter image description here

任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

注意:在下面的代码中,我假设网格中的每个图都具有相等的宽度/高度,并且使用等距的标签位置。如果不是这种情况,则必须自己调整位置。

将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)

with x-axis labels added

您可以对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)

with y-axis labels

(请注意,不要盲目地切换上面的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)

with buffer