I created 200 * 3 plots (3 rows, 200 columns). I need to save them all in one pdf file on a single page. The code itself does not trigger any errors or warnings but the upper two graphs are being cut and are not visible in the pdf. Changing the width and height did not solve the problem. It also occurs when plotting 50 columns per page.
I used the follwing code to save the data:
ggsave("allplots.pdf", arrangeGrob(grobs = List_allplots, ncol=200), device = "pdf",
width = 2000, height = 20,
units = c("cm"), dpi = 1000, limitsize = FALSE)
Adding nrow = 1
or nrow = 3
does not fix the issue.
List_allplots consists of a patchwork with 200 items (3 patch areas, 1 col, 3 rows each). Plot margins are set to 0.
I tried to save one list item per page, which worked (no cuts). I found that ggsave sometimes outputs cut pdfs, unfortunately none of the provided solutions worked for me.
I reproduced the error with the following code using iris (only Plot3 are visible in the 1-page pdf):
example1 <- function(x)
{
Plot1 <- ggplot(iris[x,], aes(x= Species, y=Sepal.Length))+
geom_point()
Plot2 <- ggplot(iris[x,], aes(x= Species, y=Sepal.Width))+
geom_point()
Plot3 <- ggplot(iris[x,], aes(x= Species, y=Petal.Length))+
geom_point()
Plot1 / Plot2 / Plot3
}
vec <- c(1:150)
ListP <- lapply(vec, example1)
ggsave("test.pdf", arrangeGrob(grobs = ListP, ncol=150), device = "pdf",
width = 1500, height = 20,
units = c("cm"), dpi = 300, limitsize = FALSE)
edit: The number of columns does not trigger the issue with cut plots.
Try converting the output of patchwork to a grob with cowplot::as_grob(). The following works for me:
library(ggplot2)
library(patchwork)
library(gridExtra)
library(cowplot)
example1 <- function(x)
{
Plot1 <- ggplot(iris[x,], aes(x= Species, y=Sepal.Length))+
geom_point()
Plot2 <- ggplot(iris[x,], aes(x= Species, y=Sepal.Width))+
geom_point()
Plot3 <- ggplot(iris[x,], aes(x= Species, y=Petal.Length))+
geom_point()
as_grob(Plot1 / Plot2 / Plot3)
}
vec <- c(1:150)
ListP <- lapply(vec, example1)
ggsave("test.pdf", arrangeGrob(grobs = ListP, ncol=150), device = "pdf",
width = 1500, height = 20,
units = c("cm"), dpi = 300, limitsize = FALSE)