Reputation: 5819
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).
Upvotes: 1
Views: 55
Reputation: 1364
ggplot(mtcars, aes(wt, mpg)) +
geom_point(aes(color = cut(mpg, c(-Inf, 20, Inf))), size = 5) +
scale_color_manual(name = "mpg",
values = c("red",
"green"),
breaks = c("(-Inf,20]",
"(20, Inf]"),
labels = c("<= 20", "> 20"))
Put the intervals in the "breaks" argument.
Upvotes: 3