如何使用多边形()在概率密度曲线下方着色 [英] How to use polygon() to shade below a probability density curve
问题描述
我无法让 polygon()
遮蔽 下方 一直到 x 轴的分布.它似乎在指数分布上方遮蔽到 y=-x
行.这是我到目前为止所拥有的:
I'm having trouble getting polygon()
to shade below the distribution all the way to the x-axis. It seems to shade above the exponential distribution to a y=-x
line. Here is where what I have so far:
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))
非常感谢!
推荐答案
解决方案很简单,通过将 (0,0)
添加到多边形的顶点.见下文:
Solution is simple, by adding (0,0)
to the vertices of the polygon. See below:
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()
的工作原理
How polygon()
works
polygon()
将按顺序排列所有顶点.您的原始代码的问题是原点 (0, 0)
不是顶点之一,因此它不会成为多边形的一部分.您还可以考虑以下玩具示例:
polygon()
will line up all vertices in order. The problem of your original code is that the origin (0, 0)
is not one of the vertices, so it will not be part of the polygon. You can also consider the following toy example:
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)
这篇关于如何使用多边形()在概率密度曲线下方着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!