在 grid.arrange 的左上角找到标题

Locate title in top left of grid.arrange

我有两个地块与 gridExtra::grid.arrange 并排排列。我可以用 top 参数在它们上面加上一个标题。问题是,我被要求在情节的左上角找到标题。

一个可重现的例子:

library(ggplot2)
library(gridExtra)

p1 <- qplot(1:20)
p2 <- qplot(30, 35)
grid.arrange(p1, p2, nrow = 1, top = "Title")

产生

但我需要的是:

我看了好几遍 ?arrangeGrob 文件(我想这就是我的答案),但还没有想出如何实现它。

patchwork 是一个 gridExtra 替代品,其行为更像 ggplot 而不是网格,包括它的 plot_annotate 如何处理标题:

library(ggplot2)
library(patchwork)

qplot(1:20) + qplot(30, 35) + plot_annotation(title = 'Title')

如果你想进一步调整它,它的 theme 参数接受一个 ggplot theme 调用。

使用 top 参数:

grid.arrange(p1, p2, nrow = 1, top = grid::textGrob("Title", x = 0, hjust = 0))