2016-12-26 144 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

相关问题