Box plot guide

When should I use box plots?

A box plot compares medians and quartiles across groups. Its whisker rule must be stated; 1.5 × IQR is common.

On this page

For a small sample, I also show the observations. A box alone hides repeated values and sample size.

What does a working example look like?

The complete result below comes from the linked Bopbun example; its data and runnable code are included.

This article uses simulated example data.

Box plot — full output from the worked example. Plot labels remain in English across languages.
Box plot — full output from the worked example. Plot labels remain in English across languages.

How can I reproduce this example?

Save the code in a writable folder and run Rscript example.R with the dependencies installed. The recorded example environment is R 4.5.3 · ggplot2 4.0.4.

These are the code, data and result files used in this article. Choose a file below to run the example or compare your results.

  • example.RComplete example code to run on your computer.
  • synthetic.csvThe input data used in the example; open it as a table.
  • after.pngThe figure after applying the settings in this article.
  • after.pdfThe result in PDF format, for viewing and page layout.
  • Usage license · LICENSE.txtTerms for reusing the code, data and figures, with source and font notices.
Complete source code
# Original example: CC0-1.0.
library(ggplot2)
d <- data.frame(group = factor(c(rep("A", 5), rep("B", 9)), levels = c("A", "B")),
                value = c(1, 2, 2, 2, 20, 1, 2, 3, 4, 5, 6, 7, 8, 20))
base <- ggplot(d, aes(group, value)) +
  scale_x_discrete(labels = c(A = "A (n = 5)", B = "B (n = 9)")) +
  scale_y_continuous(breaks = seq(0, 20, 5)) +
  labs(x = "Group", y = "Response (arbitrary units)") +
  theme_classic(base_size = 11) +
  theme(plot.caption = element_text(size = 8), plot.margin = margin(8, 8, 8, 8))
before <- base + geom_boxplot(width = .45, fill = "white", outlier.colour = "#D55E00") +
  labs(caption = "Default boxplot: whiskers use 1.5 IQR")
hidden <- base + geom_boxplot(width = .45, fill = "white", outlier.shape = NA)
after <- hidden +
  geom_point(position = position_jitter(width = .12, height = 0, seed = 15),
             size = 2, colour = "#17665c") +
  labs(caption = "Boxplot + every observation; horizontal jitter only")
discarded <- base + geom_boxplot(outliers = FALSE)
b <- ggplot_build(before)
a <- ggplot_build(after)
cols <- c("ymin", "lower", "middle", "upper", "ymax")
expected <- data.frame(ymin = c(2, 1), lower = c(2, 3), middle = c(2, 5),
                       upper = c(2, 7), ymax = c(2, 8))
stopifnot(isTRUE(all.equal(b$data[[1]][cols], expected, check.attributes = FALSE)),
          identical(b$data[[1]][cols], a$data[[1]][cols]),
          identical(b$data[[1]]$outliers, list(c(1, 20), 20)),
          nrow(a$data[[2]]) == nrow(d), identical(a$data[[2]]$y, d$value))
summary <- data.frame(group = levels(d$group), n = c(5, 9), expected,
                      IQR = c(0, 4), lower_fence = c(2, -3), upper_fence = c(2, 13),
                      lower_whisker_length = c(0, 2), upper_whisker_length = c(0, 1),
                      outlier_values = c("1;20", "20"))
yrange <- function(plot) ggplot_build(plot)$layout$panel_scales_y[[1]]$range$range
ranges <- rbind(default = yrange(before), hidden = yrange(hidden),
                 discarded = yrange(discarded), raw_overlay = yrange(after))
colnames(ranges) <- c("minimum", "maximum")
stopifnot(identical(unname(ranges["default", ]), c(1, 20)),
          identical(unname(ranges["hidden", ]), c(1, 20)),
          identical(unname(ranges["discarded", ]), c(1, 8)),
          identical(unname(ranges["raw_overlay", ]), c(1, 20)))
ggsave("before.png", before, width = 1200, height = 900, units = "px", dpi = 300, bg = "white")
ggsave("after.png", after, width = 1200, height = 900, units = "px", dpi = 300, bg = "white")
ggsave("after.pdf", after, width = 101.6, height = 76.2, units = "mm", bg = "white")
write.csv(d, "synthetic.csv", row.names = FALSE)
write.csv(summary, "boxplot-summary.csv", row.names = FALSE)
write.csv(data.frame(option = rownames(ranges), ranges), "range-check.csv", row.names = FALSE)
capture.output(sessionInfo(), file = "session-info.txt")
print(summary)
print(ranges)
cat("PASS:14 values; A has zero-length whiskers; B ends at1/8; summaries unchanged; all raw y preserved; hide/discard ranges checked.\n")

Which detailed answer should I read next?

The worked answer explains the settings behind this result.

Boxplot whiskers and quartiles

Technical references: ggplot2 1

Sources & licenses