简体   繁体   中英

ggplot: gradient scale to diverge on specific break

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))

在此处输入图片说明

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

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:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM