从ggplot中的几个大数据文件中绘制数据

时间:2013-05-14 22:50:34

标签: r ggplot2

我有几个数据文件(数字),大约有150000行和25列。在我使用gnuplot(其中脚本行是比例绘图对象)来绘制数据之前,我现在必须做一些额外的分析,我转移到 R 和ggplot2。

如何组织数据?是一个带有附加列的大数据框,用于标记数据来自哪个文件真的是唯一选项?或者有什么方法吗?

编辑:为了更准确一点,我将以现有数据的形式给出一个例子:

filelst=c("filea.dat", "fileb.dat", "filec.dat")
dat=c()
for(i in 1:length(filelst)) {
    dat[[i]]=read.table(file[i])
}

2 个答案:

答案 0 :(得分:2)

假设您的文件名以“.dat”结尾,这里是Chase提出的策略的模型示例,

require(plyr)

# list the files
lf = list.files(pattern = "\.dat")
str(lf)

# 1. read the files into a data.frame
d = ldply(lf, read.table, header = TRUE, skip = 1) # or whatever options to read
str(d) # should contain all the data, and and ID column called L1

# use the data, e.g. plot
pdf("all.pdf")
d_ply(d, "L1", plot, t="l")
dev.off()
# or using ggplot2
ggplot(d, aes(x, y, colour=L1)) + geom_line()

# 2. read the files into a list

ld = lapply(lf, read.table, header = TRUE, skip = 1) # or whatever options to read
names(ld) = gsub("\.dat", "", lf) # strip the file extension
str(ld) 

# use the data, e.g. plot
pdf("all2.pdf")
lapply(names(l), function(ii) plot(l[[ii]], main=ii), t="l")
dev.off()

# 3. is not fun

答案 1 :(得分:1)

你的问题有点模糊。如果我顺利进行,我认为你有三个主要选择:

  1. 按照您的建议进行操作,然后使用R中存在的任何一种“拆分 - 应用 - 组合”功能按组进行分析。这些功能可能包括byaggregateavepackage(plyr)package(data.table)以及其他许多功能。
  2. 将您的数据对象存储为list()中的单独元素。然后使用lapply()和朋友来处理它们。
  3. 将所有内容保存在不同的数据对象中,并单独处理它们。这可能是最有效的做事方式,除非你有内存限制等等。