2015-04-06 995 views
6

我很抱歉,我不能提供一个形象,因为有限的声誉我对这个网站...如何在ggplot2中设置scale_colour_brewer()的颜色范围? (选择调色板)

我用下面的代码来生成R中我的折线图:

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

我使用了调色板“橙子”,因为我想用不同的颜色生成一系列相似的线条。

但是,较低/较高范围的颜色太浅,所以我想设置调色板的限制以避免发白的颜色。

我知道我应该指定类似scale_color_gradient(low = "green", high = "red")的东西,但是如何才能找到指定的颜色与给定的调色板?

非常感谢!

回答

4

既然你有一个离散的比例尺,你应该能够手动创建一组颜色并且使用scale_color_manual没有太多的麻烦。

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 

现在使用brewer.pal和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 

如果您有超过6个类别,则可以使用colorRampPalette以及之前brewer.pal调用的边界颜色。然而,现在选择调色板方案需要更多的思考(也许为什么ggplot2不会自动执行离散尺度)。

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 
相关问题