使用ggplot2手动着色置信区间

sta*_*ver 9 r ggplot2

我手动创建了一个预期寿命数据集,伴随着95%的置信区间.我在时间尺度上绘制这些图,但更喜欢用阴影而不是虚线.代码显示:

p1 = ggplot()
p2 = p1 + geom_line(aes(x=pl$Time, y=pl$menle), colour="blue")
p3 = p2 + geom_line(aes(x=pl$Time, y=pl$menlelb), colour="blue", lty="dotted")
p4 = p3 + geom_line(aes(x=pl$Time, y=pl$menleub), colour="blue", lty="dotted")
Run Code Online (Sandbox Code Playgroud)

是否有一种简单的方法来遮蔽间隔而不仅仅是线条?如果我遗漏了一些简单的事情,我会事先道歉,但我找不到任何表明这样做的简单方法.

csg*_*pie 34

如果您提供自己的数据会很有帮助,但我认为以下内容可以满足您的需求.

首先,创建一些虚拟数据:

##I presume the lb and ub are lower/upper bound
pl = data.frame(Time = 0:10, menle = rnorm(11))
pl$menlelb = pl$menle -1
pl$menleub = pl$menle +1
Run Code Online (Sandbox Code Playgroud)

然后创建情节.阴影区域使用geom_ribbon以下方式创建:

ggplot(pl, aes(Time)) + 
  geom_line(aes(y=menle), colour="blue") + 
  geom_ribbon(aes(ymin=menlelb, ymax=menleub), alpha=0.2)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述