Skip to content

Instantly share code, notes, and snippets.

@andrewheiss
Created July 11, 2023 13:59
Show Gist options
  • Save andrewheiss/61ff28a5856f3393240811ff48ef2c15 to your computer and use it in GitHub Desktop.
Save andrewheiss/61ff28a5856f3393240811ff48ef2c15 to your computer and use it in GitHub Desktop.
library(tidyverse)
library(palmerpenguins)
library(patchwork)
library(rcartocolor)
library(scales)

# Clean up the data
penguins <- penguins |> 
  drop_na(sex) |>
  group_by(species) |>
  mutate(diff_from_mean = body_mass_g - mean(body_mass_g))

# Set a theme for all the plots
theme_set(
  theme_minimal(base_family = "Jost") + 
    theme(plot.title = element_text(face = "bold"),
          panel.grid.minor = element_blank(),
          legend.position = "bottom",
          legend.justification = "left")
)

# Use a few colors from the Prism palette
prism_colors <- carto_pal(name = "Prism")
p1 <- penguins |>
  count(species) |>
  ggplot(aes(x = species, y = n, fill = species)) +
  geom_col() +
  scale_fill_manual(
    values = c(prism_colors[c(2, 6, 8)]), guide = "none"
  ) +
  labs(
    x = "Species", y = "Count",
    title = "Prism palette", subtitle = "Qualitative"
  )
p1

# Use the Magenta palette as a gradient
p2 <- ggplot(
  penguins,
  aes(x = flipper_length_mm, y = body_mass_g, color = bill_depth_mm)
) +
  geom_point(size = 2) +
  scale_x_continuous(
    labels = label_comma(scale_cut = cut_si("mm"))
  ) +
  scale_y_continuous(
    labels = label_comma(scale_cut = cut_si("g"))
  ) +
  scale_color_carto_c(
    palette = "Magenta", labels = label_comma(scale_cut = cut_si("mm"))
  ) +
  guides(color = guide_colorbar(
    barwidth = 15, barheight = 0.6, title.vjust = 1
  )) +
  labs(
    x = "Flipper length", y = "Body mass", color = "Bill depth",
    title = "Magenta palette", subtitle = "Sequential"
  )
p2

# Use the OrYel palette as distinct steps
p3 <- ggplot(
  penguins,
  aes(x = flipper_length_mm, y = body_mass_g, color = bill_depth_mm)
) +
  geom_point(size = 2) +
  scale_x_continuous(
    labels = label_comma(scale_cut = cut_si("mm"))
  ) +
  scale_y_continuous(
    labels = label_comma(scale_cut = cut_si("g"))
  ) +
  scale_color_stepsn(
    colors = carto_pal(name = "OrYel"),
    labels = label_comma(scale_cut = cut_si("mm"))
  ) +
  guides(color = guide_colorbar(
    barwidth = 15, barheight = 0.6, title.vjust = 1
  )) +
  labs(
    x = "Flipper length", y = "Body mass", color = "Bill depth",
    title = "OrYel palette", subtitle = "Sequential"
  )
p3

# Use the Temps palette centered at 0
p4 <- ggplot(
  penguins,
  aes(x = flipper_length_mm, y = body_mass_g, color = diff_from_mean)
) +
  geom_point(size = 2) +
  scale_x_continuous(
    labels = label_comma(scale_cut = cut_si("mm"))
  ) +
  scale_y_continuous(
    labels = label_comma(scale_cut = cut_si("g"))
  ) +
  scale_color_carto_c(
    palette = "Temps",
    limits = c(-1300, 1300),
    labels = label_comma(
      scale_cut = cut_si("g"),
      style_negative = "minus"
    )
  ) +
  guides(color = guide_colorbar(
    barwidth = 12, barheight = 0.6, title.vjust = 1
  )) +
  labs(
    x = "Flipper length", y = "Body mass",
    color = "Difference from\nmean species weight",
    title = "Temps palette", subtitle = "Diverging"
  )
p4

# Combined plot
(p1 | p4) / (p2 | p3)

ggsave("carto-penguins.png", width = 10, height = 6, bg = "white")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment