I'm trying to set a colour scale for a ggplot dotplot, demarking zones that are: unsuitable, suitable, ideal, suitable, unsuitable. Within your standard ggplot code wrapper, I have tried gradient2
scale_colour_gradient2(low = "grey",
mid = "red",
high = "grey",
midpoint = 25) +
and gradientn;
scale_colour_gradientn(colours = rev(rainbow(5)),
breaks = seq(0, 30, by = 5),
limits = c(0, 30),
labels = as.character(seq(0, 30, by = 5))) +
stepsn,
scale_colour_stepsn(colours = c("grey", "blue", "red", "blue", "grey"),
values = scales::rescale(c(0, 22, 25, 26, 29, 40))) +
and binned
scale_colour_binned(type = "viridis",
breaks = c(22, 25, 26, 29),
limits = c(0, 40),
guide = guide_coloursteps(even.steps = FALSE,
show.limits = TRUE)) +
binned is the closest but I can't work out how to specify the 5 colours I want (i.e., not viridis). The help option for 'type' that I presumably want is:
A function that returns a continuous colour scale
But I can't mentally square how/why I'd need to create a function that creates a continuous scale of colours, given I simply want to say (e.g.)
c("red", "yellow", "green", "yellow", "red")
I get the sense that either I'm close and am just misunderstanding a step somewhere (maybe multiple places), or I'm using the wrong approach, and it's none of these 3 functions.
Any chance anyone can advise?
Thanks all!
Edit: update following Will's suggestions: Leaving the values in the geom_point call untouched, i.e.
geom_point(aes(colour = MeanETemp24hU2M), size = 0.5) +
(rather than rescaling them to 0:1), the following code:
scale_colour_stepsn(colours = c("red", "yellow", "green", "yellow", "red"),
limits = c(0, 40),
guide = guide_coloursteps(even.steps = FALSE,
show.limits = TRUE),
breaks = c(0, 22, 25, 26, 29, 40)) +
Produces:
Which is close but
A. the colours vector doesn't match the breaks bins
B. the colours themselves are blends rather than being the true R colours. Using a modification of Will's example:
x <- 1:100
y <- runif(100) * 100
tibble(x, y) %>%
ggplot() +
aes(x = x, y = y, color = y) +
geom_point() +
scale_color_stepsn(
colours = c("red", "yellow", "green", "yellow", "red"),
breaks = c(0, 0.2, 0.4, 0.6, 0.8, 1) * 100)
Produces:
The values are right and align to the colour bins properly (IDK why mine don't given the same formulation), but the colours are diluted blends - compare the red in that image to the red at the end of the rainbow in the second image.






