scale_fill_fermenter()的自定义调色板 [英] Custom color palette for scale_fill_fermenter()

查看:95
本文介绍了scale_fill_fermenter()的自定义调色板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将ggplot2的 scale_fill_fermenter()用于离散的色条图例.可以在 palette 参数下指定颜色,但是似乎只能使用一组有限的调色板(来自 RColorBrewer :: display.brewer.all()的调色板).

I'm using ggplot2's scale_fill_fermenter() for a discrete colorbar legend. Colors can be specified under the palette argument, however only a limited set of color palettes seem to be available (those from RColorBrewer::display.brewer.all()).

是否可以定义自定义调色板?传递颜色名称/十六进制向量的简单解决方案不起作用...

Is there a way to define a custom color palette? The trivial solution of passing a vector of color names/hex does not work ...

干杯!

推荐答案

scale_fill_fermenter 是返回的包装器

binned_scale(aesthetics, "fermenter", 
  binned_pal(brewer_pal(type, palette, direction)), 
  na.value = na.value, guide = guide, ...)

如果您可以调用未导出的函数 ggplot2 ::: binned_pa​​l ,则可以将此代码用作模板来编写自定义的 scale_fill_fermenter 函数,该函数允许用于自定义调色板,可能看起来像这样:

If it's fine for you to call the non-exported function ggplot2:::binned_pal you can use this code as a template to write a custom scale_fill_fermenter function which allows for custom color palettes and may look like so:

library(ggplot2)

v <- ggplot(faithfuld) +
  geom_tile(aes(waiting, eruptions, fill = density))

# Custom palette
pal <- scales::hue_pal()(8)

scale_fill_fermenter_custom <- function(pal, na.value = "grey50", guide = "coloursteps", aesthetics = "fill", ...) {
  binned_scale("fill", "fermenter", ggplot2:::binned_pal(scales::manual_pal(unname(pal))), na.value = na.value, guide = guide, ...)  
}

v + scale_fill_fermenter_custom(pal)

这篇关于scale_fill_fermenter()的自定义调色板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆