我有60个数据框,这些数据框具有相同的列名存储在我使用此代码获得的列表中:
setwd("C:/Users/Visitor/Desktop/Unesco/")
temp = list.files(pattern="*.csv")
myfiles = lapply(temp, read.csv)
我的一个csv文件的一部分:
"","PRO","TRA","MEN","ENF","COU","TOI","REP","SOM","TEL","LOI"
"HAU","610","140","60","10","120","95","115","760","175","315"
"FAU","475","90","250","30","140","120","100","775","115","305"
"FNU","105","0","495","110","170","110","130","785","160","430"
"HMU","616","141","65","10","115","90","115","765","180","305"
"FMU","179","29","421","87","161","112","119","776","143","373"
我做了一个ggplot的所有数据框的PRO
和TRA
列合并在一个数据框中:
library(dplyr)
library(reshape2)
library(ggplot2)
cols <-lapply(myfiles,function(x)select(x,PRO,TRA))
big_df2 <- do.call(rbind,cols)
df.m2 <- melt(big_df2)
ggplot(df.m2) + geom_freqpoly(aes(x = value,
+ y = ..density.., colour = variable))
我有这个输出:
但我想分别为我的所有数据框绘制相同的图,但我似乎无法找到如何做到这一点。
也许我可以在我的列表中使用此循环执行某些操作:
for(i in 1:length(myfiles)){
myfiles[[i]]$df_num <- i
}
答案 0 :(得分:0)
试试这个例子:
library(ggplot2)
library(dplyr)
library(reshape2)
# dummy data
set.seed(1)
df1 <- cars[sample(seq(nrow(cars)), 10), ]
df2 <- cars[sample(seq(nrow(cars)), 10), ]
df3 <- cars[sample(seq(nrow(cars)), 10), ]
mylist <- list(df1, df2, df3) #this is similar example of your "myfiles"
mylist <- lapply(mylist, melt)
# merge them with an id column
df_merged <- bind_rows(mylist, .id = "df_id")
# Option 1: One plot as facets, 1 file 1 page
ggsave("option1.PDF",
ggplot(df_merged, aes(value, col = variable)) + geom_freqpoly() +
facet_grid(.~df_id)
)
# Option 2: plot into files - output 3 PDF files
for(i in unique(df_merged$df_id)){
filePDF <- paste0("option2_", i, ".PDF")
myPlot <- df_merged %>%
filter(df_id == i) %>%
ggplot(aes(value, col = variable)) + geom_freqpoly()
ggsave(filePDF, myPlot)
}
# Option 3: plot into one file 3 pages
pdf("option3.PDF")
for(i in seq(length(df_merged))){
plotDat <- df_merged %>%
filter(df_id == i)
myPlot <- ggplot(plotDat, aes(value, col = variable)) + geom_freqpoly()
print(myPlot)
}
dev.off()