ggplot2::geom_boxplot()
   get_help() docs


Description

The geom_boxplot() function is part of the {ggplot2} package, which is part of the {tidyverse}.

The geom_boxplot() function is used within {ggplot2} plots to create a boxplot, which visualizes summary statistics of a numeric continuous variable collapsed into its summary statistics: minimum, median, maximum, 1st and 3rd quartiles, with any outliers shown beyond minimum and maximum. Boxplots are often used to compare different distributions across a categorical variable.

Required aesthetics:

  • x: A categorical variable to visualize boxplots across
  • y: A numeric variable to visualize distributions of
    • Note that these can be switched for a horizontal boxplot

Commonly used optional aesthetics:

  • color (colour): The outline color for the boxplot
  • fill: The fill for the boxplot
  • size: The width of the boxplot outlines
  • outlier.size: The size of outlier points
  • outlier.color (outlier.colour): The color of outlier points
  • outlier.shape: The shape of outlier points
  • outlier.fill: The fill of outlier points, when the shape is specified to allow a fill

To use this function, you need to either first load the {ggplot2} library, or always use the function with ggplot2::geom_boxplot() notation.

# Load the library
library(ggplot2)
# Or, load the full tidyverse:
library(tidyverse)

# Or, use :: notation
ggplot2::geom_boxplot()

Examples

The examples below use the msleep dataset. Learn more about this dataset with get_help("msleep").

# Show the msleep dataset with head()
head(msleep)
## # A tibble: 6 × 11
##   name  genus vore  order conservation sleep_total sleep_rem sleep_cycle awake  brainwt  bodywt
##   <chr> <chr> <chr> <chr> <chr>              <dbl>     <dbl>       <dbl> <dbl>    <dbl>   <dbl>
## 1 Owl … Aotus omni  Prim… <NA>                17         1.8      NA       7    0.0155    0.48 
## 2 Moun… Aplo… herbi Rode… nt                  14.4       2.4      NA       9.6 NA         1.35 
## 3 Grea… Blar… omni  Sori… lc                  14.9       2.3       0.133   9.1  0.00029   0.019
## 4 Cow   Bos   herbi Arti… domesticated         4         0.7       0.667  20    0.423   600    
## 5 Thre… Brad… herbi Pilo… <NA>                14.4       2.2       0.767   9.6 NA         3.85 
## 6 Nort… Call… carni Carn… vu                   8.7       1.4       0.383  15.3 NA        20.5


# Distribution of awake across vore categories
# Note to remove the NAs, you must manipulate the data first
ggplot(msleep) + 
  aes(x = vore, y = awake) + 
  geom_boxplot()


# Distribution of awake across vore categories with all boxplots filled the same
ggplot(msleep) + 
  aes(x = vore, y = awake) + 
  geom_boxplot(fill = "orange")


# Distribution of awake across vore categories, filled by vore
ggplot(msleep) + 
  aes(x = vore, y = awake, fill = vore) + 
  geom_boxplot()


# Distribution of awake across vore categories, filled by vore, with customized outliers
ggplot(msleep) + 
  aes(x = vore, y = awake, fill = vore) + 
  geom_boxplot(outlier.size = 0.5, outlier.color = "red")


# To visualize a single boxplot for all values, use x = "" instead of providing a variable
ggplot(msleep) + 
  aes(x = "", y = awake) + 
  geom_boxplot()