iT邦幫忙

2025 iThome 鐵人賽

DAY 3
0

在 ggplot2 的語法設計中,aesthetic(美學映射) 就像是數據的化妝術。

透過它,使用者可以決定資料變數如何轉換成圖表上的顏色、形狀、線條或位置。

沒有 aes(),圖表就像素顏般單調;有了 aes(),數據才真正展現出它的樣貌。

ggplot2 中,aes() 是最核心的設定,以下是常見的引數(arguments):

  • x, y → 決定座標軸使用的變數
  • color, shape, size, linetype → 決定視覺屬性
  • group → 決定哪些資料要被連接或分類

這些美學映射可以放在最外層 ggplot() 裡(全域設定),也可以放在 geom_xxx() 裡(局部設定),依情境自由運用。


短短幾行程式碼,畫出折線圖

以下範例展示如何用最精簡的程式碼,繪製「不同來源的電廠在各年度煤及煤產品耗用量」的折線圖。

https://ithelp.ithome.com.tw/upload/images/20250903/20177964WkktRYBzoE.jpg

資料來源為台灣經濟部能源署(https://www.esist.org.tw/),內容涵蓋 2011 年至 2024 年間煤及煤產品於發電用途上的耗用數據。

此處在 aes() 中映射:x = 年份(Year)y = 耗用量(Consumption,並換算為百萬單位)group = 發電來源(Source);再搭配 geom_line()geom_point(),即可呈現台電與民營電廠在 2011–2024 年間的耗用狀況。

ggplot(data = Cons,
       aes(x = Year,
           y = Consumption / 1e6,
           group = Source)) +
  geom_line() +
  geom_point() +
  scale_x_continuous(breaks = seq(2011, 2024, 1)) +
  labs(y = "Consumption (Million TOE / 10⁷ kcal)")


加入更多美學映射,更多可能性

延續上述例子,進一步加入 colorshape 的映射:

  • color = Source → 不同來源使用不同顏色區分
  • shape = Source → 點的形狀亦根據來源改變

https://ithelp.ithome.com.tw/upload/images/20250903/20177964d4tsbPw17q.jpg

這裡可以看到 aes() 同時出現在 ggplot() 主層geom_point() 子層,代表能針對不同圖層設定不同的美學屬性。

ggplot(data = Cons,
       aes(x = Year,
           y = Consumption / 1e6,
           group = Source,
           color = Source)) +
  geom_line() +
  geom_point(aes(shape = Source)) +
  scale_x_continuous(breaks = seq(2011, 2024, 1)) +
  labs(y = "Consumption (Million TOE / 10⁷ kcal)")


  • 台電(紅色線):煤及煤產品耗用量遠高於民營電廠,大致維持在 1,300–1,600 萬 TOE 之間;2017–2018 年出現明顯高峰,之後雖有波動,仍佔主要比例。
  • 民營電廠(藍色線):耗用量相對穩定,約落在 400–500 萬 TOE,變化幅度小,近年略有下降但整體平緩。

這張圖不僅清楚比較不同來源在 2011–2024 年 的耗用情況,也展現 aes(color = Source) 如何幫助我們直觀分辨資料來源。


為什麼要加 groupcolor

在這個例子裡,groupcolor 是關鍵:

  • group = Source

    告訴 ggplot2:不同來源(台電、民營電廠)的資料要各自連線。若缺少 group,所有點可能被視為同一組而錯接成一條「大亂線」。

  • color = Source

    在分組正確的前提下,再以顏色加強視覺辨識。若只有 group 沒有 color,雖能正確分線,但讀者難以快速區分來源。

簡單來說

  • group 解決「線要怎麼連

  • color 解決「誰是誰的辨識問題」

    兩者搭配,才能得到正確又好讀的折線圖。


小結

aes()數據與圖表的橋樑

  • 讓資料變成顏色、形狀、線型與位置等視覺元素;
  • 可在全域與局部靈活設定,提高繪圖彈性;
  • 提供多種能與 geom_xxx() 對應的映射(位置、顏色、大小、形狀、線型……),讓數據的故事更清晰。

延伸閱讀


English Summary

This article introduces aesthetic mapping (aes) in ggplot2 as the bridge between data and its visual expression. With aes(), variables are mapped to visual properties such as position (x, y), color, shape, size, and line type, while group determines how observations are connected or partitioned. Using coal and coal-product consumption for power generation in Taiwan (2011–2024) from the Bureau of Energy as an example, we show how a few lines of code produce a multi-series line chart. The first example maps years to the x-axis, consumption to the y-axis (scaled to millions), and groups by source to draw separate lines. The second example adds color and shape mappings to improve readability and quick identification of sources. The results indicate that Taiwan Power Company consistently consumes more coal than independent power producers, peaking around 2017–2018, while IPPs (Independent Power Producers) remain relatively stable. We emphasize why group and color matter: group ensures the correct connection of points within each source, and color provides immediate visual distinction. Together, they produce a clear, interpretable trend visualization that demonstrates how aesthetic mapping is essential for accurate and effective storytelling with data.


上一篇
基石
下一篇
堆疊 - Layered Plotting
系列文
資料視覺化的探索之旅:從 ggplot2 技術到視覺化設計5
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言