I am wondering if it is possible to assign point colours according both x, and y values in ggplot2?
For instance, I want to assign points, which (x < 0.75 & y < 2) red; which (0.75 <= x & 2 <= y < 4) blue; and which (0.75 <= x & y >= 4) green.
I know it can be done by subsetting the data frame, and then plot them together, but I am wondering if there is a simple way.
library(ggplot2)
data("iris")
ggplot() + geom_point(data = iris, aes(x = Petal.Width,
y = Petal.Length))
myData$myCol <- ifelse(x..., "red", ifelse(...., "blue", ...)))
, then use that column within aes col = myCol, and usescale_colour_identity()
. – zx8754(2.25 <= x, y >= 4) are green.
do you mean2.25 <= x AND y >= 4
or2.25 <= x OR y >= 4
? because what you marked as green points there do not fit the first case. And the optimal way I can think of is adding a column to your data with you group specification and then plotting. – DS_UNI