本文介绍了在同一页面上绘制多个ggplot2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个工作循环,可以从保存在目录中的每个文件生成并保存单个图。 我想将所有返回的图表文件作为一个2×2网格在多个页面,但不能这样做。 我试图将列表图表保存在列表中 $ b $ (f in 1:length(files)){ $ p $ pltList pltList [] b plot_object< - ggplot2(...)#make ggplot2 plot print(plot_object) pltList [[f]]< - plot_object #save ggplot2 plot in list } jpeg(filename.jpg) par(mfrow = c(2,2))#to每页生成2x2图形 print(pltList [[1]]) print(pltList [[2]]) ... print(pltList [[f]]) dev.off() 问题是生成的保存的.jpg文件只包含最后一个绘图,而不是包含许多页面上所有绘图的2x2网格,这正是我想要的。 编辑 我的第一个问题是如何从列表中的循环保存每个绘图 - 如何查看列表中保存的对象以确保它们已被正确保存? 当我做 print(pltList [1])时,结果输出为: function(x,y,...) UseMethod(plot) < environment:namespace:graphics> 而不是实际情节。看来这些地块并没有像预期的那样被保存在列表中。我该如何纠正? 希望这个问题得到解决后,您的绘图建议就可以运行。页面有多个绘图作为一个绘图,例如:如果有12个绘图,则每页绘制4个绘图。试试这个例子: library(ggplot2) library(cowplot) #列出12个虚拟地块,只有标题正在改变。 pltList< - lapply(1:12,function(i){ ggplot(mtcars,aes(mpg,cyl))+ geom_point()+ ggtitle (Title,i))}) #输出3个jpeg文件,每个文件有4个图。 for(i seq(1,12,4)) ggsave(paste0(Temp,i,。jpeg), plot_grid(pltList [[i]], pltList [[i + 1]], pltList [[i + 2]], pltList [[i + 3]],nrow = 2)) #或者我们可以使用print pdf(TempPDF.pdf) for(i seq(1,12,4)) print( plot_grid(pltList [[i]], pltList [[i + 1]], pltList [[i + 2]], pltList [[i + 3]],nrow = 2)) dev.off() 编辑: 使用gridExtra的另一种方式,如@ user20650所示: library(gridExtra) #输出为PDF pdf(multipage.pdf) #use gridExtra把地块放在一起 marrangeGrob(pltList,nrow = 2,ncol = 2) dev.off() I have a working loop which generates and can save individual plots from each file saved in a directory.I want to plot all of the returned plots in a single file as a 2x2 grid over multiple pages but cannot do this.I have tried to save the plot objects in a listpltList <- list()pltList[]for (f in 1:length(files)){plot_object <- ggplot2(...) #make ggplot2 plotprint(plot_object)pltList[[f]] <- plot_object #save ggplot2 plot in list}jpeg(filename.jpg)par(mfrow=c(2,2)) #to generate 2x2 plot per pageprint(pltList[[1]])print(pltList[[2]])...print(pltList[[f]])dev.off()The problem is that the resulting saved .jpg file only contains the last plot and not a 2x2 grid of all plots over many pages which is what I want.EDITMy first problem is how to save each plot from the loop in the list - how can I view the saved objects from the list to make sure they have been saved correctly?When I do print(pltList[1]), the resulting output is:function (x, y, ...) UseMethod("plot")<bytecode: 0x0000000010f43b78><environment: namespace:graphics>rather than the actual plot. It seems that the plots are not being saved in the list as expected. How can I correct for this?Hopefully, once this is fixed, your plotting suggestions will work. 解决方案 Assuming you need a PDF output where every page has multiple plots plotted as one, e.g.: if there are 12 plots then 4 plots per page.Try this example:library(ggplot2)library(cowplot)# list of 12 dummy plots, only title is changing.pltList <- lapply(1:12, function(i){ ggplot(mtcars,aes(mpg,cyl)) + geom_point() + ggtitle(paste("Title",i))})# outputs 3 jpeg files with 4 plots each.for(i in seq(1,12,4))ggsave(paste0("Temp",i,".jpeg"), plot_grid(pltList[[i]], pltList[[i+1]], pltList[[i+2]], pltList[[i+3]],nrow = 2))# or we can output into 1 PDF with 3 pages using printpdf("TempPDF.pdf")for(i in seq(1,12,4)) print(plot_grid(pltList[[i]], pltList[[i+1]], pltList[[i+2]], pltList[[i+3]],nrow = 2))dev.off()EDIT:Another way using gridExtra, as suggested by @user20650:library(gridExtra)#output as PDFpdf("multipage.pdf")#use gridExtra to put plots togethermarrangeGrob(pltList, nrow=2, ncol=2)dev.off() 这篇关于在同一页面上绘制多个ggplot2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-21 06:30