##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
## 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)
# requires continuous color scale ----
cont_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)
Note
This template follows lecture 5.2 slides. Please be sure to cross-reference the slides, which contain important information and additional context!
Setup
Colors for inclusive & accessible design
Viridis scales
- Check out the documentation for additional viridis palette options.
RColorBrewer scales
- Exploring available palettes and generating HEX codes (or check out the web-based interface):
# 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')
[1] "#1B9E77" "#D95F02" "#7570B3"
- Using RColorBrewer palettes
Accessibility tips
- outline light-colored points, which are difficult to see
ggplot(penguins, aes(x = bill_length_mm, y = bill_depth_mm, fill = body_mass_g)) +
geom_point(shape = 21, size = 4, alpha = 0.8) +
scale_fill_distiller(palette = "BuPu")
- use redundant mapping (colors & shapes) whenever possible (recall our original plot, with an added viridis color palette)
Paletteer
I. apply palette using scale_*_paletteer_*()
# discrete data / palette ----
cat_color_plot +
paletteer::scale_color_paletteer_d("calecopal::superbloom3")
# continuous data / palette (unclassed) ----
cont_color_plot +
paletteer::scale_color_paletteer_c("scico::batlow", direction = -1)
# continuous data / palette (classed) ----
cont_color_plot +
paletteer::scale_color_paletteer_binned("scico::batlow", direction = -1)
II. create vector of colors using paletteer_*()
, then apply using the appropriate ggplot::scale_*()
function
- discrete data / palette example:
<colors>
#F1BB7BFF #FD6467FF #5B1A18FF
# apply to scatter plot (use `color` variant) ----
cat_color_plot +
scale_color_manual(values = pal_d)
# apply to histogram (use `fill` variant) ----
ggplot(penguins, aes(x = body_mass_g, fill = species)) +
geom_histogram() +
scale_fill_manual(values = pal_d)
- continuous data / palette example:
# create palette ----
pal_c <- paletteer::paletteer_c("ggthemes::Gold-Purple Diverging", n = 5)
pal_c
<colors>
#AD9024FF #CBAE4DFF #E3D8CFFF #CA96B9FF #AC7299FF
# apply to scatter plot as an unclassed palette (use `gradientn` variant) ----
cont_color_plot +
scale_color_gradientn(colors = pal_c)
# apply to scatter plot as a classed (binned) palette (use `stepsn` variant) ----
cont_color_plot +
scale_color_stepsn(colors = pal_c)
Subduing pure hues
- Adjust saturation when selecting a HEX code from Google’s color picker, https://g.co/kgs/9SQkdgv, or adjust chroma directly in your ggplot:
- Adjust value when selecting a HEX code from Google’s color picker, https://g.co/kgs/9SQkdgv, or adjust lightness directly in your ggplot:
ggplot(na.omit(penguins), aes(x = bill_length_mm, y = bill_depth_mm, color = sex)) +
geom_point() +
scale_color_hue(l = 45)
- Increase transparency using the
alpha
argument in ggplot
Some final palette tips
Save palette outside of plot
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)
Use scale_*_identity()
# example 1 (color points based on value) ----
penguins |>
mutate(
my_color = case_when(
bill_length_mm < 40 ~ "#D7263D",
between(bill_length_mm, 40, 50) ~ "#E4BB97",
bill_length_mm > 50 ~ "#386150"
)
) |>
ggplot(aes(x = bill_length_mm, y = bill_depth_mm, color = my_color)) +
geom_point(size = 4, alpha = 0.8) +
scale_color_identity()
# example 2 (highlight points based on criteria)
penguins |>
mutate(
my_color = case_when(
body_mass_g > 6000 ~ "#D7263D",
TRUE ~ "gray50"
)
) |>
ggplot(aes(x = bill_length_mm, y = bill_depth_mm, color = my_color)) +
geom_point(size = 4, alpha = 0.8) +
scale_color_identity(guide = "legend",
name = "Body mass (g)", labels = c(">6000", "<= 6000"))