2016-07-31 41 views
0

我有一个时间序列数据文件,它具有4种代谢物A,B,AE和E随时间的浓度。我有很多这种类型的数据文件(大约100)。我想绘制一张图中所有文件中所有四种代谢物的时间序列。每种代谢物都被赋予一种特定的颜色。使用ggplot在多个文件中绘制数据

我编译了下面的代码,但它只绘制了一个文件(最后一个)的数据。我认为这是因为当我打电话给ggplot()时,它会创建一个新的情节。我试图在四个循环之外创建剧情,但没有奏效。

p = NULL 

for(i in 1:length(filesToProcess)){ 
    fileName = filesToProcess[i] 

    fileContent = read.csv(fileName) 
    #fileContent$Time <- NULL 

    p <- ggplot()+ 
    geom_line(data = fileContent, aes(x = Time, y = A, color = "A"), size =0.8) + 
    geom_line(data = fileContent, aes(x = Time, y = B, color = "B"), size =0.8) + 
    geom_line(data = fileContent, aes(x = Time, y = AE, color = "AE"), size =0.8) + 
    geom_line(data = fileContent, aes(x = Time, y = E, color = "E"), size =0.8) + 
    xlab('Time') + 
    ylab('Metabolite Concentration')+ 
    ggtitle('Step Scan') + 
    labs(color="Metabolites") 

} 
plot(p) 

下面是曲线图enter image description here

示例文件可以发现here

回答

2

我通常采取以下方法(未经测试,因为缺乏可再现例子的)

read_one <- function(f, ...){ 
    w <- read.csv(f, ...) 
    m <- reshape2::melt(w, id = c("Time")) 
    m$source <- tools::file_path_sans_ext(f) # keep track of filename 
    m 
} 

plot_one <- function(d){ 
    ggplot(d, aes(x=Time, y=value)) + 
    geom_line(aes(colour=variable), size = 0.8) + 
    ggtitle('Step Scan') + 
    labs(x = 'Time', y = 'Metabolite Concentration', color="Metabolites") 
} 

## strategy 1 (multiple independent plots) 

ml <- lapply(filesToProcess, read_one) 
pl <- lapply(ml, plot_one) 

gridExtra::grid.arrange(grobs = pl) 

## strategy 2: facetting 

m <- plyr::ldply(filesToProcess, read_one) 
ggplot(m, aes(x=Time, y=value)) + 
    facet_wrap(~source) + 
    geom_line(aes(colour=variable), size = 0.8) + 
    ggtitle('Step Scan') + 
    labs(x = 'Time', y = 'Metabolite Concentration', color="Metabolites") 
+0

谢谢你的答案。我试图围绕你的解决方案来解决问题。这对我来说看起来有点复杂。另外我还包含了一些示例文件。 – SriniShine

0

由于plot(p)在lo外面op,它只会绘制最后生成的图表。在循环内移动plot(p)

注意:虽然这个问题有点含糊不清,但我假设您需要每个输入文件一个图。

编辑:把所有的数据放在一个图中,假设你所有的文件有相同的顺序相同的列。

all_data <- lapply(filesToProcess, read.csv) 
fileContent <- do.call(rbind, all_data) 

然后你可以像上面那样运行ggplot代码(没有循环)。

+0

@Marchand我需要在所有文件中的数据图。 – SriniShine

+0

@Marchand谢谢你的建议。是的,所有文件都具有相同顺序的相同列(时间,A,A | E,B,E)。 HOWere我试过你的方法,情节看起来不像它应该的样子。另外我还包含了一些示例文件。 – SriniShine

0

我想我解决了这个问题。我承认答案有点粗糙。但是,如果我可以初始化for循环之外的“p”变量,它将解决问题。

filesToProcess = readLines("FilesToProcess.txt") 

#initializing the variable with ggplot() object 
p <- ggplot() 

for(i in 1:length(filesToProcess)){ 
    fileName = filesToProcess[i] 
    fileContent = read.csv(fileName) 

    p <- p + 
    geom_line(data = fileContent, aes(x = Time, y = A, color = "A"), size =0.8) + 
    geom_line(data = fileContent, aes(x = Time, y = B, color = "B"), size =0.8) + 
    geom_line(data = fileContent, aes(x = Time, y = AE, color = "AE"), size =0.8) + 
    geom_line(data = fileContent, aes(x = Time, y = E, color = "E"), size =0.8) 

} 

p <- p + theme_bw() + scale_x_continuous(breaks=1:20) + 
    xlab('Time') + 
    ylab('Metabolite Concentration')+ 
    ggtitle('Step Scan') + 
    labs(color="Legend text") 
plot(p) 

enter image description here