这是R代码,我尝试在1页上创建一个包含2个图形的pdf,在下一页上创建3个图表,但它没有告诉我使用arrangeGrob。我在使用grid.arrange时做错了什么?感谢
library(ggplot2)
testfunc <- function() {
pdf("test.pdf", width=8.5, height=11, onefile=TRUE)
a <- ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()
b <- ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()
args.list <- list(a=a, b=b, nrow=1, ncol=2)
x <- ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()
y <- ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()
z <- ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()
args.list1 <- list(x=x, y=y, z=z, nrow=1, ncol=3)
#grid.arrange(x, y, z) # 1 graph on each page
#do.call(grid.arrange, args.list)
args.list2 <- list(args.list, args.list1)
do.call(grid.arrange, args.list2)
dev.off()
}
testfunc()
答案 0 :(得分:2)
如果所有网页都采用相同的布局,您可以使用marrangeGrob
,但在这里您最好不要在布局参数列表上循环,
library(gridExtra)
lp <- replicate(5, rectGrob(), simplify = FALSE)
layouts <- list(c(lp[1:2], list(nrow=1, ncol=2)),
c(lp[3:5], list(nrow=1, ncol=3)))
pdf("multi.pdf")
for(page in seq_along(layouts))
do.call(grid.arrange, layouts[[page]])
dev.off()
# more terse
pdf("multi.pdf")
invisible(lapply(layouts, function(x) do.call(grid.arrange, x)))
dev.off()