Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Picking individual colours from a RColorBrewer palette as a scale_colour_manual() value in ggplot2

Tags:

r

ggplot2

Using scale_colour_manual(), can one pick specific colours from RColorBrewer to use as colour values?

For instance, in:

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

I'd like to use the first colour from the palette scale_colour_brewer(type = "qual", palette = 7) instead of "green", then the fourth colour of the palette scale_colour_brewer(type = "qual", palette = 2) instead of "orange", and so on.

like image 411
Lucien S. Avatar asked Oct 09 '14 21:10

Lucien S.


1 Answers

I often do the following,

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
like image 143
baptiste Avatar answered Sep 23 '22 22:09

baptiste