既然你有一个离散的比例尺,你应该能够手动创建一组颜色并且使用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