how to use colour palettes with ggplot
in ggplot
October 22, 2020
If I had a dollar for everytime I have googled how to change the colours in my ggplot…it is clearly time for a “I don’t have to google” post about it.
library(tidyverse)
library(palmerpenguins)
library(RColorBrewer)
library(harrypotter)
penguins <- penguins
make a couple of basic plots
When dealing with geom_point(), you can use colour to change the colour of the points. For geom_col(), you need to use fill; colour will change the border around your bars.
Plot 1: scatter plot flipper length by body mass, colour by species
penguin_point <- penguins %>%
ggplot(aes(x = flipper_length_mm, y = body_mass_g, colour = species)) +
geom_point()
penguin_point
Plot 2: mean body mass by sex and species, fill by sex
mean_mass <- penguins %>%
na.omit() %>%
group_by(species, sex) %>%
summarise(mean_mass = mean(body_mass_g, na.rm = TRUE))
## `summarise()` has grouped output by 'species'. You can override using the `.groups` argument.
penguin_col <- mean_mass %>%
ggplot(aes(x = sex, y = mean_mass, fill = sex)) +
geom_col() +
facet_wrap(~ species)
penguin_col
use RColourBrewer to automatically change the colours
Useful RColourBrewer blog post for reference.
See all the palettes using display.brewer.all()
display.brewer.all()
This package includes 3 types of palettes:
- sequential (low to high)
- qualitative (best for categorical data)
- diverging (extremes on the ends)
Most of the time I am looking to colour categorical variables so those in the middle work best.
penguin_point +
scale_color_brewer(palette = "Dark2")
penguin_col +
scale_fill_brewer(palette = "Paired")
use RColourBrewer to manually change the colours
When you are using scale_fill_brewer() or scale_colour_brewer(), R automatically assumes you want the first colours in the palette, but how do you choose individual colours?
You can look at the colours in a particular palette using display.brewer.pal() and get the hexidecimal values corresponding to each colour using brewer.pal()
# View a single RColorBrewer palette by specifying its name
display.brewer.pal(n = 8, name = 'Dark2')
# Hexadecimal color specification
brewer.pal(n = 8, name = "Dark2")
## [1] "#1B9E77" "#D95F02" "#7570B3" "#E7298A" "#66A61E" "#E6AB02" "#A6761D"
## [8] "#666666"
Then can use scale_colour_manual() to choose particular colours, as you would if you were naming colours. Just use the hexidecimal values instead.
penguin_point +
scale_color_manual(values = c("red", "blue", "green"))
penguin_point +
scale_color_manual(values = c("#E7298A", "#66A61E", "#E6AB02"))
penguin_col +
scale_fill_manual(values = c("purple", "orange"))
brewer.pal(8, "Paired")
## [1] "#A6CEE3" "#1F78B4" "#B2DF8A" "#33A02C" "#FB9A99" "#E31A1C" "#FDBF6F"
## [8] "#FF7F00"
penguin_col +
scale_fill_manual(values = c("#B2DF8A", "#33A02C"))
Other palette options to explore
Or use this blog from Anna Henschel to make your own palette!