r - 如何从 R ggplot 中的调色板中选择某些颜色
问题描述
我正在 ggplot 中制作与此类似的条形图:
但我无法轻易看到投影仪上最亮的彩条。
我想跳过“蓝色”调色板中最浅的蓝色,而是使用 2:9 或 3:9 的颜色进行绘图。
我以 iris 数据集为例:
df <- data.frame(iris,petal.colour=c("red","blue"), country=c("UK","France","Germany"))
ggplot(df, aes(petal.colour,Sepal.Length))+
geom_bar(stat = "identity",aes(fill=country))+
facet_wrap(~Species, ncol=3)+
scale_fill_brewer(palette = "Blues" ,
labels = c("French Flower", "German Flower","UK Flower"))+
theme_bw(base_size=18)
似乎常见的解决方法是使背景变暗,但这与我的其他图像看起来不合适,因此不是一个选择。重要的是,我还可以重命名图例,例如。
非常感谢!
解决方案
这是一种方法,您将调色板预定义为四色蓝色调色板的最后 3 种颜色:
my_colors <- RColorBrewer::brewer.pal(4, "Blues")[2:4]
ggplot(df, aes(petal.colour,Sepal.Length))+
geom_bar(stat = "identity",aes(fill=country))+
facet_wrap(~Species, ncol=3)+
scale_fill_manual(values = my_colors,
labels = c("French Flower", "German Flower","UK Flower"))+
theme_bw(base_size=18)
推荐阅读
- go - 无法导出两个 dbus 对象
- django - Django 会在 Windows Server 2019 或 2016 上运行吗?
- docker - 没有错误,但 celery 任务未按预期发送电子邮件
- oracle - 如何通过 Oracle ERP 云中的 Rest/SOAP api 获取密码到期日期
- automation - Ansible 不正确的行为与 when
- c - 如果 FLT_HAS_SUBNORM 为 -1,那么 1.0f / FLT_MAX 是否会产生次正规数或导致未定义行为(UB)?
- oracle - DBMS_RLS.add_policy:object_name 不工作
- php - WooCommerce - 添加到购物车时忽略未选择的变体
- r - 在 R 中的 ggplot2 中添加渐变
- jenkins - Jenkins 没有读取启动配置,https 和 jenkins 的问题