How to use its own palette with ggplot2?

1.4k views Asked by At

I have defined a palette with colorRampPalette to be used for each plot. How can set this palette by default with ggplot2 ?

I have no idea how to do that and could not find the answer.

MWE :

df <- data.frame(x = letters[1:3], y = runif(3), fill = LETTERS[1:3])
mypalette <- colorRampPalette(colors = c("white", "blue"))

Now how to use the palette itself and not the hack:

ggplot(df, aes(x=x,y=y)) + geom_col(aes(fill = fill)) +
    scale_fill_manual(values = mypalette(3))

And more generally speaking, is there something like theme(palette = mypalette) so has to have always it own palette by default ?

1

There are 1 answers

0
baptiste On BEST ANSWER

I've found the following strategy sometimes useful:

scale_fill_discrete <- function(...) 
  scale_fill_manual(..., values=palette())

ggplot(df, aes(x=x,y=y)) + geom_col(aes(fill = fill))

enter image description here

palette(c("#738290", "#A1B5D8",  "#C2D8B9"))

ggplot(df, aes(x=x,y=y)) + geom_col(aes(fill = fill))

enter image description here