如何为曲线包络着色

时间:2015-11-04 21:51:44

标签: r graphics plot

我试图给曲线的包络上色。 比如说:

set.seed(1)
middle = seq(1,10,length.out=100)
upper = middle+5*runif(100)
lower = middle-5*runif(100)
plot(seq(100),middle,ylim=range(lower,middle,upper),type='l',col='red')
lines(upper,col='grey')
lines(lower,col='grey')

plot

如何在灰线之间以灰色显示颜色?

2 个答案:

答案 0 :(得分:4)

这是一种可能性,如demo(graphics)

中所述
middle <- seq(1,10,length.out=100)
upper <- middle+5*runif(100)
lower <- middle-5*runif(100)
xx <- c(1:100,100:1)
yy <- c(upper,rev(lower))
plot(seq(100),middle,ylim=range(lower,middle,upper),type='l')
polygon(xx, yy, col="gray")
lines(seq(100),middle,ylim=range(lower,middle,upper),type='l',col='red')

enter image description here

答案 1 :(得分:3)

# ... Your code before plot( ... )
sequence = seq(100)
plot(sequence,middle,ylim=range(lower,middle,upper),type='l',col='red')
# After all the other code:
polygon(c(sequence, rev(sequence)), c(upper, rev(lower)), col = rgb( 0.5, 0.5, 0.5, 0.5 ), border = NA )

Obligatory picture of output