Reputation: 16856
I am using marrangeGrob
and ggsave
to export a series of graphs. When I output the sample code below, it gives page numbers (e.g., 1 of 6, 2 of 6, ...) at the top of the page. I know this can easily be removed with top = NULL
. But I would like to change the title for each page either from the list of plot objects (which are named) or from a character vector (e.g., plot_names
below).
I know I can use other functions like textGrob
along with gpar
to change the title, font, size of font, etc. (e.g., changing title in multiplot ggplot2 using grid.arrange). But I have not found a straightforward way to change the title on each page.
Sample Data
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)
Also, notice that these are not necessarily just sequential plots (e.g., A_Plot, B_Plot, etc.).
Expected Output
Here is what I would like for the PDFs to look like.
Upvotes: 3
Views: 1768
Reputation: 206466
You can change the top
parameter to something you want. The default value is
top = quote(paste("page", g, "of", npages))
So you can pass a quoted expression that will be evaulated for each plot where g
is the current plot index and npages
is the total number of plots. So in your case, you can extract the values from your vector
Export <- gridExtra::marrangeGrob(plots, nrow = 1, ncol = 1,
top=quote(names(plots)[g]))
Upvotes: 5