R绘制平均值+连续数据的SD

时间:2018-08-21 14:08:26

标签: r plot graph mean

我有一些连续的数据:

  • mean_force-2700个值反映了我的依赖变量的变化 到2700 ms
  • sd_force-另外2700个值,相关变量的标准偏差 随时间变化

我正在图上绘制mean_force,如下所示:

    x=c(1:2700)
    plot(x, mean_force, ty="l", col="blue", ylim=c(-15, 15),
    ylab="force (mN)", 
    xlab='time (ms)',
    lty=1,lwd=3)

问题是,如何在下图(均值周围的阴影区域)上绘制标清图?在R中有什么简单的方法吗?

CLICK HERE TO SEE THE PIC

1 个答案:

答案 0 :(得分:1)

如果您正在寻找基本的图形解决方案,也许这将对您有用:

#create data
x<-1:100
mean_force<-0.5*x+rnorm(100)
#assume constant standard deviation across the 
sd<-5
#determine error band
psd<-mean_force+sd
nsd<-mean_force-sd

plot(x, mean_force, ty="l", col="blue", 
     ylab="force (mN)", 
     xlab='time (ms)',
     lty=1,lwd=3)
#draw boundary and fill
lines(x, psd)
lines(x, nsd)
polygon(x=c(x, rev(x)), y=c(psd, rev(nsd)), col="lightblue", density = 40, angle=90)
#redraw line on top
lines(x, mean_force, col="blue",lwd=3)

enter image description here