How to make a continuous ggplot2 scale with midpoint 10, min 0 and max 20?

How to make a continuous ggplot2 scale with midpoint 10, min 0 and max 20?

I am able to get the midpoint of 10, but I wasn't sure how to do the rest. It seems like the rescaler argument and the scales::rescale functions are the way to do this??

library(tidyverse)

mpg %>% 
  mutate(centred = cty - mean(cty)) %>% #
  select(displ, hwy, centred) %>% 
  ggplot() +
  geom_point(aes(x = displ, y = hwy, col = centred)) +
  scale_color_gradientn(
    colors = c("red", "white", "blue"), 
    rescaler = ~scales::rescale_mid(.x, mid = 10),
    breaks = scales::breaks_width(4))

Created on 2023-06-18 with reprex v2.0.2

Hi @davidhodge931
Instead of rescaler try xlim(c(0,20))

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.