How to color points in a different color if a data attribute is not null

限于喜欢 提交于 2019-12-08 06:52:58

问题


I have a scatter plot in R (with ggplot2). The data has a numeric column (let's call it bin) which can contain various integer values or null.

I would like to colour the points with non-null bin values differently from the others. I do not want to one colour per value of bin, that would be too noisy. Just simply, say, red for those with a non-null bin and black for the others.

qplot has a colour attribute, but I don't know how to express a condition like colour = bin != null ? "red" : "black"


回答1:


You could define the color first:

color <- rep("black", length(bin))
color[is.null(color)] <- "red"

Otherwise you can use an ifelse statement:

colour=ifelse(is.null(bin), "red", "black")


来源:https://stackoverflow.com/questions/2376034/how-to-color-points-in-a-different-color-if-a-data-attribute-is-not-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!