20  Overlapping frequency polygons

This graph is largely complete and just needs final proof reading.


This graph requires:

✅ a categorical variable

✅ a numeric (continuous) variable

20.1 Description

Overlapping frequency polygons are similar to overlapping histograms–they allow us to compare distributions of a continuous variable across the levels of a categorical variable.

Instead of using bars, frequency polygons use lines to show the shape of the distribution.

20.2 Set up

PACKAGES:

Install packages.

show/hide
install.packages("palmerpenguins")
library(palmerpenguins) 
library(ggplot2)

DATA:

Artwork by allison horst

The penguins data.

show/hide
penguins <- palmerpenguins::penguins
glimpse(penguins)
#> Rows: 344
#> Columns: 8
#> $ species           <fct> Adelie, Adelie, Adelie…
#> $ island            <fct> Torgersen, Torgersen, …
#> $ bill_length_mm    <dbl> 39.1, 39.5, 40.3, NA, …
#> $ bill_depth_mm     <dbl> 18.7, 17.4, 18.0, NA, …
#> $ flipper_length_mm <int> 181, 186, 195, NA, 193…
#> $ body_mass_g       <int> 3750, 3800, 3250, NA, …
#> $ sex               <fct> male, female, female, …
#> $ year              <int> 2007, 2007, 2007, 2007…

::::

20.3 Grammar

CODE:

  • Create labels with labs()

  • Initialize the graph with ggplot() and provide data

  • Map flipper_length_mm to the x and species to group

  • Map species to the color aesthetic inside the geom_freqpoly()

show/hide
labs_ovrlp_freq_poly <- labs(
  title = "Adult foraging penguins",
  x = "Flipper length (mm)",
  color = "Species")
ggp2_ovrlp_freq_poly <- ggplot(data = penguins, 
       aes(x = flipper_length_mm,
           group = species)) + 
  geom_freqpoly(aes(color = species))
ggp2_ovrlp_freq_poly + 
  labs_ovrlp_freq_poly

GRAPH: