Is it possible to define the “mid” range in scale_fill_gradient2()?

You can try scale_fill_gradientn and the values argument. From ?scale_fill_gradientn:

if colours should not be evenly positioned along the gradient this vector gives the position (between 0 and 1) for each colour in the colours vector. See rescale for a convience function to map an arbitrary range to between 0 and 1.

Thus, resolution of the colour scale for values close to zero may be increased by using suitable numbers in values = rescale(...).

scale_fill_gradientn(colours = c("cyan", "black", "red"),
                       values = scales::rescale(c(-0.5, -0.05, 0, 0.05, 0.5)))

enter image description here

Leave a Comment