Can I adjust the lower limit of scale_color_brewer?
Asked Answered
B

3

9

I have ordered categorical data that I would like to use color brewer on. But I have a hard time seeing the very light lower values. Is there a way to either trim off those lower values or set the lower limit in the scale?

ggplot(data.frame(x=1:6, y=10:15, w=letters[1:6]), aes(x, y, color=w)) + 
geom_point()+ scale_color_brewer(type="seq", palette=1) + theme_bw()

Is there a better way to do this? So far I either see qualitative scales that aren't ordered or continuous scales that don't like being applied to discrete data. I'm aware of manual scales if that's the only route.

Blindstory answered 21/11, 2014 at 6:3 Comment(1)
A simple possible alternative would be to use a plot symbol which have an outline around a filled area, such as pch = 21 (see e.g. here). Then you could use the fill aesthetic instead. ggplot(data.frame(x = 1:6, y = 10:15, w = letters[1:6]), aes(x, y, fill = w)) + geom_point(pch = 21,size = 5)+ scale_fill_brewer(type = "seq", palette = 1) + theme_bw()Feeble
S
7

You cannot just set a lower limit. But you can use a palette with more colors than needed and map the brightest colors to unused levels. Below is an example with 9 levels:

ggplot(data.frame(x=1:6, y=10:15, w=letters[1:6]), aes(x, y, color=w)) + 
  geom_point() + theme_bw() + 
  scale_color_brewer(type="seq", palette=1, 
                     limits=c(LETTERS[1:3], letters[1:6]), 
                     breaks=letters[1:6])
Sp answered 21/11, 2014 at 7:54 Comment(2)
Yes! That's the workaround I was trying to figure out. Thanks!Blindstory
Can you please elaborate on what you're doing in the limits argument here? Where does the all-caps LETTERS come from? How can this be extrapolated to a generic sense where I'm not plotting letters?Hawking
H
5

While @shadow's answer was a start for me, the kind of brewer palette I needed to use (sequential) only has 9 values -- I had 8 categorical variables to plot! Removing only the 9th and lightest palette color still wasn't enough to make the color scheme completely visible.

So I used the colorRampPalette() function, which allows you to expand existing color palettes into continuous functions:

library(RColorBrewer)
ggplot(data.frame(x=1:6, y=10:15, w=letters[1:6]), aes(x, y, color=w)) + 
  geom_point() + theme_bw() + 
  scale_color_manual(values = colorRampPalette(brewer.pal(9, "YlGnBu"))(12)[6:12])

So in this case, I'm mapping the (maximum) 9 native colors from the "YlGnBu" palette onto 12 colors, and then only using the darkest 6 of those colors ([6:12]) in the plot.

Hawking answered 30/3, 2020 at 4:23 Comment(0)
H
1

I'm not aware of any additional arguments you could pass to scale_colour_brewer() to set the lower limit of the scale (see http://docs.ggplot2.org/current/scale_brewer.html)

You have more flexibility with one of ggplot's colour options, which take the format of: scale_xxx_yyy, for example scale_fill_discrete() which take more arguments. See for example http://docs.ggplot2.org/current/scale_hue.html but also note the other options ('see also').

scale_fill_continuous might be a good starting place for ordinal data as you've requested.

You could, for example, pass colours from http://colorbrewer2.org/ to it, and choose a more suitable starting colour. The only problem is you would need to convert the rgb/hex values to HSL values using a tool such as: http://serennu.com/colour/hsltorgb.php

Helvetian answered 21/11, 2014 at 7:42 Comment(2)
Thanks @Phil. I tried scale_colour_discrete but couldn't find a way to do ordered categorical values (ie on some sort of gradient). Do you know of a way? If I just pick values, then I'm pretty much back to manual.Blindstory
I've updated my answer. @Sp 's answer was more applicable to your specific requirements, but the scale_xxx_yyy options are always worth considering.Helvetian

© 2022 - 2024 — McMap. All rights reserved.