I have a list of dataframes (e.g. 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? Thanks.
如何使用lapply函数为列表中的所有数据帧生成x,y散点图,然后输出为单页pdf?谢谢。
1 个解决方案
#1
1
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
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()
#1
1
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
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()