简体   繁体   English

如何从一个数据框列表中制作多个ggplots,并将它们放在一个pdf页面上?

[英]How can I make multiple ggplots from a list of dataframes and have them on one pdf page?

I have a list of dataframes (eg a simple example below) 我有一个数据框列表(例如下面的一个简单示例)

df1 <- data.frame(v1=c(1:5), v2=c(2:6))
df2 <- data.frame(v1=c(10:50), v2=c(20:60))
df3 <- data.frame(v1=c(100:500), v2=c(200:600))
df4 <- data.frame(v1=c(1000:5000), v2=c(2000:6000))
df.list <- list(df1, df2, df3, df4)

how can I use lapply function to generate x,y scatter plots for all dataframes in the list and then output as a one-page pdf? 如何使用lapply函数为列表中的所有数据框生成x,y散点图,然后输出为一页pdf? Thanks. 谢谢。

Something like this will do the trick 这样的事情可以解决问题

pdf("myplots.pdf")
par(mfrow=c(2,2))
lapply(df.list, function(x) plot(x=x[,1], y=x[,2])) 
dev.off()

ggplot solution ggplot解决方案

library(ggplot2)
library(grid)
library(gridExtra)
p <- lapply(df.list, function(d) ggplot(data = d, aes(x=v1, y=v2)) + geom_point() )

pdf("myplots2.pdf")
do.call("grid.arrange", p)
dev.off()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从具有指定分页符的多页列表中排列多个ggplots - Arrange multiple ggplots from a list on multiple pages with specified page breaks 如何从 R 中的列表创建多个矩阵或数据框? - How can I create multiple matrices or dataframes from a list in R? 同一个pdf页面上有多个R ggplots - Multiple R ggplots on same pdf page 在多页pdf中绘制多个ggplots(每页一个或多个图) - plotting multiple ggplots in a several page pdf (one or several plots per page) 如何编写一个for循环以将多个csv文件读入R并子集数据以生成干净的ggplots数据帧? - How to write a for loop to read multiple csv files into R and subset the data to make clean dataframes for ggplots? 如何将两个 ggplots 放在一起并使它们在 rstudio 中不占用相同的空间(50:50)? - How can I put two ggplots next to each other and make them NOT occupy the same amount of space (50:50) in rstudio? 如何从嵌套数据帧创建直方图并将它们作为对象存储在 r 的列表中? - How can I create histograms from nested dataframes and store them as objects in a list in r? 如何将ggplots的列表列打印为pdf? - how to print a list-column of ggplots to pdf? 一页中的多个 ggplots 在 R 中使用 for Loop - Multiple ggplots in one page using for Loop in R 从列表中数据框的名称中将标题名称添加到ggplots列表中 - Add title name to list of ggplots from the names of dataframes in list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM