如果“ y”的值小于零,则必须使图中点的颜色为红色,否则为绿色。但是像这样所有的点都得到相同的颜色。我尝试了以下方法:
x <- c(rnorm(100))
y <- c(rnorm(100))
x <- sort(x, decreasing = FALSE)
y <- sort(y, decreasing = FALSE)
plot(x,y)
for(i in y){if(i<0){points(x,y, col="red")} else {points(x,y, col="green")}}
但它们都是绿色的:
您可以ifelse
用来传入颜色向量(每个点一个):
set.seed(1492) # makes the random number generation reproducible
x <- c(rnorm(100))
y <- c(rnorm(100))
x <- sort(x, decreasing = FALSE)
y <- sort(y, decreasing = FALSE)
plot(x, y, col=ifelse(y<0, "red", "green"))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句