Graph info

Should I use this graph?


This graph requires:

✅ a numeric (continuous) variable

Description

Box plots (sometimes called box-and-whisker plots) use position, lines (vertical and horizontal), and points to convey a collection of summary statistics in a single graph.

Getting set up

PACKAGES:

Install packages.

Code
install.packages("palmerpenguins")
library(palmerpenguins) 
library(ggplot2)

DATA:

Artwork by @allison_horst

We’ll be using the penguins data from palmerpenguins.

Code
penguins <- palmerpenguins::penguins 
glimpse(penguins)
Rows: 344
Columns: 8
$ species           <fct> Adelie, Adelie, Adelie, Adelie, Adelie, Adelie, Adel…
$ island            <fct> Torgersen, Torgersen, Torgersen, Torgersen, Torgerse…
$ bill_length_mm    <dbl> 39.1, 39.5, 40.3, NA, 36.7, 39.3, 38.9, 39.2, 34.1, …
$ bill_depth_mm     <dbl> 18.7, 17.4, 18.0, NA, 19.3, 20.6, 17.8, 19.6, 18.1, …
$ flipper_length_mm <int> 181, 186, 195, NA, 193, 190, 181, 195, 193, 190, 186…
$ body_mass_g       <int> 3750, 3800, 3250, NA, 3450, 3650, 3625, 4675, 3475, …
$ sex               <fct> male, female, female, NA, female, male, female, male…
$ year              <int> 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007…

The grammar

CODE:

Create labels with labs()

Initialize the graph with ggplot() and provide data

  • Assign a blank character string ("") to the x axis in labs()

Map flipper_length_mm to the y axis and an empty string ("") to the x axis

Add the geom_boxplot() layer

Code
labs_boxplot <- labs(
  title = "Adult foraging penguins",
  subtitle = "Distribution of flipper length",
  x = "",
  y = "Flipper length (millimeters)")
ggp2_boxplot <- ggplot(data = penguins,
           aes(x = "", 
               y = flipper_length_mm)) +
        geom_boxplot() 
ggp2_boxplot + 
  labs_boxplot

GRAPH:

More Info

Below we provide more information on interpreting Box plots.

We’ll use the ggplot2movies::movies data to create a box plot for movie length

Code
install.packages("ggplot2movies")
library(ggplot2movies) 
library(ggplot2)

Filter ggplot2movies::movies to only include films after the made after 2000, and remove missing values from mpaa and budget

Code
movies_box <- ggplot2movies::movies |> 
                dplyr::filter(year > 2000 & 
                                mpaa != "" & 
                                    !is.na(budget))
head(movies_box)
# A tibble: 6 × 24
  title      year length budget rating votes    r1    r2    r3    r4    r5    r6
  <chr>     <int>  <int>  <int>  <dbl> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 100 Mile…  2002     98  1.1e6    5.6   181   4.5   4.5   4.5   4.5  14.5  24.5
2 13 Going…  2004     98  3.7e7    6.4  7859   4.5   4.5   4.5   4.5   4.5  14.5
3 15 Minut…  2001    120  4.2e7    6.1 10866   4.5   4.5   4.5   4.5  14.5  24.5
4 2 Fast 2…  2003    107  7.6e7    5.1  9556  14.5   4.5   4.5   4.5  14.5  14.5
5 2046       2004    129  1.2e7    7.6  2663   4.5   4.5   4.5   4.5   4.5   4.5
6 21 Grams   2003    124  2  e7    8   21857   4.5   4.5   4.5   4.5   4.5   4.5
# … with 12 more variables: r7 <dbl>, r8 <dbl>, r9 <dbl>, r10 <dbl>,
#   mpaa <chr>, Action <int>, Animation <int>, Comedy <int>, Drama <int>,
#   Documentary <int>, Romance <int>, Short <int>

Below we create a box plot of the length variable using the methods described above:

Code
labs_boxplot <- labs(
  title = "IMDB Movie information and user ratings",
  y = "Movie length (min)", x = "")
ggp2_boxplot <- ggplot(data = movies_box, 
           aes(x = " ", 
               y = length)) +
        geom_boxplot() 

ggp2_boxplot + 
  labs_boxplot

The table below shows the 25th percentile, the median, the 75th percentile, the IQR, and a histogram of the length variable from the movies_box dataset.

25th Median 75th IQR Histogram
92 100 113 21 ▁▇▅▁▁

The figure below displays how each element in the box plot represents each of the statistics using lines and points.

In ggplot2, values that fall more than 1.5 times the IQR are displayed as individual points (aka outliers). The lines extending from the bottom and top of the main box represent the last non-outlier value in the distribution.

Compare the geom_point(), geom_freqpoly(), geom_histogram(), and geom_density() graphs of length from movie_box below to the geom_boxplot():