ggplot2 reverse order of scale_brewer

I think you probably want to select the colors using brewer.pal directly and then use scale_colour_manual:

library(ggplot2)
library(RColorBrewer)

ggplot(mtcars,aes(x = mpg, y = disp)) + 
    geom_point(aes(colour = factor(cyl))) + 
    scale_colour_manual(values = rev(brewer.pal(3, "BuPu")))

Then you can rev the order of the colors there.

As of version 2.0,0 of ggplot there is now a more direct way to do this, see the answer by @pbaylis below.


This will not help with the OP's problem - I know. For discrete scales like scale_..._brewer(), doing scale_..._manual(values = rev(colorsYouHad)) is the right answer.

Nevertheless, for continuous scales, you can simply pass:

scale_..._...(..., trans = "reverse")

e.g., for the continuous equivalent of scale_..._brewer():

scale_..._distiller("My Scale", palette = "Spectral", trans = "reverse")


The CRAN version of ggplot2 now allows users to specify direction=-1 in scale_brewer to reverse the colors. The following produces the same plot as the accepted answer.

ggplot(mtcars,aes(x = mpg, y = disp)) + 
  geom_point(aes(colour = factor(cyl))) + 
  scale_colour_brewer(palette="BuPu", direction=-1)

Tags:

R

Ggplot2