patchwork
patchwork copied to clipboard
ggsave() to single PDF file
Without patchwork, I can save a list of plots into a single PDF file (with several pages) using gridExtra::marrangeGrob().
For example:
library(gridExtra)
p1 <- ggplot(mapping = aes(x = 1:10, y = rnorm(10))) + geom_point()
p2 <- ggplot(mapping = aes(x = 1:10, y = rnorm(10))) + geom_point()
p3 <- ggplot(mapping = aes(x = 1:10, y = rnorm(10))) + geom_point()
p4 <- ggplot(mapping = aes(x = 1:10, y = rnorm(10))) + geom_point()
p <- list(p1, p2, p3, p4)
print(p)
ggsave(filename = "plots.pdf", plot = marrangeGrob(p, nrow = 1, ncol = 1))
But when I use patchwork, this doesn't work anymore, as only parts of the patchwork are saved, although print() works fine:
library(patchwork)
p12 <- p1 / p2
p34 <- p3 / p4
p12.34 <- list(p12, p34)
print(p12.34)
ggsave(filename = "plots-patched.pdf", plot = marrangeGrob(p12.34, nrow = 1, ncol = 1))
Am I missing something or is there an issue? I don't necessarily need to use marrangeGrob(), but I'd like to save all plots into one PDF file; any other way?
Thank you!