繁体   English   中英

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

我将不胜感激有关出了什么问题的任何反馈/指示。 我以前也尝试过类似的东西:

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]

但这有点棘手。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM