I would like to create a chart with ggplot2
using a conditional color so that if the data point respect a condition on another column it will be "green"
, otherwise, it will follow a predefined color palette.
The answers I've seen in SO about conditional color in ggplot2
suggest to manually indicate colors using scale_fill_manual
or scale_color_manual
here and here.
and this is not really ideal if you have many color categories to assess or just want to use a one of those nice-looking predefined color palettes from RColorBrewer
or Viridis
.
Here is my reproducible example with my attempt:
library(ggplot2)
library(RColorBrewer)
# get data
data("mtcars")
ggplot(mtcars, aes(x=qsec, y=hp, color= ifelse( cyl < 6, "green", factor(carb)) )) +
scale_colour_brewer( type = "seq", palette = "Reds") +
geom_point() +
theme_bw()