Search code examples
rggplot2palettecolorbrewer

Reversing order of colorbrewer palette while using statebins package


statebins( dataset ,state_col= "state", value_col="value",
      brewer_pal = "Spectral",) 

I'm using statebins package to plot energy consumption of 50 states of US. Though Spectral palette from colorbrewer look well on the plot:

this is in the exact opposite order I wanted them to be. How can I reverse the order of Spectral colors. In the ggplot I used to use brewerpal reversed order as:

scale_fill_gradientn(colours = rev(brewer.pal(20,'Spectral')))

But when I am using Spectral the other way like in the first code chunk, I am not able to use the reverse order.

Details about Statebins package: https://github.com/hrbrmstr/statebins/

The function I am using here is statebins (which Creates a new ggplot-based "statebin" chart for USA states ,discrete scale)


Solution

  • This is the line of the source code for statebins() that sets the color palette in a ggplot layer:

    gg <- gg + scale_fill_brewer(palette = brewer_pal, name = legend_title)
    

    It calls scale_fill_brewer(), without allowing you to modify the direction argument. This means brewer_pal in statebins() has to be set to a valid palette argument for scale_fill_brewer(), and that's the extent of customization you're allowed. You can't pass a reversed palette, you just have to pick an existing one, either by name or number.