Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot: gradient scale to diverge on specific break

Tags:

r

colors

ggplot2

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.

like image 474
CptNemo Avatar asked Jun 04 '15 08:06

CptNemo


1 Answers

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

like image 124
Jaap Avatar answered Sep 28 '22 06:09

Jaap