如何从R ggplot的调色板中选择某些颜色 [英] How to select certain colours from a colour palette in R ggplot

查看:305
本文介绍了如何从R ggplot的调色板中选择某些颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在ggplot中绘制类似于此的条形图:

I am making a bar plot similar to this in ggplot:

但是我不能轻易看到投影机上最亮的彩条.

But I cannot easily see the lightest colour bars on projectors.

我想跳过蓝调"(Blues)调色板中最浅的蓝色,而将2:9或3:9的颜色用于绘图.

I would like to skip the lightest colour blue in the "Blues" palette and instead use colours 2:9 or 3:9 for plots.

我以虹膜数据集为例:

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)

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

It seems the common fix is to make the background darker, but this would look out of place with my other images and is therefore not an option. It is important I am also able to rename the legend, as the example.

非常感谢!

推荐答案

这里是一种方法,您将调色板预先定义为四色蓝色调色板的最后3种颜色:

Here's one approach, where you predefine the palette as being the last 3 colors of a four-color Blue palette:

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)

这篇关于如何从R ggplot的调色板中选择某些颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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