箱形圖:選圖與判讀指南
箱形圖適合回答什麼問題?
箱形圖以中位數與四分位數比較組別;鬚線規則需明確,常用規則是 1.5 × IQR。
本文目錄
樣本少時我也會顯示原始點;單看箱體不容易看出重複值和樣本數。
實際執行的範例圖是什麼樣?
下方是本站對應範例實際產生的完整結果,附輸入資料與可執行程式。
本文使用模擬示範資料。

如何重現這個範例?
將程式存入可寫入目錄,安裝所需套件後執行 Rscript example.R。原例實測環境:R 4.5.3 · ggplot2 4.0.4。
以下是本文使用的程式碼、資料與結果檔案。你可以下載程式碼跟著操作,也可以開啟結果檔案,對照自己執行後的結果。
- example.R可在電腦上執行的完整範例程式碼。
- synthetic.csv本例使用的輸入資料,可用表格開啟。
- after.png依本文設定產生的結果圖。
- after.pdfPDF 格式的結果圖,方便檢視與排版。
- 使用授權 · LICENSE.txt說明程式碼、資料與圖表的使用條件,並附來源及字型授權。
完整原始程式
# 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")
接著應該查看哪篇具體解答?
具體解答說明這張結果圖對應的參數設定。
詳細操作解答目前提供英文、簡中與日文;下列連結開啟英文版。
Boxplot whiskers and quartiles
技術依據: ggplot2 1
從零開始學習R語言

