【问题标题】:Get multiple density plots of specifics columns for every data frame in a list为列表中的每个数据框获取特定列的多个密度图
【发布时间】:2017-03-22 12:03:25
【问题描述】:

我有 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"

我对我的所有数据框的 PROTRA 列做了一个 ggplot 合并到一个数据框中:

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
}

【问题讨论】:

  • 这个问题有很多很多的解决方案,请展示一些研究和代码工作。
  • 我加了很多东西^^。
  • 将数据帧列表转换为单个big_df2 数据帧时,为每个数据帧添加 id(例如,从 data.table 包中读取 rbindlist)然后在 ggplot 中使用 facet。
  • 你能给我举个例子吗?

标签: r list dataframe ggplot2


【解决方案1】:

试试这个例子:

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()

【讨论】:

  • Thx 但如果我希望每个图都包含每个数据帧的具体列,它是否有效?我现在无法测试。
  • 好吧,我只需要使用 lapply select() 列出子集 data.frame 吗?
  • @JulienNguyen 试试看,如果有问题请告诉我们。
猜你喜欢
  • 1970-01-01
  • 2020-09-06
  • 2019-10-15
  • 1970-01-01
  • 2020-04-11
  • 2021-07-28
  • 2021-09-06
  • 2021-10-21
  • 1970-01-01
相关资源
最近更新 更多