如何用R中的数据框绘制具有平均值和标准偏差的单个数据点 [英] How to plot a single datapoint with mean and standard deviation from a data frame in R

查看:259
本文介绍了如何用R中的数据框绘制具有平均值和标准偏差的单个数据点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 SubjIDHRIBI激励 状态
S175.98 790 1 1
S175.95 791 1 2
S165.7 918 1 3
S159.63 100 1 4
S159.44 101 1 5
S159.62 101 2 1
S163.85 943 2 2
S160.75 992 2 3
S159.62 101 2 4
S161.68 974 2 5
S265.21 921 1 1
S259.23 101 1 2
S261.23 979 1 3
S270.8 849 1 4
S274.21 809 1 4

我想绘制状态列中每个值的HR列的平均值。



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

  numberOfSeconds < -  8; 

为(1:40中的stimNumber){

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 $状态,xlab =,ylab =)

}

因此获得类似于以下内容的情节:


$ b $我每个刺激都有一个情节。在每个图的X轴上,我有状态列,对于每个SubjID,我有一个HR值。几乎在那里...



然而,我想要最终获得的是每个X值的单个Y数据点。即Y应为平均值(HR列的平均值),类似于以下图:





如何实现?在每个数据点中,标准偏差也显示为误差条,这是非常好的。



提前感谢您的帮助。

解决方案

最简单的方法是 tapply()。如果您的 data.frame 数据

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

很容易计算和绘制错误栏:

 code> serr<  -  with(data,tapply(HR,Status,function(x)sd(x)/ sqrt(length(x))))
plot(means,type =o (i)+ serr [i],表示[i(i,i,i,..., i] -serr [i])))


I have a large dataframe in R with this format:

"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

I would like to plot the mean of the "HR" column for each one of the values of the status column.

I wrote the following R code where I create a subset of the data (by different values of "Status") and plot it:

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

    }

Thus obtaining a plot similar to the following:

I have one plot per each "Stimulus". On the X axis of each plot I have the "Status" column, on the Y I have one "HR" value for each "SubjID". Almost there...

However what I would like to obtain ultimately is a single Y datapoint per each X value. i.e. Y should be the mean value (mean of HR column), similar to the following plot:

How can this be achieved? It would be great having also the standard deviation shown as error bars in each datapoint.

Thanks in advance for your help.

解决方案

The simplest way to do it would be tapply(). If your data.frame is data:

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

It is easy to calculate and plot the error bars as well:

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

这篇关于如何用R中的数据框绘制具有平均值和标准偏差的单个数据点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆