【问题标题】:How to select certain colours from a colour palette in R ggplot如何从 R ggplot 中的调色板中选择某些颜色
【发布时间】:2018-11-13 17:46:43
【问题描述】:

我正在 ggplot 中制作与此类似的条形图:

但我无法轻易看到投影仪上最亮的彩条。

我想跳过“蓝色”调色板中最浅的蓝色,而是使用 2:9 或 3:9 的颜色进行绘图。

我以鸢尾花数据集为例:

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)

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

非常感谢!

【问题讨论】:

  • RColorBrewer::brewer.pal(9, 'Blues') 给出了来自“Blues”的所有九种颜色的向量
  • 为了澄清,在我标记为重复的问题中,我们正在向调色板添加颜色。在您的情况下,您正在删除颜色。但是添加或删除向量的元素是一样的

标签: r ggplot2


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-25
    • 1970-01-01
    • 1970-01-01
    • 2015-08-16
    • 1970-01-01
    相关资源
    最近更新 更多