ggplot2 的語法基於 「圖形語法(Grammar of Graphics)」 的理論基礎,讓使用者可以用組合的方式建構圖表。
它的生態系非常完整,延伸套件眾多,本系列文章也會介紹一些延伸套件,幫助大家在描述數據時,能更全面、無死角地進行繪圖。
在 ggplot2 中,每一張圖的誕生,都源自於 Grammar of Graphics。基本組成元素包含:
接下來,將以 鳶尾花 (iris) 資料為例,透過花瓣寬度 (Petal.Width) 與種類 (Species) 的變數,分別展示 ggplot2 各元素的作用。
只指定資料來源,尚未定義映射或幾何層,所以不會畫出圖形。
ggplot(data = iris)
指定要把哪個變數映射到哪個視覺屬性;這裡先把 Petal.Width 映射到 y 軸。
ggplot(data = iris,
aes(y = Petal.Width))
加上幾何物件(這裡用箱型圖)後,才會真的畫出圖形。
ggplot(data = iris,
aes(y = Petal.Width)) +
geom_boxplot()
按 Species 分面,把不同品種的分佈拆成多個小圖,方便比較。
ggplot(data = iris,
aes(y = Petal.Width)) +
geom_boxplot() +
facet_wrap(~ Species)
疊加統計轉換;這裡用 stat_summary() 在每個面板上標出平均值(紅點)。
ggplot(data = iris,
aes(y = Petal.Width)) +
geom_boxplot() +
facet_wrap(~ Species) +
stat_summary(aes(x = 0),
fun = mean,
geom = "point",
color = "red",
size = 3)
翻轉座標,把原本的直向箱型圖改為橫向呈現,常用來改善長標籤的可讀性。
ggplot(data = iris,
aes(y = Petal.Width)) +
geom_boxplot() +
facet_wrap(~ Species) +
stat_summary(aes(x = 0),
fun = mean,
geom = "point",
color = "red",
size = 3) +
coord_flip()
套用主題(這裡用 theme_bw())來統一背景、格線、字體等整體風格。
ggplot(data = iris,
aes(y = Petal.Width)) +
geom_boxplot() +
facet_wrap(~ Species) +
stat_summary(aes(x = 0),
fun = mean,
geom = "point",
color = "red",
size = 3) +
theme_bw()
透過 iris 的例子,我們逐一看到 ggplot2 的七大核心元素 如何在圖形構建過程中逐步發揮作用。這些元素就像積木一樣,可以自由組合,讓我們在面對複雜資料時,依舊能以清晰而靈活的方式呈現。未來幾天的內容,將繼續探索更多的圖形內容與延伸套件,帶大家進一步了解ggplot2 的應用。
The foundation of ggplot2 lies in the Grammar of Graphics, which provides a systematic way to build plots by combining different layers. In this article, we explored the seven essential components of ggplot2: Data, Aesthetics, Geometries, Facets, Statistics, Coordinates, and Theme. Each element plays a unique role: Data supplies the raw information, Aesthetics maps variables to visual attributes, and Geometries define the shapes used to represent data. Facets allow us to split data into smaller panels for comparison, while Statistics enable additional transformations such as calculating means or adding smooth lines. Coordinates control how data is displayed in different systems, and Themes adjust the overall style and readability of the plots.
Using the classic iris dataset, we demonstrated step by step how each of these elements contributes to plot construction. Starting from specifying the dataset, we gradually added layers such as boxplots, facets by species, statistical summaries with mean points, flipped coordinates for better readability, and finally a clean theme. Together, these layers highlight the flexibility of ggplot2, making it a powerful tool for transforming data into meaningful visual stories.