In the plot below, how do I show all cty >= 30
values in color green and cty < 30
values in red? Thanks!
library(ggplot2)
data(mpg)
p <- ggplot()
geom_point(data = mpg, aes(displ, manufacturer, size = cty), alpha = 0.2)
scale_size_continuous(breaks = c(30, 20, 10))
scale_color_manual(values = c("green", "red", "red")) # does not work
p
CodePudding user response:
I don't think there are any cty
values greater than 30 in the dataset, but since it is only an example, let's make the cutoff 15:
ggplot()
geom_point(data = mpg, aes(displ, manufacturer, size = cty,
color = cty <= 15), alpha = 0.2)
scale_size_continuous(breaks = c(30, 20, 10))
scale_color_manual(values = c("green", "red"))
If you want the legends merged, you would need a continuous color scale that emulated a discrete one, so something like:
ggplot()
geom_point(data = mpg, aes(displ, manufacturer, size = cty,
color = cty), alpha = 0.2)
scale_size_continuous(breaks = c(30, 20, 10))
scale_color_gradientn(colors = c("red", 'red', "green", 'green'),
values = c(0, 0.5, 0.50001, 1),
breaks = c(30, 20, 10),
guide = guide_legend())