multi-page PDF 的页面标题在 R 中使用 marrangeGrob 和 ggsave

Page titles for multi-page PDF using marrangeGrob and ggsave in R

我正在使用 marrangeGrobggsave 导出一系列图表。当我输出下面的示例代码时,它会在页面顶部给出页码(例如,第 6 页中的第 1 页、第 6 页中的第 2 页...)。我知道这可以很容易地用 top = NULL 删除。但是我想从绘图列表 objects (已命名)或字符向量(例如下面的 plot_names)更改每个页面的标题。

我知道我可以使用 textGrobgpar 等其他功能来更改标题、字体、字体大小等(例如 changing title in multiplot ggplot2 using grid.arrange)。但是我还没有找到一个直接的方法来更改每个页面上的标题。

示例数据

library(ggplot2)
library(gridExtra)

# Create some plots
p1 <- qplot(mpg, wt, data = mtcars, colour = cyl)
p2 <- qplot(mpg, data = mtcars) + ggtitle("title")
p3 <- qplot(mpg, data = mtcars, geom = "dotplot")
plots <- list(p1, p2, p3)
plot_names <- c("A_Plot_1", "A_Plot_2", "B_Plot_1")
names(plots) <- plot_names


# Combine into a list, and change where page numbers will appear
Export <- gridExtra::marrangeGrob(plots, nrow = 1, ncol = 1)

# Export to a pdf file
ggsave(filename = "multipage.plots.pdf", Export, scale = 1.5)

此外,请注意,这些不一定只是顺序图(例如,A_Plot、B_Plot 等)。

预期输出

这是我希望 PDF 的样子。

您可以将 top 参数更改为您想要的内容。默认值为

top = quote(paste("page", g, "of", npages))

因此您可以传递一个引用表达式,该表达式将为每个绘图计算,其中 g 是当前绘图索引,npages 是绘图总数。所以在你的情况下,你可以从你的向量中提取值

Export <- gridExtra::marrangeGrob(plots, nrow = 1, ncol = 1,
                                  top=quote(names(plots)[g]))