I want to build a ggplot with a color gradient containing an exception.
For instance, say that I want the gradient to go from blue
to black
, but with the special value 21
being red
.
This can be done using 2 geoms with data filtering:
library(tidyverse)
mtcars %>%
ggplot(aes(x = qsec, y = wt, color = mpg)) +
geom_point(size = 3, data=~filter(.x, mpg==21), color="red") +
geom_point(size = 3, data=~filter(.x, mpg>21)) +
scale_color_gradient(low="blue", high="black",
transform = "log")
Created on 2024-12-08 with reprex v2.1.1
However, using this technique has annoying side effects, one being that the legend doesn’t contain the red value.
Is there a way to achieve the same result with one standard geom?