我想填补一个阶跃函数行我使用ploted下的面积plot(X, type='s')我试过polygon没有成功.
set.seed(1);y = abs(rnorm(10))
plot(y,ylim=c(0,max(y)), type="p",pch=24)
lines(y, type='s')
abline(h=0)
Run Code Online (Sandbox Code Playgroud)
假设我想在曲线下面和上面画灰色 y=0

Hon*_*Ooi 11
x <- seq_along(y)
y2 <- rep(y, each=2)
y2 <- y2[-length(y2)]
x2 <- rep(x, each=2)[-1]
x3 <- c(min(x2), x2, max(x2))
y3 <- c(0, y2, 0)
# because polygon() is dumb and wants a pre-existing plot
plot(x, y, ylim=c(0, max(y)), type="n")
polygon(x3, y3, border=NA, col="grey")
lines(x2, y2)
Run Code Online (Sandbox Code Playgroud)