ggplot2 reverse order of scale_brewer

RGgplot2

R Problem Overview


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

R Solutions


Solution 1 - R

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)

Solution 2 - R

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.

Solution 3 - R

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

Solution 4 - R

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

Solution 5 - R

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

Solution 6 - R

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionyosukesabaiView Question on Stackoverflow
Solution 1 - RpbaylisView Answer on Stackoverflow
Solution 2 - RjoranView Answer on Stackoverflow
Solution 3 - RAntoine LizéeView Answer on Stackoverflow
Solution 4 - RJosh O'BrienView Answer on Stackoverflow
Solution 5 - Rdj20b22View Answer on Stackoverflow
Solution 6 - RSophistMView Answer on Stackoverflow