【问题标题】:plot 2D colored based on column value根据列值绘制二维颜色
【发布时间】:2014-06-21 00:30:16
【问题描述】:

运行一个简单的绘图,尝试根据一列的条件绘制两列二维数据。

Table <- read.csv("Del.csv",stringsAsFactors=FALSE)
plot(DEL$PAST, DEL$ACTV, col=ifelse(is.na(DEL$PAST), 'blue', 'red'))
dput(DEL, "foo")

我明白了

PAST = c(-3.68, NA, NA, 74.67, 147, 
233.47, 371.26, NA, NA, NA, 72.1, 72.1, NA, NA, NA, NA, NA, ...

那么为什么剧情全是红色的呢?

【问题讨论】:

  • NAs 不会被绘制。你会把积分放在哪里??
  • 果然我尝试将NA改为0但失败了。这确实是问题所在。

标签: r


【解决方案1】:

正如评论中指出的那样,NA 没有阴谋。

set.seed(1)    # for reproducible example
DEL <- data.frame(PAST=sample(c(rep(NA,10),1:10),10, replace=T),
                  ACTV=sample(1:10,10))

par(mfrow=c(1,2))
# only get non-NA points
with(DEL, plot(PAST,ACTV, main="NA does not plot"))
# can set NA's to 0 - will plot on y-axis
DEL$PAST <- ifelse(is.na(DEL$PAST),0,DEL$PAST)
with(DEL, plot(PAST,ACTV, col=ifelse(PAST==0,"blue","red"), main="NAs set to 0"))

【讨论】:

    猜你喜欢
    • 2011-04-05
    • 1970-01-01
    • 2022-12-11
    • 1970-01-01
    • 2019-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-04
    相关资源
    最近更新 更多