这里有一种方法:
首先,我们会得到你的密度的并行最低 - 这是对一个向量顶部y
我们的多边形坐标。
y = pmin(y1, y2)
# set up your plot as in the question
plot(x, y1, type="l")
lines(x, y2)
# define a re-usable variable for the vertical line placement
x_vert = 380
abline(v = x[x_vert])
# Now we'll draw 2 polygons, one for the left side, one for the right.
# The first (x,y) pairs of the polygon are just the (x,y) coords of the
# density we're filling to, until the vertical line
# Then we need to connect the "bottom" points, which have coordinates
# (x[x_vert], 0) and (x[1], 0)
polygon(x = c(x[1:x_vert], x[x_vert], x[1]),
y = c(y[1:x_vert], 0, 0),
col = "blue")
# similar for the right hand polygon, but now going from x_vert to length(x)
polygon(x = c(x[x_vert:length(x)], x[length(x)], x[x_vert]),
y = c(y[x_vert:length(x)], 0, 0),
col = "red")
瞧!
你想填补了两个密度的最大值以下,或者你想两个密度填充不同的颜色(也许有的透明度,使你可以看到重叠)下的面积? – Gregor
@Gregor一个是垂直线右侧的部分,但低于左侧法线密度的右侧尾部,另一个是右侧法线密度左侧但低于左侧尾部的部分。对不起,措辞严重........ –
不用担心, - 所以在任何'x'点,你只想填补最低密度 - 或者说另一种方式,你想填补交叉/重叠的密度。 (垂直线任一侧的不同颜色都非常清晰。) – Gregor