This template follows the colors lecture . Please be sure to cross-reference the slides, which contain important information and additional context!
Setup
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
## setup ----
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#..........................load packages.........................
library (palmerpenguins)
library (tidyverse)
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
## create base plots ----
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#................requires categorical color scale................
cat_color_plot <- ggplot (na.omit (penguins),
aes (x = bill_length_mm, y = bill_depth_mm,
color = species, shape = species)) +
geom_point (size = 4 , alpha = 0.8 )
cat_color_plot
#................requires continuous color scale.................
con_color_plot <- ggplot (na.omit (penguins),
aes (x = bill_length_mm, y = bill_depth_mm,
color = body_mass_g)) +
geom_point (size = 4 , alpha = 0.8 )
con_color_plot
Colors for inclusive & accessible design
Viridis scales
#....................discrete viridis scales.....................
cat_color_plot +
scale_color_viridis_d (option = "viridis" )
#....................continuous viridis scales...................
con_color_plot +
scale_color_viridis_c (option = "magma" )
RColorBrewer scales
#.....display only colorblind-friendly RColorBrewer palettes.....
RColorBrewer:: display.brewer.all (colorblindFriendly = TRUE )
#.......preview palette with your number of desired colors.......
RColorBrewer:: display.brewer.pal (n = 3 , name = "Dark2" )
#..............print the HEX codes of your palette...............
RColorBrewer:: brewer.pal (n = 3 , name = "Dark2" )
[1] "#1B9E77" "#D95F02" "#7570B3"
#..................or save hex codes as a vector.................
my_brewer_pal <- RColorBrewer:: brewer.pal (n = 3 , name = "Dark2" )
Using RColorBrewer palettes
#....................for qualitative palettes....................
# Accessibility tip: use redundant mapping (e.g. colors + shapes) whenever possible
cat_color_plot +
scale_color_brewer (palette = "Dark2" )
#..............for unclassed continuous color scales.............
con_color_plot +
scale_color_distiller (palette = "BuPu" )
#..............for classed continuous color scales...............
# Accessibility tip: outline points to make light colors more visible
ggplot (penguins, aes (x = bill_length_mm, y = bill_depth_mm, fill = body_mass_g)) + # notice switch from `color` > `fill`
geom_point (shape = 21 , size = 4 , alpha = 0.8 ) + # notice point `shape`
scale_fill_distiller (palette = "YlGnBu" )
{paletteer}
A few different ways to use {paletteer} palettes, but my preferred approach takes two steps:
create vector of colors using paletteer_*()
apply your palette using the appropriate ggplot::scale_*() function
For discrete data:
#........................1. create palette.......................
pal_d <- paletteer:: paletteer_d ("wesanderson::GrandBudapest1" , n = 3 )
pal_d
<colors>
#F1BB7BFF #FD6467FF #5B1A18FF
#.........2. apply to scatter plot (use `color` variant).........
cat_color_plot +
scale_color_manual (values = pal_d)
#...........2. apply to histogram (use `fill` variant)...........
ggplot (penguins, aes (x = body_mass_g, fill = species)) +
geom_histogram () +
scale_fill_manual (values = pal_d)
For continuous data:
#........................1. create palette.......................
# reverse order of colors using the `direction` arg
pal_c <- paletteer:: paletteer_c ("scico::batlow" , n = 5 , direction = - 1 )
pal_c
<colors>
#F9CCF9FF #F19D6BFF #818231FF #215F61FF #001959FF
#...2. apply as an unclassed palette (use `gradientn` variant)...
con_color_plot +
scale_color_gradientn (colors = pal_c)
#..2. apply as a classed (binned) palette (use `stepsn` variant).
con_color_plot +
scale_color_stepsn (colors = pal_c)
Some final palette tips
Save palette outside of plot
#.........................create palette.........................
my_palette <- c ("#32DE8A" , "#E36414" , "#0F4C5C" )
#..........................apply to plot.........................
cat_color_plot +
scale_color_manual (values = my_palette)
Set color names
#.........................create palette.........................
my_palette_named <- c ("Adelie" = "#32DE8A" ,"Chinstrap" = "#E36414" , "Gentoo" = "#0F4C5C" )
#..................apply to plot (all penguins)..................
ggplot (penguins, aes (x = bill_length_mm, y = bill_depth_mm, color = species)) +
geom_point (size = 4 , alpha = 0.8 ) +
scale_color_manual (values = my_palette_named)
#..............apply to plot (just adelie & gentoo)..............
penguins |>
filter (species != "Chinstrap" ) |>
ggplot (aes (x = bill_length_mm, y = bill_depth_mm, color = species)) +
geom_point (size = 4 , alpha = 0.8 ) +
scale_color_manual (values = my_palette_named)