用R绘制带置信区间的时间序列图

3

这是我在R中制作的几个不同时间序列的图表:enter image description here

我是使用一个简单的循环来制作这些图表的:

for(i in 1:ngroups){
  x[paste0("Group_",i)] = apply(x[,group == i],1,mean)
}

plot(x$Group_1,type="l",ylim=c(0,300))
for(i in 2:ngroups){
    lines(x[paste0("Group_",i)],col=i)
}

我也可以使用matplot来制作这个图。如您所见,每个组都是几个其他列的平均值。我想做的是像上面的图一样绘制系列,但另外显示贡献于该平均值的基础数据范围。例如,紫色线将被边缘为浅紫色的区域限定。在任何给定的时间索引处,紫色区域将从紫色组中的最低值延伸到最高值(或者说,5到95百分位数)。有没有一种优雅/聪明的方法来实现这一点?

我不确定我是否理解你的代码,但是你看过 ggplot2::geom_smooth() 吗? - maj
我认为geom_smooth是用于添加平均值的 - 这可能是创建上面图表版本的好方法,但我不认为它可以用于在线周围添加区间。 - Eric Brooks
我认为geom_smooth正是你所需要的,可以在这里查看最后一个图表:http://docs.ggplot2.org/0.9.3.1/geom_smooth.html - mts
你需要使用stat_summary函数来绘制置信区间图。这里有足够的信息:http://docs.ggplot2.org/current/stat_summary.html。你可能对stat_sum_df("mean_cl_normal", geom = "smooth")感兴趣,但是你也可以看到stat_summary可以处理很多任务。 - Matias Andina
@MatiasAndina 经验分位数并不等同于参数置信区间,mean_cl_boot可能更接近。 - Rorschach
2个回答

1
这里是一个使用 graphics 包(随 R 一起发行的图形库)的答案。我尝试解释了如何创建 polygon(用于生成CI)。这可以重新应用于解决你的问题,但我没有准确的数据。
# Values for noise and CI size
s.e. <- 0.25 # standard error of noise
interval <- s.e.*qnorm(0.975) # standard error * 97.5% quantile

# Values for Fake Data
x <- 1:10 # x values
y <- (x-1)*0.5 + rnorm(length(x), mean=0, sd=s.e.) # generate y values

# Main Plot
ylim <- c(min(y)-interval, max(y)+interval) # account for CI when determining ylim
plot(x, y, type="l", lwd=2, ylim=ylim) # plot x and y

# Determine the x values that will go into CI
CI.x.top <- x # x values going forward
CI.x.bot <- rev(x) # x values backwards
CI.x <- c(CI.x.top, CI.x.bot) # polygons are drawn clockwise

# Determine the Y values for CI
CI.y.top <- y+interval # top of CI
CI.y.bot <- rev(y)-interval # bottom of CI, but rev Y!
CI.y <- c(CI.y.top,CI.y.bot) # forward, then backward

# Add a polygon for the CI
CI.col <- adjustcolor("blue",alpha.f=0.25) # Pick a pretty CI color
polygon(CI.x, CI.y, col=CI.col, border=NA) # draw the polygon

# Point out path of polygon
arrows(CI.x.top[1], CI.y.top[1]+0.1, CI.x.top[3], CI.y.top[3]+0.1)
arrows(CI.x.top[5], CI.y.top[5]+0.1, CI.x.top[7], CI.y.top[7]+0.1)

arrows(CI.x.bot[1], CI.y.bot[1]-0.1, CI.x.bot[3], CI.y.bot[3]-0.1)
arrows(CI.x.bot[6], CI.y.bot[6]-0.1, CI.x.bot[8], CI.y.bot[8]-0.1)

# Add legend to explain what the arrows are
legend("topleft", legend="Arrows indicate path\nfor drawing polygon", xjust=0.5, bty="n")

这是最终结果:

enter image description here


0
我使用一些随机数据创建了一个df。
这是df:
df
   x         y
1  1 3.1667912
2  1 3.5301539
3  1 3.8497014
4  1 4.4494311
5  1 3.8306889
6  1 4.7681518
7  1 2.8516945
8  1 1.8350802
9  1 5.8163498
10 1 4.8589443
11 2 0.3419090
12 2 2.7940851
13 2 1.9688636
14 2 1.3475315
15 2 0.9316124
16 2 1.3208475
17 2 3.0367743
18 2 3.2340156
19 2 1.8188969
20 2 2.5050162

当您使用stat_summary、mean_cl_normal和geom smooth进行绘图时

   ggplot(df,aes(x=x,y=y))+geom_point() +  
stat_summary(fun.data=mean_cl_normal, geom="smooth", colour="red")

enter image description here

正如某人所评论的那样,也许 mean_cl_boot 更好,所以我使用了它。

 ggplot(df,aes(x=x,y=y))+geom_point() +
  stat_summary(fun.data=mean_cl_boot, geom="smooth", colour="red")

enter image description here

他们确实有点不同。而且,你可以根据你的需求调整 confint 参数,以便进行一些试验。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接