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:
- https://ggplot2.tidyverse.org/reference/scale_brewer.html
- https://ggplot2-book.org/scale-colour.html#colour-continuous
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:
- https://ggplot2.tidyverse.org/reference/scale_brewer.html
- https://ggplot2-book.org/scale-colour.html#colour-continuous
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.
I am playing around with this library. I had some questions about diffferent palettes. Let's take the first color set in the sequential palette
For example, I use brewer.pal(9,"YlOrRd") I get the following colors [1] "#FFFFCC" "#FFEDA0" "#FED976" "#FEB24C" "#FD8D3C" "#FC4E2A" "#E31A1C" [8] "#BD0026" "#800026"
But when I want to use the first five colors of the set, brewer.pal(5,"YlOrRd"), I get the following output [1] "#FFFFB2" "#FECC5C" "#FD8D3C" "#F03B20" "#BD0026"
Why I am I getting different colors in the same set, just by changing the number of colors I want?
Also when I notice in brewer.pal(5,"YlOrRd"),some of the colors like "#F03B20" are not even there in the brewer.pal(9,"YlOrRd"). What exactly is this doing?
Using Cynthia Brewer's color brewer website might provide some insight: http://colorbrewer2.org/#type=sequential&scheme=YlOrRd&n=6
You'll notice when you change the number of classes, it spreads out the distribution of the color ramp to be more gradual so each class still has a unique hex/RGB etc for the ramp.
ColorBrewer was primarily developed for GIS and cartography so I know when I use color brewer ramps for my maps in ArcGIS, I'm supposed to use the 5-class version of the palette for maps w/ 5 classes and so on. If I change my map classification to have 4 classes but leave the 5-class ramp on, it won't display correctly. Think about your data classification vs your palette choice. It might have to do with that.
The easy way to do it is to select your palette once and maximally: my_pal = brew.pal(9, "YlOrRd"), and then if you want to use the first 5 colors of that you do my_pal[1:5]. Less efficient (marginally) but still workable is to use brew.pal(9, "YlOrRd")[1:5].



