knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  warning = FALSE,
  message = FALSE,
  out.width = "100%"
)
library(palmerpenguins)
library(dplyr)
library(ggplot2)
theme_set(theme_minimal())

The palmerpenguins R package contains two datasets that we believe are a viable alternative to Anderson's Iris data (see datasets::iris). In this introductory vignette, we'll highlight some of the properties of these datasets that make them useful for statistics and data science education, as well as software documentation and testing.

You can download all the palmerpenguins art directly from vignette("art")

Meet the penguins

The palmerpenguins data contains size measurements for three penguin species observed on three islands in the Palmer Archipelago, Antarctica.

The Palmer Archipelago penguins. Artwork by @allison_horst.
The Palmer Archipelago penguins. Artwork by @allison_horst.

\ Aside: That's right, developers -- Gentoo Linux is named after penguins!

These data were collected from 2007 - 2009 by Dr. Kristen Gorman with the Palmer Station Long Term Ecological Research Program, part of the US Long Term Ecological Research Network. The data were imported directly from the Environmental Data Initiative (EDI) Data Portal, and are available for use by CC0 license ("No Rights Reserved") in accordance with the Palmer Station Data Policy.

Installation

You can install the released version of palmerpenguins from CRAN with:

install.packages("palmerpenguins")

Or install the development version from GitHub with:

# install.packages("remotes")
remotes::install_github("allisonhorst/palmerpenguins")

The palmerpenguins package

This package contains two datasets:

  1. Here, we'll focus on a curated subset of the raw data in the package named penguins, which can serve as an out-of-the-box alternative to datasets::iris.

  2. The raw data, accessed from the Environmental Data Initiative (see full data citations below), is also available as palmerpenguins::penguins_raw.

The curated palmerpenguins::penguins dataset contains r ncol(penguins) variables (n = r nrow(penguins) penguins). You can read more about the variables by typing ?penguins.

glimpse(penguins)

The palmerpenguins::penguins data contains r sum(complete.cases(penguins)) complete cases, with r sum(is.na(penguins)) missing values.

visdat::vis_dat(penguins)
ggsave("vignettes/figs/penguin-visdat.png", width = 8)
knitr::include_graphics("figs/penguin-visdat.png")

Highlights

We don't want to ruin all the fun exploration, visualization, and potential analyses, so below are just a few examples to get you quickly waddling along with penguins. You can check out more in vignette("examples").


Exploring factors

The penguins data has three factor variables:

penguins %>%
  dplyr::select(where(is.factor)) %>% 
  glimpse()
# Count penguins for each species / island
penguins %>%
  count(species, island, .drop = FALSE)
ggplot(penguins, aes(x = island, fill = species)) +
  geom_bar(alpha = 0.8) +
  scale_fill_manual(values = c("darkorange","purple","cyan4"), 
                    guide = FALSE) +
  theme_minimal() +
  facet_wrap(~species, ncol = 1) +
  coord_flip()
# Count penguins for each species / sex
penguins %>%
  count(species, sex, .drop = FALSE)
ggplot(penguins, aes(x = sex, fill = species)) +
  geom_bar(alpha = 0.8) +
  scale_fill_manual(values = c("darkorange","purple","cyan4"), 
                    guide = FALSE) +
  theme_minimal() +
  facet_wrap(~species, ncol = 1) +
  coord_flip()

Exploring scatterplots

The penguins data also has four continuous variables, making six unique scatterplots possible!

penguins %>%
  dplyr::select(body_mass_g, ends_with("_mm")) %>% 
  glimpse()
# Scatterplot example 1: penguin flipper length versus body mass
ggplot(data = penguins, aes(x = flipper_length_mm, y = body_mass_g)) +
  geom_point(aes(color = species, 
                 shape = species),
             size = 2) +
  scale_color_manual(values = c("darkorange","darkorchid","cyan4")) 

# Scatterplot example 2: penguin bill length versus bill depth
ggplot(data = penguins, aes(x = bill_length_mm, y = bill_depth_mm)) +
  geom_point(aes(color = species, 
                 shape = species),
             size = 2)  +
  scale_color_manual(values = c("darkorange","darkorchid","cyan4"))

You can add color and/or shape aesthetics in ggplot2 to layer in factor levels like we did above. With three factor variables to work with, you can add another factor layer with facets, like the plot below.

ggplot(penguins, aes(x = flipper_length_mm,
                     y = body_mass_g)) +
  geom_point(aes(color = sex)) +
  scale_color_manual(values = c("darkorange","cyan4"), 
                     na.translate = FALSE) +
  facet_wrap(~species)

Exploring correlations

Also see vignette("pca") for an example principal component analysis.

penguins %>%
  select(species, body_mass_g, ends_with("_mm")) %>% 
  GGally::ggpairs(aes(color = species)) +
  scale_colour_manual(values = c("darkorange","purple","cyan4")) +
  scale_fill_manual(values = c("darkorange","purple","cyan4"))
ggsave("vignettes/figs/penguin-ggpairs.png", width = 8)
knitr::include_graphics("figs/penguin-ggpairs.png")

Exploring distributions

# Jitter plot example: bill length by species
ggplot(data = penguins, aes(x = species, y = bill_length_mm)) +
  geom_jitter(aes(color = species),
              width = 0.1, 
              alpha = 0.7,
              show.legend = FALSE) +
  scale_color_manual(values = c("darkorange","darkorchid","cyan4"))

# Histogram example: flipper length by species
ggplot(data = penguins, aes(x = flipper_length_mm)) +
  geom_histogram(aes(fill = species), alpha = 0.5, position = "identity") +
  scale_fill_manual(values = c("darkorange","darkorchid","cyan4"))

More

See more examples in:

Or contribute your own!

Package citation

Please cite the palmerpenguins R package using:

citation("palmerpenguins")

References

Data originally published in:

Individual datasets:

Individual data can be accessed directly via the Environmental Data Initiative:

Have fun with the Palmer Archipelago penguins!



allisonhorst/palmerpenguins documentation built on Sept. 20, 2024, 12:05 p.m.