[英]Multiple colours for data points with scatterplot in R
所以我的任务是基本上得到下图。 数据来自数据框,其中某些值已标记为 NA。 因此,我的代码如下:
> plot(temp,ozone,
+ xlab = "temperature",
+ ylab = "ozone",
+ col = ifelse(which(ozone>100), "red", "orange",),
+ pch = 17)
但是我现在收到一个错误:
"Error in ifelse(which(ozone > 100), "red", "orange", ) :
unused argument (alist())
我将不胜感激有关出了什么问题的任何反馈/指示。 我以前也尝试过类似的东西:
highlevels <- which(ozone>100)
lowlevels <- which(ozone<100)
col = c("red","orange")[highlevels,lowlevels]
省略which
并将其包装在另一个ifelse
是一种选择:
plot(temp, ozone,
xlab = "temperature",
ylab = "ozone",
col = ifelse(ifelse(!is.na(ozone), ozone, 0) > 100, "red", "orange"),
pch = 17)
否则,如果您使用这些库,像tidyr::replace_na
或dplyr::coalesce
这样的函数可以帮助您。
另一种选择可能是将所有变量子集到那些非缺失的变量中。
另一个正在设置
col = c("red", "orange")[(ozone > 100) + 1]
但这有点棘手。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.