【问题标题】:Colors of points in a base plot conditioned by values in another column基础图中点的颜色取决于另一列中的值
【发布时间】:2022-01-17 08:54:13
【问题描述】:

我有一个图,其中圆圈的颜色 (pch=19) 目前要么是红色(当条件列中的值大于 4.5 时),要么是黑色(如果缺少值)。

points(bb$Year,bb$Tot,col=ifelse(bb$rate<4.5|is.na(bb$rate),'black','red'),pch=19,cex=1.7)

当值为

可能很简单,但对我来说有点棘手。

【问题讨论】:

  • 基图是必需的吗?
  • make this question reproducible 包含纯文本格式的代表性数据集和所有相关代码。该图表中发生了很多事情,这些事情并非仅使用 points() 生成的。

标签: r plot


【解决方案1】:

您需要为符号和颜色创建一个索引值:

x <- 0:10
y <- (5 - x)^2
z <- 0:10
z[c(3, 9)] <- NA    # Conditioning variable
sym <- c(1, 16, 16)
clr <- c("black", "black", "red")
idx <- ifelse(is.na(z), 1, ifelse(z <= 4.5, 2, 3))
plot(x, y, pch=sym[idx], col=clr[idx])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 2013-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    相关资源
    最近更新 更多