Learn R Programming  /  ggplot2 Graphics  /  Box-Whisker Plot

Box-Whisker Plot

geom_boxplot() is ggplot2's box-whisker layer โ€” the same five-number summary as base R's boxplot(), but themeable and easy to combine with other layers.

A Basic Box-Whisker Plot

R Code
library(ggplot2)

df <- data.frame(
  group = rep(c("A","B"), each = 5),
  score = c(55,60,65,70,62, 80,85,90,78,88)
)

ggplot(df, aes(x = group, y = score, fill = group)) +
  geom_boxplot() +
  labs(title = "Score by Group", x = NULL) +
  theme_minimal()

Removing the Legend When It Is Redundant

Since the group is already labelled on the x-axis, the fill legend just repeats information โ€” hide it.

R Code
ggplot(df, aes(x = group, y = score, fill = group)) +
  geom_boxplot(show.legend = FALSE)

Overlaying Individual Points with geom_jitter()

Showing the raw data points on top of the summary box builds trust in the chart, especially with small sample sizes.

R Code
ggplot(df, aes(x = group, y = score, fill = group)) +
  geom_boxplot(alpha = 0.6, show.legend = FALSE) +
  geom_jitter(width = 0.1, size = 2, colour = "black")

Advanced: Boxplot Ordered by Median

🚀 Advanced Example
Just like bar plots, boxplots read better when groups are ordered by a summary statistic rather than alphabetically.
R Code
df$group <- reorder(df$group, df$score, median)

ggplot(df, aes(x = group, y = score, fill = group)) +
  geom_boxplot(show.legend = FALSE) +
  labs(title = "Groups Ordered by Median Score")

Key Points

  • geom_boxplot() draws the same five-number summary as base R\'s boxplot(), styled with ggplot2 themes.
  • Hide a redundant legend with show.legend = FALSE when the grouping is already on an axis.
  • geom_jitter() overlays the individual data points next to (not hidden by) the box.
  • reorder(group, value, median) sorts boxes by a summary statistic for easier comparison.