首页 > 解决方案 > 如何从 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)

似乎常见的解决方法是使背景变暗,但这与我的其他图像看起来不合适,因此不是一个选择。重要的是,我还可以重命名图例,例如。

非常感谢!

标签: rggplot2

解决方案


这是一种方法,您将调色板预定义为四色蓝色调色板的最后 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)

在此处输入图像描述


推荐阅读