【问题标题】:How to plot a single datapoint with mean and standard deviation from a data frame in R如何在R中的数据框中绘制具有平均值和标准偏差的单个数据点
【发布时间】:2013-03-12 11:49:48
【问题描述】:

我在 R 中有一个大数据框,格式如下:

"SubjID"    "HR"    "IBI"   "Stimulus"  "Status"
"S1"    75.98   790 1   1
"S1"    75.95   791 1   2
"S1"    65.7    918 1   3
"S1"    59.63   100 1   4
"S1"    59.44   101 1   5
"S1"    59.62   101 2   1
"S1"    63.85   943 2   2
"S1"    60.75   992 2   3
"S1"    59.62   101 2   4
"S1"    61.68   974 2   5
"S2"    65.21   921 1   1
"S2"    59.23   101 1   2
"S2"    61.23   979 1   3
"S2"    70.8    849 1   4
"S2"    74.21   809 1   4

我想为状态列的每个值绘制“HR”列的平均值。

我编写了以下 R 代码,在其中创建数据子集(通过“状态”的不同值)并绘制它:

numberOfSeconds <- 8;

    for(stimNumber in 1:40) {

    stimulus2plot <- subset(resampledDataFile, Stimulus == stimNumber & Status <= numberOfSeconds, select=c(SubjID, HR, IBI, Stimulus, Status))

    plot(stimulus2plot$HR~stimulus2plot$Status, xlab="",ylab="")
    lines(stimulus2plot$HR~stimulus2plot$Status, xlab="",ylab="")

    }

从而得到类似如下的情节:

每个“刺激”都有一个情节。在每个图的 X 轴上,我有“状态”列,在 Y 轴上,每个“SubjID”都有一个“HR”值。差不多了……

但是,我最终想要获得的是每个 X 值都有一个 Y 数据点。即 Y 应该是平均值(HR 列的平均值),类似于下图:

如何做到这一点?如果每个数据点中的标准偏差也显示为误差线,那就太好了。

提前感谢您的帮助。

【问题讨论】:

    标签: r plot dataframe


    【解决方案1】:

    最简单的方法是tapply()。如果你的data.framedata

    means <- with(data, tapply(HR, Status, mean))
    plot(means, type="l")
    

    计算和绘制误差线也很容易:

    serr <- with(data, tapply(HR, Status, function(x)sd(x)/sqrt(length(x))))
    plot(means, type="o", ylim=c(50,80))
    sapply(1:length(serr), function(i) lines(rep(i,2), c(means[i]+serr[i], means[i]-serr[i])))
    

    【讨论】:

    • 感谢第一部分完美运行。但是,如果我将代码的第二部分用于误差线,则绘图会变得更加“平坦”(线条几乎是水平的),而您可以看到它们的陡度不同
    • @Albz 您只需要调整ylim 参数,它指定y 轴的下限和上限。我将其设置为c(50,80),以便误差线可以整齐地融入情节,但您可以根据自己的喜好进行调整。见?plot.default
    【解决方案2】:

    您可以做的最简单的事情是首先预先计算值,然后绘制它们。我会使用ddply 进行这种分析:

    library(plyr)
    res = ddply(df, .(Status), summarise, mn = mean(HR))
    

    并使用 ggplot2 绘制它:

    ggplot(res, aes(x = Status, y = mn)) + geom_line() + geom_point()
    

    【讨论】:

    • 编辑我的答案以反映这个错误。
    • 感谢您的提醒,我会在几分钟内删除我的 cmets,如果您这样做,那么除了我的编辑之外,将不存在此错误的任何记录。
    【解决方案3】:

    为了让它最接近你想要的:

    library(ggplot2)
    library(plyr)
    df.summary <- ddply(df, .(Stimulus, Status), summarise,
                        HR.mean = mean(HR),
                        HR.sd = sd(HR))
    ggplot(df.summary, aes(Status, HR.mean)) + geom_path() + geom_point() + 
      geom_errorbar(aes(ymin=HR.mean-HR.sd, ymax=HR.mean+HR.sd), width=0.25) +facet_wrap(~Stimulus) 
    

    【讨论】:

      【解决方案4】:

      您可以使用以下假数据示例作为指导,在 ggplot2 中完全执行此操作:

      DF <- data.frame(stimulus = factor(rep(paste("Stimulus", seq(4)), each = 40)),
                       subject = factor(rep(seq(20), each = 8)),
                       time = rep(seq(8), 20),
                       resp = rnorm(160, 50, 10))
      # spaghetti plots
      ggplot(DF, aes(x = time, y = resp, group = subject)) +
         geom_line() +
         facet_wrap(~ stimulus, ncol = 1)
      # plot of time averages by stimulus
      ggplot(DF, aes(x = time, y = resp)) +
         stat_summary(fun.y = mean, geom = "line", group = 1) +
         stat_summary(fun.y = mean, geom = "point", group = 1, shape = 1) +
         facet_wrap(~ stimulus, ncol = 1)
      

      【讨论】:

        猜你喜欢
        • 2022-07-05
        • 1970-01-01
        • 2021-04-25
        • 1970-01-01
        • 2020-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-11
        相关资源
        最近更新 更多