iT邦幫忙

2025 iThome 鐵人賽

DAY 14
0
自我挑戰組

資料視覺化的探索之旅:從 ggplot2 技術到視覺化設計系列 第 14

最新ggplot2 4.0.0釋出啦! 更新摘要與使用心得

  • 分享至 

  • xImage
  •  

ggplot2 在今年 9 月,推出了 4.0.0 重大更新版本,除了迎來 18 歲生日,也帶來了相當多結構性的更新。這篇文章我會先整理官方的重點,再分享一些我實測與觀察到的影響。

  • 物件系統更新至 S7:增加防呆機制
  • theme 一致化調整/進階細微調整:彈性調整主題元素
  • 增加許多主題快捷函數:對於圖形細節,例如軸線、顏色、label 有更多選擇與客製化的空間

👉 官方完整公告:ggplot2 4.0.0 release note


1. 底層物件導向:從 S3 到 S7

ggplot2 許多社群提到最大的改動,就是 ggplot2 把許多物件系統轉為 S7

  • 對一般使用者:最直接的感受是型別檢查更嚴謹,像是 element_text(hjust = "foo") 這類錯誤會即時被擋下。
  • 對擴充套件開發者:S7 支援 double dispatch,未來設計新的 geoms 或 theme element 會更彈性。

💡 過去因為小小拼錯字或參數誤用,最後 debug 半天。S7 的改進對避免這種低階錯誤會很有幫助。


2. 主題(theme)的全新控制

theme 一直是 ggplot2 控制「非資料」樣式的核心。4.0.0 帶來幾個亮點:

  • ink / paper / accent:把前景、背景與強調色分開定義。
  • layer 預設樣式統一化:可以用 theme(geom)element_geom() 統一調整。
  • 快速函數:像 theme_sub_axis() 讓調整更直覺。

範例程式碼:

ggplot(mpg, aes(class, displ)) +
  geom_boxplot(aes(colour = from_theme(accent))) +
  theme(geom = element_geom(accent = "tomato", paper = "cornsilk"))


https://ithelp.ithome.com.tw/upload/images/20250914/201779649ojXOiohCn.png

3. 色板與比例尺(Palettes & Scales)

過去 ggplot2 的顏色控制有時候比較零散。現在 palette 系統被納入 theme 管理,例如 palette.colour.continuouspalette.shape.discrete,可以確保整張圖的色彩搭配一致。

範例:

ggplot(mpg, aes(displ, hwy, shape = drv, colour = cty)) +
  geom_point() +
  theme(
    palette.colour.continuous = c("chartreuse", "forestgreen"),
    palette.shape.discrete = c("triangle", "triangle open", "triangle down open")
  )

https://ithelp.ithome.com.tw/upload/images/20250914/20177964w2Ku7QJl1D.png


4. 新的快捷函數

在過去,theme() 常讓人覺得「參數太多、名稱太長、結構混亂」。例如 axis.minor.ticks.length.x.bottom

ggplot2 4.0.0 引入了一組 helper functions

theme_sub_axis()
theme_sub_axis_x()
theme_sub_axis_bottom()
theme_sub_axis_top()
theme_sub_axis_y()
theme_sub_axis_left()
theme_sub_axis_right()
theme_sub_legend()
theme_sub_panel()
theme_sub_plot()
theme_sub_strip()

這些函數的用途,就是把原本冗長的參數,拆解成 更清晰的子模組

例如:

  • theme_sub_axis_bottom():設定 x 軸底部(如刻度、次刻度)。
  • theme_sub_legend():處理圖例樣式。
  • theme_sub_panel():處理繪圖面板。

範例:

https://ithelp.ithome.com.tw/upload/images/20250914/201779645DCBCooftb.png

p1 <- ggplot(mpg, aes(drv, hwy, colour = factor(year))) +
  geom_point(position = "jitterdodge") +
  guides(colour = "none") +
  scale_x_discrete(
    minor_breaks = scales::breaks_width(1, offset = 0.5),
    guide = guide_axis(minor.ticks = TRUE)
  )

p2 <- p1 +
  theme(panel.grid.major.x = element_blank()) +
  theme_sub_axis_bottom(ticks = element_blank(), minor.ticks = element_line())

p1 | p2


小結

這次 ggplot2 4.0.0 的更新,我覺得有三個關鍵價值:

  1. 更安全:S7 型別檢查避免了許多常見錯誤。
  2. 更一致:theme 與 palette 統一管理,圖表外觀的可控性更高。
  3. 更細緻:圖形細節調整的自由度提升。

雖然不是每項功能都是日常繪圖必須,但客製化空間大幅提升,讓 ggplot2 更專業、更穩定、更易於維護


🔎 English Abstract

The release of ggplot2 4.0.0 marks a major milestone as the package celebrates its 18th anniversary. This update introduces significant structural improvements. The most notable change is the transition from the S3 to the S7 object system, which enforces stricter type checking and supports double dispatch. For everyday users, this reduces common mistakes such as incorrect argument types, while developers gain more flexibility in extending geoms and themes.

Another key area of improvement lies in themes. The new release introduces the concepts of ink, paper, and accent colors, allowing users to distinguish foreground, background, and highlight elements. The theme(geom) and element_geom() functions centralize the styling of geoms, while the new helper functions (e.g., theme_sub_axis_bottom, theme_sub_legend) simplify the previously chaotic theme declarations, replacing lengthy argument names with structured modules.

Additionally, palettes and scales are now integrated into theme management, ensuring consistent styling across layers. Minor breaks are also available for discrete scales, providing more fine-tuned axis control.

In summary, ggplot2 4.0.0 offers three core benefits: greater safety through stricter checks, more consistency in styling, and finer granularity for customization, making the package more professional, stable, and maintainable.


上一篇
選擇適合展現資料的圖形 - 圖形分類總覽
下一篇
Box Plot — 富含統計訊息的經典圖形
系列文
資料視覺化的探索之旅:從 ggplot2 技術到視覺化設計15
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言