2015-10-21 70 views
0

下面的腳本GGPLOT2 grid_arrange_shared_legend產生空的陰謀/頁

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() 

結果在兩頁的PDF文件。第一頁是空的。第二頁包含所需的情節網格。

爲什麼第一頁是空的,我該如何擺脫它?

回答