I would like to plot contour fills with colour based on a range of breaks that is larger than the range of the data, so that different plots have the same scale. In the following example, blue and red are expected to correspond to more negative or positive values, respectively. However, when the plot does not contain the full range, geom_contour_filled recognizes the breaks but does not match the colour scale. So, positive values are all blue.
library(ggplot2)
grid <- expand.grid(x=0:10,y=0:10)
grid$z <- with(grid, x*y) # 0 to 100 does not work as expected
# grid$z <- with(grid, 2*x*y-100) # -100 to 100 works as expected
ggplot(grid,aes(x=x,y=y,z=z)) +
scale_colour_manual( aesthetics = 'fill',
values = colorRampPalette(c('blue','white','red'))(20) ) +
geom_contour_filled( breaks=floor(seq(-100,100, length.out=20)) )
You need to add drop = FALSE to your color scale:
ggplot(grid,aes(x=x,y=y,z=z)) +
scale_colour_manual( aesthetics = 'fill', drop = FALSE,
values = colorRampPalette(c('blue','white','red'))(20) ) +
geom_contour_filled( breaks=floor(seq(-100,100, length.out=20)) )

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With