How to fix 'continuous value supplied to discrete scale' in with scale_color_brewer
Asked Answered
P

2

9

I am attempting to make a map of global data anomaly (precip, temp etc) and need to use a diverging color palette. I have code that runs well with scale_color_viridis but it does not easily convert with scale_color_brewer. I would like to use the RdBu diverging palette.

I have tried supplying the values vars within the function scale_color_brewer and using scale_fill brewer but I would like to continue plotting using the geom_points I have so far.

  titletext <- paste(VAR, "Response")
  expand.grid(lon, lat) %>%
    rename(lon = Var1, lat = Var2) %>%
    mutate(lon = ifelse(lon > 180, -(360 - lon), lon),
           vars = as.vector(global.array)) %>% 
    ggplot() + 
    geom_point(aes(x = lon, y = lat, color = vars),
               size = 2.0) + 
    borders("world", colour="black", fill=NA) + 
    scale_color_brewer(type="div", palette="RdBu")+
    #scale_color_viridis(na.value="white",name = VAR) +  
    theme(legend.direction="vertical", legend.position="right", 
          legend.key.width=unit(0.4,"cm"), legend.key.heigh=unit(2,"cm")) + 
    coord_quickmap() + ggtitle(titletext) 

Error message is

Error: Continuous value supplied to discrete scale

and

Warning messages: 1: In RColorBrewer::brewer.pal(n, pal) : n too large, allowed maximum for palette RdBu is 11 Returning the palette you asked for with that many colors

Percent answered 19/7, 2019 at 20:15 Comment(0)
F
8

You can bin/coarse-grain the continuous values to give them a discrete-ordered value to map to the discrete 8 to 12 colors included in your Brewer palette of choice.

But I think the solution you are looking for is to "distill" them with the scale_color_distiller() or scale_fill_distiller() functions already included in ggplot2, effectively converting them to a continuous color scale. Documentation and examples are provided in some canonical locations:

Firebird answered 3/5, 2021 at 20:56 Comment(1)
Yep, scale_*_distiller() is what you want.Bergama
E
-2

RdBu is a discrete color palette, but you are trying to use it for continuous data.

Please see this documentation and use scale_colour_gradient() instead.

Ermelindaermengarde answered 19/7, 2019 at 20:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.