How to select certain colours from a colour palett

2020-03-31 05:55发布

问题:

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

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

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

I have used the iris dataset as an example:

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.

Many thanks!

回答1:

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 ggplot2