【问题标题】:Loop through dataframe and plot time series based on common column循环数据框并根据公共列绘制时间序列
【发布时间】:2015-04-21 20:24:42
【问题描述】:

所以我有一个数据框,其中包含两个读数的板 ID、时间点、底物和平均值(组成数据):

Plate_ID    Day    Name           590_Mean    590_SD    750_Mean    750_Mean
MCBA15 001  0      Cyclodextrin   0.217       0.012     0.161       0.012
MCBA15 001  1      Cyclodextrin   0.257       0.012     0.171       0.012
MCBA15 001  3      Cyclodextrin   0.217       0.012     0.161       0.012
...
MCBA15 001  10     Cyclodextrin   0.217       0.012     0.161       0.012
MCBA15 005  0      Cyclodextrin   0.217       0.012     0.161       0.012
MCBA15 005  1      Cyclodextrin   0.257       0.012     0.171       0.012
MCBA15 005  3      Cyclodextrin   0.217       0.012     0.161       0.012
...
MCBA15 005  10     Cyclodextrin   0.217       0.012     0.161       0.012
MCBA15 001  0      Lactose        0.217       0.012     0.161       0.012
MCBA15 001  1      Lactose        0.257       0.012     0.171       0.012
MCBA15 001  3      Lactose        0.217       0.012     0.161       0.012
...
MCBA15 001  10     Lactose        0.217       0.012     0.161       0.012
MCBA15 005  0      Lactose        0.217       0.012     0.161       0.012
MCBA15 005  1      Lactose        0.257       0.012     0.171       0.012
MCBA15 005  3      Lactose        0.217       0.012     0.161       0.012
...
MCBA15 005  10     Lactose        0.217       0.012     0.161       0.012

每个 Plate_ID 有 32 个基材,每个板有 7 天的读数。

理想情况下,我想用标准差条(时间间隔 = 1 天)绘制 10 天期间(7 个读数)上同一时间序列的 590 和 750 平均值。

我能够生成一个这样的图表,但这是对数据进行排序。然后我采取了以下方法:

library('Hmisc')

x <- sortbiolog$Day[1:7]
y <- sortbiolog$X750_Mean[1:7]
sd <- sortbiolog$X750_SD[1:7]

plot(x, y, type = "b", ylim = c(0,.3))
with(
  data = sortbiolog,
  expr = errbar(x, y, y + sd, y - sd, add = T, pch =1, cap = .01), main = "?-Cyclodextrin Substrate for MCBA15 001")

par(new=TRUE)

a <- sortbiolog$Day[1:7]
b <- sortbiolog$X590_Mean[1:7]
ab_sd <- sortbiolog$X590_SD[1:7]

plot(a, b, type = "b", ylim = c(0,.3))
with(
  data = sortbiolog,
  expr = errbar(a, b, b + ab_sd, b - ab_sd, add = T, pch =1, cap = .01, col="red", axis=FALSE))

legend('topright', legend=c("Mean 750", "Mean 590"), text.col=c("black", "red"))

但是,我想知道是否有一种方法可以遍历数据并根据数据创建这些图像。

【问题讨论】:

    标签: r plot


    【解决方案1】:
    dat <- read.table(header = TRUE, text="Plate_ID    Day    Name           590_Mean    590_SD    750_Mean    750_SD
    'MCBA15 001'  0      Cyclodextrin   0.217       0.012     0.161       0.012
    'MCBA15 001'  1      Cyclodextrin   0.257       0.012     0.171       0.012
    'MCBA15 001'  2      Cyclodextrin   0.257       0.012     0.171       0.012
    'MCBA15 001'  3      Cyclodextrin   0.217       0.012     0.161       0.012
    'MCBA15 001'  5      Cyclodextrin   0.257       0.012     0.171       0.012
    'MCBA15 001'  7      Cyclodextrin   0.257       0.012     0.171       0.012
    'MCBA15 001'  10     Cyclodextrin   0.217       0.012     0.161       0.012
    'MCBA15 005'  0      Cyclodextrin   0.217       0.012     0.161       0.012
    'MCBA15 005'  1      Cyclodextrin   0.257       0.012     0.171       0.012
    'MCBA15 005'  2      Cyclodextrin   0.257       0.012     0.171       0.012
    'MCBA15 005'  3      Cyclodextrin   0.217       0.012     0.161       0.012
    'MCBA15 005'  5      Cyclodextrin   0.257       0.012     0.171       0.012
    'MCBA15 005'  7      Cyclodextrin   0.217       0.012     0.161       0.012
    'MCBA15 005'  10     Cyclodextrin   0.217       0.012     0.161       0.012
    'MCBA15 001'  0      Lactose        0.217       0.012     0.161       0.012
    'MCBA15 001'  1      Lactose        0.257       0.012     0.171       0.012
    'MCBA15 001'  2      Lactose        0.217       0.012     0.161       0.012
    'MCBA15 001'  3      Lactose        0.217       0.012     0.161       0.012
    'MCBA15 001'  5      Lactose        0.217       0.012     0.161       0.012
    'MCBA15 001'  7      Lactose        0.217       0.012     0.161       0.012
    'MCBA15 001'  10     Lactose        0.217       0.012     0.161       0.012
    'MCBA15 005'  0      Lactose        0.217       0.012     0.161       0.012
    'MCBA15 005'  1      Lactose        0.257       0.012     0.171       0.012
    'MCBA15 005'  2      Lactose        0.257       0.012     0.171       0.012
    'MCBA15 005'  3      Lactose        0.217       0.012     0.161       0.012
    'MCBA15 005'  5      Lactose        0.257       0.012     0.171       0.012
    'MCBA15 005'  7      Lactose        0.217       0.012     0.161       0.012
    'MCBA15 005'  10     Lactose        0.217       0.012     0.161       0.012")
    

    此函数中有几处不是必需的(例如索引),但我想尽可能少地更改您的代码,以向您展示一般方法。所以这里是你的代码包装成一个函数,它接受一个数据框。

    f <- function(sortbiolog) {
      require('Hmisc')
    
      x <- sortbiolog$Day[1:7]
      y <- sortbiolog$X750_Mean[1:7]
      sd <- sortbiolog$X750_SD[1:7]
    
      plot(x, y, type = "b", ylim = c(0,.3))
      with(
        data = sortbiolog,
        expr = errbar(x, y, y + sd, y - sd, add = T, pch =1, cap = .01), main = "?-Cyclodextrin Substrate for MCBA15 001")
    
      par(new=TRUE)
    
      a <- sortbiolog$Day[1:7]
      b <- sortbiolog$X590_Mean[1:7]
      ab_sd <- sortbiolog$X590_SD[1:7]
    
      plot(a, b, type = "b", ylim = c(0,.3), ann = FALSE)
      with(
        data = sortbiolog,
        expr = errbar(a, b, b + ab_sd, b - ab_sd, add = T, pch =1, cap = .01, col="red"))
    
      legend('bottomright', legend=c("Mean 750", "Mean 590"), text.col=c("black", "red"))
    }
    

    所以你仍然可以在你的完整数据上使用它,比如f(dat[1:7, ]),但是你不是用索引来愚弄,而是根据Plate_IDName将原始数据框拆分为一个数据框列表:

    sp <- split(dat, with(dat, interaction(Plate_ID, Name)))
    
    sapply(sp, dim)
    
    #      MCBA15 001.Cyclodextrin MCBA15 005.Cyclodextrin MCBA15 001.Lactose MCBA15 005.Lactose
    # [1,]                       7                       7                  7                  7
    # [2,]                       7                       7                  7                  7
    

    然后将函数应用于列表:

    pdf('~/desktop/tmp.pdf')
    par(mfrow = c(2,2))
    p <- lapply(sp, f)
    dev.off()
    

    给我

    【讨论】:

    • 谢谢!经过一些小的调整,我得到了我想要的东西。这是一个巨大的帮助!
    猜你喜欢
    • 2023-03-13
    • 2013-12-25
    • 2019-12-07
    • 1970-01-01
    • 1970-01-01
    • 2020-07-01
    • 2018-11-17
    • 2019-04-15
    • 1970-01-01
    相关资源
    最近更新 更多