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 シグマのしきい値 (青い線) を超えるときはいつでも、その上または下で、線は黒とは異なる色になります。
私はこれを試しましたが、うまくいきませんでした:
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")
他に方法はありますか?