在装箱的颜色/填充比例中指定箱的颜色 [英] Specify bin colours in binned colour/fill scales

查看:67
本文介绍了在装箱的颜色/填充比例中指定箱的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 scale_fill_gradientn()考虑以下图,其中我手动指定颜色...

 库(ggplot2)p<-ggplot(忠实)+geom_tile(aes(等待,爆发,填充=密度))p + scale_fill_gradientn(colours = c("white","red","blue","green")),限制= c(0,0.04)) 

由(v0.3.0)创建于2021-01-08 sup>

但是,您可以看到已选择的装箱颜色已插值.相反,我想明确指定它们,例如[0.00,0.01]是白色,[0.01,0.02]是红色,等等.该怎么办?

标记

解决方案

这是我根据此问题的答案找到的替代解决方案:

(v0.3.0)创建于2021-01-10 sup>

Consider the following plot using scale_fill_gradientn() where I specify the colours manually...

library(ggplot2)
p <- ggplot(faithfuld) +
  geom_tile(aes(waiting, eruptions, fill = density))

p+  scale_fill_gradientn(colours=c("white","red","blue","green"),
                         limits=c(0,0.04))

Created on 2021-01-08 by the reprex package (v0.3.0)

Apologies for the (deliberately) ugly colour scheme.

Now, I would much prefer to have this as binned colours, which can now be done using scale_fill_stepsn() like so.

p+  scale_fill_stepsn(colours=c("white","red","blue","green"),
                      breaks=seq(0.01,0.03,by=0.01),
                      limits=c(0,0.04))

Created on 2021-01-08 by the reprex package (v0.3.0)

However, you can see that the binned colours that have been chosen are interpolated. Instead I would like to specify them explicitly, so that e.g. [0.00,0.01] is explicitly white, [0.01,0.02] is red etc... How can I do this?

Mark

解决方案

Here is an alternative solution I found, based on the answer to this question:

Custom color palette for scale_fill_fermenter()

The code is essentially just a rejigging of the code underlying scale_fill_fermenter(), but using manual_pal() to specify the colour palette directly.

library(ggplot2)
p <- ggplot(faithfuld) +
  geom_tile(aes(waiting, eruptions, fill = density))

p+  binned_scale("fill",
                 "foo",
                 ggplot2:::binned_pal(scales::manual_pal(c("white","red","blue","green"))),
                 guide="coloursteps",
                 breaks=seq(0.01,0.03,by=0.01),
                 limits=c(0,0.04),
                 show.limits=TRUE)

Created on 2021-01-10 by the reprex package (v0.3.0)

这篇关于在装箱的颜色/填充比例中指定箱的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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