如何使用GGPLOT2曲线树荫下的区域(How to shade a region under a

2019-06-18 04:46发布

我一直在尝试使用GGPLOT2以产生类似于该R图文一个情节:

xv<-seq(0,4,0.01)
yv<-dnorm(xv,2,0.5) 
plot(xv,yv,type="l") 
polygon(c(xv[xv<=1.5],1.5),c(yv[xv<=1.5],yv[xv==0]),col="grey") 

这是据我已经与GGPLOT2得到:

x<-seq(0.0,0.1699,0.0001)   
ytop<-dnorm(0.12,0.08,0.02)
MyDF<-data.frame(x=x,y=dnorm(x,0.08,0.02))
p<-qplot(x=MyDF$x,y=MyDF$y,geom="line") 
p+geom_segment(aes(x=0.12,y=0,xend=0.12,yend=ytop))

我想树荫尾部区域超出X = 0.12。 我将如何做到这一点使用ggplot或qplot?

概括地说,一个人如何灯罩的任意子集的曲线下,无论是尾,或之间的任意两个线划分的区域为不同的区域?

感谢您的任何意见。

Answer 1:

创建一个区域的多边形要遮荫

#First subst the data and add the coordinates to make it shade to y = 0
shade <- rbind(c(0.12,0), subset(MyDF, x > 0.12), c(MyDF[nrow(MyDF), "X"], 0))

#Then use this new data.frame with geom_polygon
 p + geom_segment(aes(x=0.12,y=0,xend=0.12,yend=ytop)) +
     geom_polygon(data = shade, aes(x, y))



文章来源: How to shade a region under a curve using ggplot2