iT邦幫忙

2025 iThome 鐵人賽

DAY 2
0

ggplot2 的基本語法結構

ggplot2 的語法基於 「圖形語法(Grammar of Graphics)」 的理論基礎,讓使用者可以用組合的方式建構圖表。

它的生態系非常完整,延伸套件眾多,本系列文章也會介紹一些延伸套件,幫助大家在描述數據時,能更全面、無死角地進行繪圖。


ggplot2 的核心概念

在 ggplot2 中,每一張圖的誕生,都源自於 Grammar of Graphics。基本組成元素包含:

  • Data(資料):提供視覺化所需的原始資料
  • Aesthetics(美學映射):定義變數如何對應到視覺屬性,例如 x 軸、y 軸、顏色或大小
  • Geometries(幾何物件):呈現資料的幾何圖形,例如點、線、長條
  • Facets(分面):根據變數將圖表切割成多個小圖,方便比較
  • Statistics(統計轉換):對資料進行統計轉換,例如計數、加上平滑線、顯示密度分布
  • Coordinates(座標系統):定義資料如何映射到圖表的座標系統,例如直角座標或極座標
  • Theme(主題樣式):控制整體圖表的外觀,例如背景、字體、格線與風格

使用 iris 資料集進行展示

接下來,將以 鳶尾花 (iris) 資料為例,透過花瓣寬度 (Petal.Width) 與種類 (Species) 的變數,分別展示 ggplot2 各元素的作用。

Data(指定資料集)

只指定資料來源,尚未定義映射或幾何層,所以不會畫出圖形。

ggplot(data = iris)


Aesthetics(建立美學映射)

指定要把哪個變數映射到哪個視覺屬性;這裡先把 Petal.Width 映射到 y 軸。

ggplot(data = iris,
       aes(y = Petal.Width))


Geometries(加入幾何圖層)

加上幾何物件(這裡用箱型圖)後,才會真的畫出圖形。

ggplot(data = iris,
       aes(y = Petal.Width)) +
  geom_boxplot()


Facets(分面比較)

按 Species 分面,把不同品種的分佈拆成多個小圖,方便比較。

ggplot(data = iris,
       aes(y = Petal.Width)) +
  geom_boxplot() +
  facet_wrap(~ Species)


Statistics(統計摘要)

疊加統計轉換;這裡用 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)


Coordinates(座標系統)

翻轉座標,把原本的直向箱型圖改為橫向呈現,常用來改善長標籤的可讀性。

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(主題樣式)

套用主題(這裡用 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 的應用。


English Summary

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.


上一篇
啟程
下一篇
化妝術 - Aesthetic 映射:數據與圖形的橋樑
系列文
資料視覺化的探索之旅:從 ggplot2 技術到視覺化設計4
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言