如何使用polygon()在概率密度曲线下方着色

时间:2016-05-26 22:59:53

标签: r plot

我无法让polygon()分布在下方分布一直到x轴。它似乎高于指数分布到y=-x线。这是我到目前为止所处的位置:

x <- seq(0,50,0.01)
y <- dexp(seq(0,50,0.01),rate=0.11)
plot(x, y, type="l", col=col2rgb("yellow",0.5), xaxs="i", yaxs="i", ylim=c(0,0.15))
polygon(x, y ,border=NA,col=col2rgb("yellow",0.5))

bad shade

非常感谢!

1 个答案:

答案 0 :(得分:8)

解决方案很简单,只需x <- seq(0,50,0.01) y <- dexp(seq(0,50,0.01),rate=0.11) plot(x, y, type="l", col=col2rgb("yellow",0.5), xaxs="i", yaxs="i", ylim=c(0,0.15)) polygon(c(0, x), c(0, y), border=NA, col=col2rgb("yellow",0.5)) 添加到多边形的顶点即可。见下文:

polygon()

shade

polygon()如何运作

(0, 0)按顺序排列所有顶点。原始代码的问题是原点x0 <- c(0, 0.5, 1.5) y0 <- c(1.5, 0.5, 0) ## triangle, with three vertices plot(x0, y0, pch = ".") polygon(x0, y0, col = "red", border = NA) ## area under triangle, four vertices polygon(c(0, x0), c(0, y0), col = "yellow", border = NA) 不是顶点之一,因此它不是多边形的一部分。您还可以考虑以下玩具示例:

git update-index --asume-unchanged <filenameHere>

toy example