-1

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 ?

ClementWalter
  • 4,814
  • 1
  • 32
  • 54
  • Not my downvote but, could you please provide a small example of data and the ggplot statement you would like to use it in. – G5W Dec 26 '16 at 22:36
  • 1
    Looks like a [Previous SO Post](http://stackoverflow.com/questions/3079264/using-a-pre-defined-color-palette-in-ggplot) used the same `scale_fill_manual` solution that you came up with – G5W Dec 26 '16 at 22:54
  • I had a look at this post too – ClementWalter Dec 26 '16 at 23:02

1 Answers1

3

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

baptiste
  • 75,767
  • 19
  • 198
  • 294