Can't apply a different color palette to geom_contour_filled

Trying to change colors of the representation on the graph, and I get an error that I don't understand.

df <- expand.grid( x = 0:10
                 , y = 0:10
                 ) %>% %>%
      bind_cols(data.frame(z = rnorm(121,0,1)))

ggplot(df, aes( x=x, y=y, z=z)) + 

ggplot(df, aes( x=x, y=y, z=z)) + 
  geom_contour_filled() +
  scale_fill_gradient(low = "blue", high = "red", na.value = NA)

The first graph appears normally. The second graph says Error: Discrete value supplied to continuous scale. scale_color_gradient throws no error but it also doesn't change the colors of the graph any.

Hi there,

your error is due to the fact that geom_contour_filled creates discrete bins, and therefore requires a discrete colour palette. But scale_fill_gradient creates a continuous palette.

You can either create your own discrete palette with scale_manual:

Or you can use a preexisting discrete scale. For your example, this code might work well:

ggplot(df, aes(x, y, z = z)) + 
  geom_contour_filled() + 
  scale_fill_brewer(palette = "Spectral")

Hope that helps.

Thanks, that solved it.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.