Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the color range of scale_colour_brewer() in ggplot2? (palette selected)

I am sorry I cannot offer an image because of limited reputation I have on this site...

I used the following code to generate my line chart in R:

p <- ggplot()+
geom_line(data=data, aes(x, y, color=Label))+ scale_colour_brewer(palette="Oranges")

I used the palette"Oranges" because I want to generate a series of lines with similar while different colors.

However, the color of lower/upper range is too light, so I want to set a limit for the palette to avoid whitish colors.

I know I should specify something like scale_color_gradient(low = "green", high = "red"), but how can I find the specify color with a given palette?

Many thanks!

like image 373
Yuyu ZENG Avatar asked Apr 06 '15 06:04

Yuyu ZENG


People also ask

What is the default color palette in Ggplot?

By default, ggplot2 chooses to use a specific shade of red, green, and blue for the bars.

How do you make a color scale in R?

If we want to create a color range in R, we can use the colorRampPalette function. In the previous R code, we have defined a new function called fun_color_range, which can be used to generate color ranges from the color with the color code #1b98e0 to red. The color codes are now stored in the data object my_colors.

How to change the colors of a ggplot2 scatterplot using rcolorbrewer?

To do that using the RColorBrewer package, we have to install and load the package: Now, we can jump right into the examples! In this example, I’ll show how to change the colors of a ggplot2 scatterplot using the scale_colour_brewer function. For this, we have to add the scale_colour_brewer function to our ggplot2 plot that we have created before.

What is the range of the color scale in each graph?

So we know that the color scale should be from 1 to 8. We define this range as col.range and we then use it to specify the range in each graph: This will get you the following graph.

Do you understand the color scale difference between the two plots?

Do I understand this correctly? You have two plots, where the values of the color scale are being mapped to different colors on different plots because the plots don't have the same values in them. In the top one, dark blue is 1 and light blue is 4, while in the bottom one, dark blue is (still) 1, but light blue is now 8.

How to set the Order of colours in R ColorBrewer?

If a string, will use that named palette. If a number, will index into the list of palettes of appropriate type. The list of available palettes can found in the Palettes section. Sets the order of colours in the scale. If 1, the default, colours are as output by RColorBrewer::brewer.pal (). If -1, the order of colours is reversed.


Video Answer


2 Answers

Since you have a discrete scale, you should be able to manually create the set of colors and use scale_color_manual without too much trouble.

library(ggplot2)
theme_set(theme_bw())
fake_data = data.frame(
    x = rnorm(42), 
    y = rnorm(42), 
    Label = rep(LETTERS[1:7], each = 6))

p_too_light <- ggplot()+ geom_line(data=fake_data, aes(x, y, color=Label))+ 
  scale_colour_brewer(palette="Oranges")
p_too_light

Now use brewer.pal and http://www.datavis.ca/sasmac/brewerpal.html.

library(RColorBrewer)
my_orange = brewer.pal(n = 9, "Oranges")[3:9] #there are 9, I exluded the two lighter hues

p_better <- ggplot()+ geom_line(data=fake_data, aes(x, y, color=Label))+ scale_colour_manual(values=my_orange)
p_better

If you have more than 6 categories, you could use colorRampPalette with the boundary colors from the brewer.pal call earlier. However, now choosing the palette scheme requires more thought (maybe why ggplot2 doesn't do this automatically for discrete scales).

fake_data2 = data.frame(
  x = rnorm(140), 
  y = rnorm(140), 
  Label = rep(LETTERS[1:20], each = 7))

orange_palette = colorRampPalette(c(my_orange[1], my_orange[4], my_orange[6]), space = "Lab")
my_orange2 = orange_palette(20)

p_20cat <- ggplot()+ geom_line(data=fake_data2, aes(x, y, color=Label))+
  scale_colour_manual(values=my_orange2)
p_20cat
like image 171
bmayer Avatar answered Oct 06 '22 11:10

bmayer


A nice direct answer to this question is to use the {shades} library. For example, to reduce the lightness of scale_color_brewer by 20%, do:

library(shades)
lightness(scale_color_brewer(palette="Oranges"),
          scalefac(0.8))
like image 1
dash2 Avatar answered Oct 06 '22 11:10

dash2