【问题标题】:Plot data in multiple files using ggplot使用 ggplot 在多个文件中绘制数据
【发布时间】:2016-07-31 10:46:49
【问题描述】:

我有一个时间序列数据文件,其中包含 4 种代谢物 A、B、AE 和 E 随时间变化的浓度。我有很多这种类型的数据文件(大约 100 个)。我想在一张图中绘制所有文件中所有四种代谢物的时间序列。每种代谢物都被分配了一种特定的颜色。

我编译了下面的代码,但是它只在一个文件(最后一个文件)中绘制数据。我认为这是因为当我调用 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)

下图是

可以找到示例文件 here

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    我通常采用以下方法(未经测试,因为缺乏可重复的示例)

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

    【讨论】:

    • 感谢您的回答。我正试图围绕你的解决方案。对我来说看起来有点复杂。我还包含了一些示例文件。
    【解决方案2】:

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

    注意:虽然这个问题有点模棱两可,但我假设您希望每个输入文件都有一个图表。

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

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

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

    【讨论】:

    • @Marchand 我需要一张图表来记录所有文件中的数据。
    • @Marchand 谢谢你的建议。是的,所有文件都以相同的顺序(时间、A、A|E、B、E)具有相同的列。如果我尝试了你的方法,但情节看起来不像它应该是什么样子。我还包含了一些示例文件。
    【解决方案3】:

    我想我解决了这个问题。我承认这个答案有点粗糙。但是,如果我可以在 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-31
      • 1970-01-01
      • 2018-07-02
      相关资源
      最近更新 更多