如何使所有低于阈值的值具有连续刻度的单色? [英] How to make all values below a threshold have single color with scale continuous?

查看:60
本文介绍了如何使所有低于阈值的值具有连续刻度的单色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将变量映射到连续的色标,但是如果该值低于某个阈值,则它应该具有特定的颜色.在此示例中,我想使用 mpg<20 用灰色填充,上面的所有内容都使用连续的色标:

 库(ggplot2)达特<-头(mtcars)dat $ model<-头(colnames(mtcars))dat $ is_low<-ifelse(dat $ mpg< 20,TRUE,FALSE)ggplot(dat,aes(x =模型,y = mpg,填充= mpg))+geom_col() 

那有可能吗?我所看到的最接近的是此

I would like to map a variable to a continuous color scale, but if the value is a below a certain threshold, it should have a specific color. In this example, I would like to make all car models with an mpg < 20 filled with gray, and everything above with the continuous color scale:

library(ggplot2)
dat <- head(mtcars)
dat$model <- head(colnames(mtcars))
dat$is_low <- ifelse(dat$mpg < 20, TRUE, FALSE)

ggplot(dat, aes(x = model, y = mpg, fill = mpg)) +
    geom_col()

Is that even possible? The closest I have seen is this solution but it maps a second variably to scale_color and not to scale_fill_continuous

解决方案

Not the most elegant, but you could add a layer that's filtered to just the ones you want gray:

ggplot(dat, aes(x = model, y = mpg, fill = mpg)) +
  geom_col() +
  geom_col(data = dat[dat$is_low,], fill = "gray70")

这篇关于如何使所有低于阈值的值具有连续刻度的单色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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