用ggplot2手动着色置信区间 [英] Shading confidence intervals manually with ggplot2

查看:2607
本文介绍了用ggplot2手动着色置信区间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我手动创建了一个带有95%置信度带的预期寿命数据集。我在时间尺度上绘制了这些曲线,但宁愿将这些乐队变成阴影而不是虚线。显示的代码:

  p1 = ggplot()
p2 = p1 + geom_line(aes(x = pl $ Time, y = pl $ menle),color =blue)
p3 = p2 + geom_line(aes(x = pl $ Time,y = pl $ menlelb),color =blue,lty =dotted)
p4 = p3 + geom_line(aes(x = pl $ Time,y = pl $ menleub),color =blue,lty =dotted)
pre>

有没有一种简单的方法来遮蔽区间而不是仅仅有线条?
如果我遗漏了一些简单的东西,我提前道歉,但是我找不到任何东西来表示这样做的简单方法。

解决方案

如果您提供了自己的数据,这将有所帮助,但我认为以下内容可以满足您的需求。

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

p>

  ##我假设lb和ub是下限/上限
pl = data.frame(Time = 0:10 ,menle = rnorm(11))
pl $ menlelb = pl $ menle -1
pl $ menleub = pl $ menle +1

然后创建该图。阴影区域使用 geom_ribbon 创建:

  ggplot(pl,aes (时间))+ 
geom_line(aes(y = menle),color =blue)+
geom_ribbon(aes(ymin = menlelb,ymax = menleub),alpha = 0.2)


I have manually created a data set of life expectancies with accompanying 95% confidence bands. I plot these over the time scale but would prefer the bands to be shaded rather than dotted lines. Code shown:

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

Is there a simple way to shade the interval rather than just have the lines?? If I'm missing something simple I apologise in advance but I cannot find anything to indicate a simple way of doing this.

解决方案

It would be helpful if you provided your own data, but I think the following does what you are after.

First, create some dummy data:

##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

Then create the plot. The shaded region is created using geom_ribbon:

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

这篇关于用ggplot2手动着色置信区间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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