从RColorBrewer调色板中选取单个颜色作为ggplot2中的scale_colour_manual()值

时间:2014-10-09 21:02:49

标签: r ggplot2

使用scale_colour_manual(),可以从RColorBrewer中选择特定颜色作为颜色值吗?

例如,在:

scale_colour_manual(breaks=c("A","B","C","D","E"), 
    values=c("green","orange","blue","pink","yellow")) 

我想使用调色板scale_colour_brewer(type = "qual", palette = 7)中的第一种颜色而不是"绿色",然后使用调色板scale_colour_brewer(type = "qual", palette = 2)的第四种颜色而不是&#34 ;橙"等等。

1 个答案:

答案 0 :(得分:17)

我经常做以下事情,

library(RColorBrewer)
my_palette = c(brewer.pal(5, "Set1")[c(1,3,4,5)], brewer.pal(5, "Pastel1")[c(2,5,1,3)])
#grid::grid.raster(my_palette, int=F)

scale_colour_discrete = function(...) scale_colour_manual(..., values = palette())

dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
(p <- qplot(carat, price, data = dsamp, colour = clarity)) # default palette

palette(my_palette)
p # custom colors