如何对R中的打印点进行颜色编码

如何对R中的打印点进行颜色编码,r,plot,R,Plot,我有一个简单的data.frame,如下所示: status age score 0 34 90 0 56 70 1 44 69 0 53 88 1 54 44 然后我把分数和年龄作了对比 plot(age, score) 我的问题是,如何根据点的状态对点进行颜色编码 DF <- data.frame( status=c

我有一个简单的
data.frame
,如下所示:

    status   age   score
    0        34    90
    0        56    70
    1        44    69 
    0        53    88
    1        54    44
然后我把分数和年龄作了对比

   plot(age, score)
我的问题是,如何根据点的状态对点进行颜色编码

DF <- data.frame(
  status=c(0,0,1,0,1),
  age=c(34,56,44,53,54),
  score=c(90,70,69,88,44)
)
##
with(DF, plot(age,score,col=(1+status)))


我非常喜欢基础绘图。我只需要使用ifelse()创建一个颜色向量,然后将其提供给plot()

此方法允许您最大限度地自定义颜色映射

status <- c(0,0,1,0,1)
colvec <- ifelse( status == 1, "red", "blue")
age <- c(seq(1,length.out=5, by=1))
score <- c(90, 70, 69, 88, 44)

plot(age, score, col=colvec, pch=19)

status我非常喜欢基本绘图。我只需要使用ifelse()创建一个颜色向量,然后将其提供给plot()

此方法允许您最大限度地自定义颜色映射

status <- c(0,0,1,0,1)
colvec <- ifelse( status == 1, "red", "blue")
age <- c(seq(1,length.out=5, by=1))
score <- c(90, 70, 69, 88, 44)

plot(age, score, col=colvec, pch=19)

状态强制性
ggplot
解决方案:

gg <- ggplot(dat, aes(x=age, y=score, color=factor(status)))
gg <- gg + geom_point(size=3)
gg <- gg + theme_bw()
gg

gg强制性
ggplot
解决方案:

gg <- ggplot(dat, aes(x=age, y=score, color=factor(status)))
gg <- gg + geom_point(size=3)
gg <- gg + theme_bw()
gg

gg同样,这只适用于
status
是一个
numeric
向量,
col
接受
numeric
参数。同样,这只适用于
status
是一个
numeric
向量,
col
接受
numeric
参数。