I want to add a stepped gradient for the fill
aesthetic, with specific colors for specific intervals.
For instance, I’d like red between 0 and 15, blue between 15 and 18, etc.
Here is the closest I’ve (painfully) come to my goal:
library(tidyverse)
val_breaks = c(0, 15, 18, 20, 25)
col_breaks = c("red", "blue", "yellow", "purple")
#4 intervals, 4 colors
cut(mtcars$qsec, breaks=val_breaks) %>% levels()
#> [1] "(0,15]" "(15,18]" "(18,20]" "(20,25]"
mtcars %>%
ggplot(aes(x = mpg, y = wt, color = qsec)) +
geom_point(size = 3) +
scale_color_stepsn(
colors=col_breaks,
limits=c(0,25),
breaks=val_breaks,
values=scales::rescale(val_breaks)
)
Created on 2024-12-08 with reprex v2.1.1
As you can see, the colors are not really respected and mix with each other.
Is there a way?