我在 R 中有以下数据和代码:
x <- runif(1000, -9.99, 9.99)
mx <- mean(x)
stdevs_3 <- mx + c(-3, +3) * sd(x/5) # Statndard Deviation 3-sigma
我在 R 中绘制为线(以及 3 个标准差和平均线):
plot(x, t="l", main="Plot of Data", ylab="X", xlab="")
abline(h=mx, col="red", lwd=2)
abline(h=stdevs_3, lwd=2, col="blue")
我想做的事:
在图上的任何地方,只要线超过 3 个 sigma 阈值(蓝线),在其上方或下方,线的颜色应与黑色不同。
我试过这个,但没有奏效:
plot(x, type="l", col= ifelse(x < stdevs_3[[1]],"red", "black"))
abline(h=mx, col="red", lwd=2)
abline(h=stdevs_3, lwd=2, col="blue")
还有其他方法吗?