在R中用散点图对数据点进行多种颜色的处理。

问题描述 投票:0回答:1

所以我的任务是基本上要得到下面的图。数据来自一个数据框,其中有一些值被标记为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())

如果有任何反馈意见,我将感激不尽 I would appreciate any feedbackpointers on what has gone wrong. 我之前也试过这样的方法。

highlevels <- which(ozone>100)
lowlevels <- which(ozone<100)
col = c("red","orange")[highlevels,lowlevels]

但是很明显这并不奏效... ...Goal

r scatter-plot
1个回答
3
投票

删去 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]

但这是有点棘手。

© www.soinside.com 2019 - 2024. All rights reserved.