使用for循环并排保存多个绘图(来自ggplot2)

使用for循环并排保存多个绘图(来自ggplot2),r,plot,ggplot2,R,Plot,Ggplot2,我像这样将ggplot保存到循环中。我总共有大约300个绘图(I=1,,,300)。我想用pdf文件保存绘图,但在pdf文件中一页(2乘2)保存4个绘图。如果是这样的话,pdf文件的输出应该有75页 for(1:300中的i){ 绘图列表[[i]]=ggplot(out,aes(篮子大小,组,组,项目,颜色=通道2))+ylim(min00,max00)+ 几何点() }我建议使用cowplot和plot\u grid()函数,并使用ggplot2对象附加网格。例如,见: [编辑]: 示例代码(

我像这样将ggplot保存到循环中。我总共有大约300个绘图(
I=1,,,300
)。我想用
pdf
文件保存绘图,但在pdf文件中一页(2乘2)保存4个绘图。如果是这样的话,
pdf
文件的输出应该有75页

for(1:300中的i){
绘图列表[[i]]=ggplot(out,aes(篮子大小,组,组,项目,颜色=通道2))+ylim(min00,max00)+
几何点()

}

我建议使用
cowplot
plot\u grid()
函数,并使用
ggplot2
对象附加网格。例如,见:

[编辑]
示例代码(测试代码):

$
1
[1] 1 2 3

$
2
[1] 4 5 6

$
3
[1] 7 8 9

$
4
[1] 101112

您可以与此额外代码进行比较,以了解发生了什么:

f1 <- rep(seq(1,4), times=3)
split(x,f1)
甚至是包裹

?cowplot

由于已生成绘图列表,因此可以使用
ggplot2
软件包中的
ggsave
功能和
marrangeGrob
功能生成pdf,形成
gridExtra
软件包。下面是一个小示例,它生成八个绘图,并将它们保存在一个两页pdf中,每页四个

library(ggplot2)
library(gridExtra)

plot_list <- vector("list", 8) 
plot_list[[1]] <- ggplot(mtcars) + aes(x = wt, y = mpg)   + geom_point() + ggtitle("This is plot 1")
plot_list[[2]] <- ggplot(mtcars) + aes(x = cyl, y = mpg)  + geom_point() + ggtitle("This is plot 2")
plot_list[[3]] <- ggplot(mtcars) + aes(x = disp, y = mpg) + geom_point() + ggtitle("This is plot 3")
plot_list[[4]] <- ggplot(mtcars) + aes(x = drat, y = mpg) + geom_point() + ggtitle("This is plot 4")
plot_list[[5]] <- ggplot(mtcars) + aes(x = drat, y = mpg) + geom_point() + ggtitle("This is plot 5")
plot_list[[6]] <- ggplot(mtcars) + aes(x = qsec, y = mpg) + geom_point() + ggtitle("This is plot 6")
plot_list[[7]] <- ggplot(mtcars) + aes(x = vs, y = mpg)   + geom_point() + ggtitle("This is plot 7")
plot_list[[8]] <- ggplot(mtcars) + aes(x = gear, y = mpg) + geom_point() + ggtitle("This is plot 8")


### Use your plot_list here:
glist <- lapply(plot_list, ggplotGrob)
ggsave("plots.pdf", marrangeGrob(glist, nrow = 2, ncol = 2))
库(ggplot2)
图书馆(gridExtra)

绘图列表请查看我的编辑,您可以为绘图网格函数指定
plotlist
参数。如果您现在(例如,使用Lappy)创建ggplot对象列表,您可以将该列表转发到针对特定问题的plot网格。您可以执行以下操作:1)创建一个包含所有绘图的绘图列表,2)将列表拆分为4个绘图块,每个块3)使用绘图列表参数在每个块上运行绘图网格5)以2by2排列您可以使用
ncol=2
参数非常感谢。另外,有没有办法缩小比例,比如比例=0.8之类的?我认为通过
scale=0.8
可以修改绘图之间的间距?例如,您可以通过
ggplot2::theme(plot.margin=unit(c(2,2,2,2),“cm”)
在ggplot中设置打印边距。尝试将其添加到绘图中,生成pdf,并根据需要调整
plot.margin
f1 <- rep(seq(1,4), times=3)
split(x,f1)
?split
?plot_grid
?cowplot
library(ggplot2)
library(gridExtra)

plot_list <- vector("list", 8) 
plot_list[[1]] <- ggplot(mtcars) + aes(x = wt, y = mpg)   + geom_point() + ggtitle("This is plot 1")
plot_list[[2]] <- ggplot(mtcars) + aes(x = cyl, y = mpg)  + geom_point() + ggtitle("This is plot 2")
plot_list[[3]] <- ggplot(mtcars) + aes(x = disp, y = mpg) + geom_point() + ggtitle("This is plot 3")
plot_list[[4]] <- ggplot(mtcars) + aes(x = drat, y = mpg) + geom_point() + ggtitle("This is plot 4")
plot_list[[5]] <- ggplot(mtcars) + aes(x = drat, y = mpg) + geom_point() + ggtitle("This is plot 5")
plot_list[[6]] <- ggplot(mtcars) + aes(x = qsec, y = mpg) + geom_point() + ggtitle("This is plot 6")
plot_list[[7]] <- ggplot(mtcars) + aes(x = vs, y = mpg)   + geom_point() + ggtitle("This is plot 7")
plot_list[[8]] <- ggplot(mtcars) + aes(x = gear, y = mpg) + geom_point() + ggtitle("This is plot 8")


### Use your plot_list here:
glist <- lapply(plot_list, ggplotGrob)
ggsave("plots.pdf", marrangeGrob(glist, nrow = 2, ncol = 2))