Reputation: 482
The following script
require(ggplot2)
require(gridExtra)
grid_arrange_shared_legend <- function(plots) {
g <- ggplotGrob(plots[[1]] + theme(legend.position="bottom"))$grobs
legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]
lheight <- sum(legend$height)
grid.arrange(arrangeGrob(grobs=lapply(plots, function(x)
x + theme(legend.position="none")),ncol = 3),
legend,
ncol = 1,
heights = unit.c(unit(1, "npc") - lheight, lheight))
}
plotList <- list()
df <- data.frame(value=rnorm(10))
for (i in 1:12){
plotList[[i]] <- qplot(Sepal.Length, Petal.Length, data = iris, color = Species)
}
pdf("plots.pdf",width=12.21/2.54, height=20.92/2.54)
grid_arrange_shared_legend(plotList)
dev.off()
Results in a two page pdf. The first page is empty. The second page contains the desired grid of plots.
Why is the first page empty and how do I get rid of it?
Upvotes: 0
Views: 791
Reputation: 88
I'm not sure why the first page is empty. But setting onefile = FALSE
in pdf
solves the issue.
pdf("plots.pdf",width=12.21/2.54, height=20.92/2.54,onefile=FALSE)
There is another question in Stack Overflow related to this issue. In R, how to prevent blank page in pdf when using gridBase to embed subplot inside plot
Upvotes: 1