R 中具有散点图的数据点的多种颜色

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())

如有任何 feedback/pointers 问题,我将不胜感激。 我以前也尝试过类似的东西:

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_nadplyr::coalesce 等函数可以为您提供帮助。

另一种选择是将所有变量子集化为非缺失变量。

还有一个正在设置

col =  c("red", "orange")[(ozone > 100) + 1]

但这有点棘手。