Reputation: 481
I have an error when trying to plot multiple plots to a PDF. I have used the code from Printing multiple ggplots into a single pdf, multiple plots per page to create my loop, but I am not sure what is causing the error.
The PDF compiles, but it’s just one 1 page and its blank. I have recreated the problem below and it seems like a data dimension issue, but it’s not clear to me how it should be resolved. The plots can be saved manually to PDF from the plot viewer, but it seems to fall over in the loop. My expectation is to get a PDF that's 3 pages long with one chart per page.
library(ggplot2)
library(naniar)
library(visdat)
library(gridExtra)
p = list()
head(iris)
p[[1]] = gg_miss_var(iris)
p[[2]] = vis_miss(iris)
p[[3]] = vis_dat(iris)
pdf("plots_test2.pdf", onefile = TRUE)
for (i in seq(length(p))) {
do.call("grid.arrange", p[[i]])
}
dev.off()
The error that comes up:
Error in `$<-.data.frame`(`*tmp*`, "wrapvp", value = list(x = 0.5, y = 0.5, :
replacement has 17 rows, data has 5
Upvotes: 1
Views: 247
Reputation: 481
Replace do.call
and grid.arrange
with print()
Each plot saves to a single page in the PDF
pdf("plots_test2.pdf", onefile = TRUE)
for (i in seq(length(p))) {
print(p[[i]])
}
dev.off()
Upvotes: 1