2016-12-26 30 views
-1

我已經定義了一個調色板colorRampPalette用於每個情節。如何使用ggplot2默認設置此調色板?如何在ggplot2中使用自己的調色板?

我不知道該怎麼做,也找不到答案。

MWE:

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

現在如何使用調色板本身,而不是黑客:

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

更一般地講,是有什麼樣theme(palette = mypalette)所以必須有它總是自己的調色板默認 ?

+0

不是我的downvote,但是,請你提供數據和ggplot的一個小例子聲明你想使用它。 – G5W

+1

看起來像一個[前SO崗位](http://stackoverflow.com/questions/3079264/using-a-pre-defi ned-color-palette-in-ggplot)使用了與你想出的相同的'scale_fill_manual'解決方案 – G5W

+0

我也看過這篇文章 – clemlaflemme

回答

3

我發現了以下策略有時是有用的:

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

相關問題