Tim*_*son 22 statistics graphics r ggplot2
我一直在尝试使用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来做到这一点?
从广义上讲,如何对曲线下的任何子集进行着色,无论是尾部,还是在将该区域划分为不同区域的两条任意线之间?
谢谢你的建议.
Luc*_*zer 16
使用要着色的区域创建多边形
#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))

| 归档时间: | 
 | 
| 查看次数: | 15215 次 | 
| 最近记录: |