14

I have this data:

df <- data.frame(x = 1:10,
                 y = 1:10,
                 value = c(seq(from = 0.5, to = 3.2, length.out = 9), Inf))

which I want to plot with a gradient colour scale highlighting three break values:

breaks <- c(0.5,1,3.2)

I want the white colour to highlight my middle breaking value (that is 1) and then the other two to increase in intensity as they reach the two tails of the distribution. Yet this will plot

require(ggplot)
ggplot(df, aes(x,y,colour=value)) + geom_point(size=10) +
  scale_colour_gradientn(colours = c("red","white","blue"),
                         breaks = breaks, labels = format(breaks))

enter image description here

with white centered on the mean/median value (1.85). I don't understand what's the point in declaring break points then.

Jaap
  • 81,064
  • 34
  • 182
  • 193
CptNemo
  • 6,455
  • 16
  • 58
  • 107

1 Answers1

17

You could use scale_colour_gradient2 instead of scale_colour_gradientn:

ggplot(df, aes(x,y,colour=value)) + 
  geom_point(size=10) +
  scale_colour_gradient2(low = "red", mid = "white", high = "blue", midpoint = 1, breaks = breaks) +
  theme_bw()

this gives:

enter image description here

Jaap
  • 81,064
  • 34
  • 182
  • 193
  • If you had some time, would you take a look at this: https://stackoverflow.com/questions/73711073/using-a-custom-gradient-fill-based-on-two-different-columns ? Your answer got me closer but I cannot get the labels on the right break value. – M-- Sep 14 '22 at 03:44