Our illustrated penguins have reached the purrr package! The photo backdrop is a snowy Antarctic wonderland featuring a Gentoo penguin with outstretched flippers

purrr: info



Provides tools for working with functions and vectors

The purrr family of functions helps us replace for loops, making our code easier to read and more succint.

With purrr you can

  • Iterate over a single input with map()
  • Iterate over two inputs in parallel with map2()
  • Iterate with multiple arguments with pmap()
  • Iterate with multiple arguments and functions with invoke_map()
  • Call a function for its side-effects with walk(), walk2(), and pwalk()

{fig-alt=“R4DS book cover”}



R for Data Science: Ch 21 Iteration

Package documentation: https://purrr.tidyverse.org

purrr: exercise

Ok, we love our earlier boxplot showing us body_mass_g by sex and colored by species

…but let’s change up the colors to keep with our Antarctica theme!

I’m a big fan of the color palettes in the nord 📦

16 different nordic color palettes from the Nord package. We will be focusing on the mountain_forms palette which was dark teal, dusty blue, snowy white, dusty purple, and dark purple

Let’s turn this plot…

Our filled boxplot from our earlier ggplot2 exercises! To recap, a boxplot with penguin sex along the x axis and body mass along the y axis. Again, the three sex categories are female, male, and NA, and the body mass appears to range between 2400g and 6500g. There is a boxplot for each species per sex category, and these are filled with different colors. Gentoo boxplots are blue, Adélie boxplots are reddish, and Chinstrap boxplots are green.

…into this one!

In contrast to the other filled boxplot referred to in this tab, this one is filled with nordic colors. Gentoo boxplots are a dark purple, Adélie boxplots are a dark teal, and Chinstrap boxplots are a snowy white.

Note: The color choices in this example are meant for demo purposes only. Be sure to consider the accessibility of your data viz, including color contrast between different elements.

You can choose colors using
the color hex codes

nord::nord_palettes$mountain_forms
[1] "#184860" "#486078" "#d8d8d8" "#484860" "#181830"

And assign them using the scale_fill_manual() function

penguins |>
  ggplot(aes(x = sex, y = body_mass_g)) +
  geom_boxplot(aes(fill = species)) +
  scale_fill_manual(
    values = c("#184860", 
               "#D8D8D8", 
               "#181830"))


You can also use the palette name, like mountain_forms, though the colors assigned may not align with what you want

penguins |>
  ggplot(aes(x = sex, y = body_mass_g)) +
  geom_boxplot(aes(fill = species)) +
  scale_fill_manual(
    values = nord::nord_palettes$mountain_forms
    )

Our boxplot filled with nordic colors though the scale_fill_manual function has automatically selected a different combination of colors from the palette. Gentoo boxplots are snowy white intead of dark purple, Adélie boxplots are a still a dark teal, and Chinstrap boxplots are a dusty blue intead of snowy white.

And sometimes, color palette packages come with their own functions that assign colors, like scale_fill_nord()

penguins |>
  ggplot(aes(x = sex, y = body_mass_g)) +
  geom_boxplot(aes(fill = species)) +
  nord::scale_fill_nord(
    palette = "mountain_forms"
    )

Our boxplot filled with nordic colors. Gentoo boxplots are a dark purple, Adélie boxplots are a dark teal, and Chinstrap boxplots are a snowy white.

The prismatic 📦 helps us see the colors that correspond to each color hex code (mostly), with the color() function

library(prismatic)

prismatic::color(
    nord::nord_palettes$mountain_forms
    )

hex codes for the 5 colors in the mountain_forms palette, with a background color to match it. Dark teal is #184860, dusty blue is #486078, snowy white is #D8D8D8, purple is #484860, and dark purple is #181830

purrr’s map() function can help us iterate color() over all palettes in a palette package like nord!

nord::nord_palettes |> 
    map(prismatic::color)

hex color codes for 4 of the palettes in the nord package, including mountain_forms



🎨 r-color-palettes repo from Emil Hvitfeldt

Like this Wes Anderson themed one! And many, many others 🤩

10 different bright color palettes from the wesanderson color palette package.