I want to conditionally format points to be red if they're less than 20 miles per gallon, and green if they're greater than 20 mpg. This works (but not the colors I want).
ggplot(mtcars, aes(wt, mpg)) +
geom_point(aes(color = cut(mpg, c(-Inf, 20, Inf))), size = 5)
As soon as I get fancy and try to select my own color scheme things break.
ggplot(mtcars, aes(wt, mpg)) +
geom_point(aes(color = cut(mpg, c(-Inf, 20, Inf))), size = 5) +
scale_color_manual(name = "mpg",
values = c("(-Inf, 20]" = "red",
"(20, Inf]" = "green"),
labels = c("<= 20", "> 20"))
I found other examples on stackoverflow that follow the same template, but this doesn't work. I get this error and all points below 20 mpg are removed.
Warning message: Removed 18 rows containing missing values (geom_point).
values = c("(-Inf,20]" = "red",
basically eliminating one space, then everything works fine. The answer below works as well so I'll leave everything as is.