Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

geom_contour_filled color scale does not correspond to breaks

Tags:

r

ggplot2

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)) )
like image 922
noname Avatar asked Oct 28 '25 17:10

noname


1 Answers

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

enter image description here

like image 159
Allan Cameron Avatar answered Oct 31 '25 06:10

Allan Cameron



Donate For Us

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