Breaks in map in r

Hi,
If you are trying to keep a continuous scale and just show these breaks the code you have should work fine. In terms of your labels and breaks, these do not align, with the current setup anything 0-10 will be in the first bin, 10-100 in the second etc. If you want 0-0.1 to be a bin to itself you will need c(0,0.1,10,100). The top Inf break is not required. i.e.:

   scale_fill_distiller(palette ="YlGnBu", 
                     labels = c("Zero", "0.1-9.9", "10-99.9","100+"),
                      breaks = c(0, 0.1,10,100),
                       direction = 1) 

If you are trying to set fixed colours for each of these groups this is a little more complex but will give a cleaner result. You will need to bin your colour scale as well as your data. We can do this with scale_fill_stepsn(). e.g.:

  scale_fill_stepsn(colors = RColorBrewer::brewer.pal(4, "YlGnBu"),
                 breaks = c(0,0.1,10,100), 
                 labels = c("Zero", "0.1-9.9", "10-99.9","100+"), 
                 values = scales::rescale(c(0,0.1,10,100)))

To customise the brewer palette I think you still need to install the old package RColorBrewer to access the palette files (the RColorBrewer::brewer.pal() above).

You will also see that I use the “values” argument to scale the palette so it covers the full depth of available colours, if you remove this you will see that 0 and 0.1-9.9 have very similar colours as they are numerically similar.

If your data do not cover the whole range of the breaks you will get errors like this: ! `breaks` and `labels` are different lengths, you can fix this by setting limits outside of your data range:

  scale_fill_stepsn(colors = RColorBrewer::brewer.pal(4, "YlGnBu"),
                 breaks = c(0,0.1,10,100), 
                 labels = c("Zero", "0.1-9.9", "10-99.9","100+"), 
                 limits = c(0,200),
                 values = scales::rescale(c(0,0.1,10,100)))

Hope this helps,
David

If this is not the issue you are trying to fix please try and provide error messages and a reproducible example like this

2 Likes