87

Seemingly a very simple thing to do but it took me >30min without finding answer.

How do I reverse the order of colors? By looking at documentation for scale_brewer, i figured it can be formatter= argument being suspicious. I passed 'rev' and then rev, but they have no effect (no error message, just ignored).

joran
  • 169,992
  • 32
  • 429
  • 468
yosukesabai
  • 6,184
  • 4
  • 30
  • 42
  • 6
    pbaylis's answer (set ``direction=-1``) seems to be the simplest answer to the question in the title, but joran's or Josh's give more control, e.g. passing an arbitrary sequence of colors, not just the reverse one. – PatrickT Feb 04 '18 at 19:23

6 Answers6

98

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)
pbaylis
  • 1,529
  • 11
  • 19
72

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.

Rich Pauloo
  • 7,734
  • 4
  • 37
  • 69
joran
  • 169,992
  • 32
  • 429
  • 468
  • 2
    +1 -- This is nice. It might also be good to 'hardwire' in the number of colors needed, like this: `rev(brewer.pal(n=length(unique(mtcars$cyl)), "BuPu"))`. – Josh O'Brien Jan 05 '12 at 23:37
  • ...or `scale_fill_manual` whichever paradigm is most appropriate for your geom. – Brandon Bertelsen Jan 06 '12 at 11:52
  • Feel free to update your answer with the code I posted and I'll remove mine for clarity. – pbaylis Apr 29 '16 at 19:04
  • 1
    This solution works when using `library(RColorBrewer)`. (I know OP knows this already, but I wanted to swap ggplot colors and didn't know this.) – Jeff Jun 02 '16 at 17:52
32

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")

Antoine Lizée
  • 3,743
  • 1
  • 26
  • 36
  • I appreciate the desire to alert users to a now simpler way to do the same thing, but since both answers _do_ work, an edit calling a different answer the "correct" one is inappropriate. I am quite responsive to comments on my answers. A simple comment telling me my answer is out of date is sufficient. – joran Apr 02 '16 at 23:49
  • This is a nice, simple answer, worked for me, thanks! – RobertMyles Aug 24 '16 at 15:02
  • 3
    Note that while using `trans = "reverse"` , also the order of the values changes. For continuous scales, this means that the legend values will _decrease_ upward – Lennert May 12 '17 at 13:39
  • 1
    @joran, this answer was *not* solving the OP's problem and hence was not doing 'the same thing'. This is why I submitted a different answer. – Antoine Lizée Jun 06 '17 at 16:18
  • FWIW if you are trying to plot significance values (i.e. working with probabilities) this really helps, in connection with changing the direction (which is -1 by default in the continuous case, for some reasons). E.g. `scale_fill_distiller(palette = "YlOrRd", direction = 1, na.value = "gray", trans="reverse", name="Significance")` – posdef Aug 28 '17 at 11:08
  • @Lennert did you find any way to rectify this? I'm having this exact issue right now. – Nautica Oct 21 '18 at 16:27
  • 4
    For anyone reading my query, try using `direction="horizontal"` in the scale continous function instead of `trans="reverse"`. – Nautica Oct 21 '18 at 18:02
11

If you don't want to muck around directly with RColorBrewer (a lovely package), you can reverse the levels of the factor in the original data.frame, and then plot it:

dsamp <- diamonds[sample(nrow(diamonds), 1000), ] 

# Reverse the levels of the factor associated with color, here 'clarity'
# (Might be safer to assign this to a new column named, e.g., 'clarity2')
levels(dsamp$clarity) <- rev(levels(dsamp$clarity))

d <- qplot(carat, price, data = dsamp, colour = clarity)
d + scale_colour_brewer(breaks = levels(dsamp$clarity))

And if you want to print the key in the same order as before the reversal, just do this:

d + scale_colour_brewer(breaks = rev(levels(dsamp$clarity)))
Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
4

I know, very late to the party. But I ran into this problem a while back and used the above solution. I'm currently going through r4ds by Hadley Wickham and there is a ridiculously easy solution so I thought I'd post it. Change this:

ggplot(mtcars,aes(x = mpg, y = disp)) + 
geom_point(aes(colour = factor(cyl)))

to this:

ggplot(mtcars,aes(x = mpg, y = disp)) + 
geom_point(aes(colour = factor(-cyl))) #note the minus symbol
dj20b22
  • 73
  • 6
1

I get an error saying the minus sign is not applicable for factors. I'm working with greyscale rather than brewer, perhaps that is the source of the error? For scale_color_grey, I found reversing the start and end points from the default worked.

# lighter shade for first category (default)
+ scale_color_grey(start = 0.2, end = 0.8) 

# darker shade for first category 
+ scale_color_grey(start = 0.8, end = 0.2)
SophistM
  • 21
  • 3