iT邦幫忙

2025 iThome 鐵人賽

DAY 15
0

在統計分析的情境中,箱型圖(Box Plot) 幾乎是最常見的圖形之一。它能以簡潔的圖像,同時呈現資料的集中趨勢離散程度:包含箱體高低(IQR)、箱體上下緣(Q1 與 Q3)、箱內的中位數線(Median)、以及延伸出去的鬚(Whiskers)與末端小橫線(Staples)。因此,無論在期刊或一般文章,Box Plot 的出鏡率都非常高。本文以 ggplot2 內建的 diamonds 資料集為例,透過 Box Plot 觀察 Carat(克拉)Price(價格) 的關係。


資料處理:依克拉分三類

先將鑽石依克拉數分成 <1、1–2、>2 三組(方便比較不同克拉的價格分布)。

library(tidyverse)

data(diamonds)

diam_cuts <- diamonds %>%
  mutate(carat_group = cut(
    carat,
    breaks = c(-Inf, 1, 2, Inf),
    labels = c("<1", "1-2", ">2"),
    right = TRUE
  ))


基本 Box Plot(不分組)

先看整體價格分布。因為此圖沒有指定 x,所有資料會視為同一組集中呈現,能快速感覺到價格的大致落點與極端值情況。

library(ggplot2)
library(scales)  # 用於 y 軸 comma 標籤

ggplot(diam_cuts, aes(y = price)) +
  geom_boxplot() +
  scale_y_continuous(labels = comma) +
  labs(x = NULL, y = "Price (USD)")

註:此處僅提供「整體」分布輪廓;價格受到多種因素影響(切工、顏色、淨度與克拉等),尚未分層比較前,解讀以概覽為主。

https://ithelp.ithome.com.tw/upload/images/20250915/20177964c8fYBOjpsd.png


加入克拉分組:價格中位數隨克拉上升

引入 carat_group 之後,可以清楚看到不同克拉區間的價格分布差異;克拉越高,中位數價格越高,而且高價的長尾也更明顯。

ggplot(diam_cuts, aes(x = carat_group, y = price, fill = carat_group)) +
  geom_boxplot() +
  scale_y_continuous(labels = comma) +
  labs(x = "Carat Group", y = "Price (USD)") +
  guides(fill = "none")

https://ithelp.ithome.com.tw/upload/images/20250915/20177964LB1AP3buqP.png


疊加「平均數」:與中位數的互補觀察

中位數(Median)較不受極端值影響;平均數(Mean)則會被長尾拉動。兩者一起看,更能掌握分布的偏態與尾部狀況。

ggplot(diam_cuts, aes(x = carat_group, y = price, fill = carat_group)) +
  geom_boxplot() +
  stat_summary(fun = mean, geom = "point",
               color = "orange", size = 4, shape = 18) +
  scale_y_continuous(labels = comma) +
  labs(x = "Carat Group", y = "Price (USD)") +
  guides(fill = "none")

https://ithelp.ithome.com.tw/upload/images/20250915/20177964pgGjC7XaVn.png


細緻控制 Box Plot 組件(ggplot2 4.x)

在新版4.0.0 ggplot2 中,箱型圖的各組件可分別調整(如鬚線、箱體邊框、中位數線、鬚末端小橫線 staples 等),有助教學或出版時做更清晰的視覺標示。

ggplot(diam_cuts, aes(x = carat_group, y = price, colour = carat_group)) +
  geom_boxplot(
    whisker.linetype = "dashed",  # 鬚線線型
    box.colour      = "red",      # 箱體邊框顏色
    median.linewidth= 2,          # 中位數線寬
    staplewidth     = 0.5,        # 鬚末端小橫線寬度
    staple.colour   = "grey50"    # 鬚末端小橫線顏色
  ) +
  stat_summary(fun = mean, geom = "point",
               color = "orange", size = 4, shape = 18) +
  scale_y_continuous(labels = comma) +
  labs(x = "Carat Group", y = "Price (USD)") +
  guides(colour = "none")

https://ithelp.ithome.com.tw/upload/images/20250915/20177964vrVz5ImMON.png


Box Plot 各元素意義速查

  • 箱體(Box):從 Q1Q3,高度為 IQR = Q3 − Q1
  • 中位數線(Median):箱體中的一條橫線,代表 Q2
  • 鬚(Whiskers):從箱體往外延伸,直到位於 Q1 − 1.5×IQR 與 Q3 + 1.5×IQR 範圍內的最小/最大「實際觀測值」
  • 鬚的末端小橫線(Staples):鬚線最外端的小橫線,標記「到這裡為止」的邊界位置(對應到上面規則中的實際值,不一定等於精算的 Q1 − 1.5×IQR 或 Q3 + 1.5×IQR)。
  • 離群點(Outliers):超出上述範圍的觀測值,通常以點標示。

小結

  • Box Plot 能以極小的版面,同時呈現位置與變異,是探索性資料分析(EDA)的得力工具。
  • diamonds 範例中,克拉數越高,價格中位數整體越高,分布也更拉長。
  • 中位數 + 平均數 的雙指標視角,有助於辨識偏態與長尾。
  • 新版 ggplot2 對組件的細緻參數控制,讓教學、報告與出版的圖形更易讀、更專業

🔎 English Abstract

This post demonstrates how box plots concisely encode rich summary statistics, using the diamonds dataset to explore the relationship between Carat and Price. We first show an overall box plot to reveal the global distribution of prices. Next, we group diamonds into three carat ranges (<1, 1–2, >2) and plot prices by group. The result highlights a clear pattern: as carat increases, the median price rises, and distributions become more skewed with heavier upper tails. To complement the median, we overlay the mean to illustrate how long tails influence average values relative to the median. We also leverage recent enhancements in ggplot2 that allow fine-grained styling of box plot components—including whiskers, box outlines, median lines, and the small terminal bars (“staples”)—which makes didactic and publication graphics clearer and more consistent. Finally, we recap the interpretation of each box plot element: the IQR box, median line, whiskers, staples, and outliers beyond 1.5×IQR from the quartiles. Overall, box plots provide a compact and effective way to compare distributions across groups and to communicate central tendency and variability at a glance.


上一篇
最新ggplot2 4.0.0釋出啦! 更新摘要與使用心得
系列文
資料視覺化的探索之旅:從 ggplot2 技術到視覺化設計15
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言