这本质上是卢西亚诺答案的副本,我发现它很有用,但是它可能会为其他想要使用这种方法的人节省一些时间。
创建数据。这里是具有指定均值和 sd 的正态分布的第 0.1 个百分位到第 99.9 个百分位的 0.001 个间隔的密度。
mean_ = 10
sd_ = 4
x = seq(qnorm(c(0.001), mean_, sd_),qnorm(c(0.999), mean_, sd_),0.001)
distdata = data.frame(x=x,y=dnorm(x,mean_,sd_))
用于根据特定值对左尾或右尾进行着色的函数。
shade_under_curve = function(p, d, left=NULL, right=NULL, distrib, fill, ...){
if(!is.null(left)){
shade = rbind(c(d[1, "x"], 0), d[d$x<left,], c(left,0))
} else if(!is.null(right)){
shade = rbind(c(right,0), d[d$x>right,], c(d[nrow(d), "x"], 0))
}
value = c(left, right)
ytop<-distrib(value,...)
p + geom_segment(aes(x=value,y=0,xend=value,yend=ytop)) +
geom_polygon(data = shade, aes(x, y), alpha=0.2, fill=fill)
}
例子:
p = qplot(x=distdata$x,y=distdata$y,geom="line")
shade_under_curve(p, distdata, left=3, distrib=dnorm, mean=mean_, sd=sd_, fill = "red")
shade_under_curve(p, distdata, right=15, distrib=dnorm, mean=mean_, sd=sd_, fill = "blue")
p2 = shade_under_curve(p, distdata, left=qnorm(0.025, mean_, sd_), distrib=dnorm, mean=mean_, sd=sd_, fill = "green")
shade_under_curve(p2, distdata, right=qnorm(0.975, mean_, sd_), distrib=dnorm, mean=mean_, sd=sd_, fill = "green")