這系列文章截至目前為止,一步步從 ggplot2 的基礎圖形開始,期望分享ggplot2架構與資料視覺化的廣度與多樣性。
過程中,不只是單純「會畫圖」,且分享如何挑選合適的圖表來表達資料故事。
以下表格整理了我們已經介紹過的 主要 geom_xx 圖層 與其用途:
# 載入套件
library(ggplot2)
# 基本架構
ggplot(data = <DATA>,
aes(x = <X變數>, y = <Y變數>)) +
geom_<幾何圖層>() +
scale_x_continuous() +
facet_wrap(~ <分面變數>) +
theme_minimal() +
labs(title = "圖表標題",
x = "X軸標籤",
y = "Y軸標籤")
類型 | 代表語法 | 主要用途 / 特色 |
---|---|---|
散點圖 | geom_point() |
呈現兩個連續變數間的關係。 |
抖動圖 | geom_jitter() |
解決點重疊問題,讓分布更清楚。 |
點密度圖 | ggpointdensity::geom_pointdensity() |
以顏色強調密度高低。 |
折線圖 | geom_line() |
顯示時間序列或有序資料趨勢。 |
平滑線 | geom_smooth() |
添加回歸線或趨勢線。 |
直方圖 | geom_histogram() |
呈現連續數據的分布。 |
密度圖 | geom_density() |
平滑分布,補充直方圖不足。 |
頻數多邊形 | geom_freqpoly() |
以線條替代直方圖,便於比較。 |
盒鬚圖 | geom_boxplot() |
展示五數摘要與離群值。 |
小提琴圖 | geom_violin() |
呈現完整分布形狀,可加分位數線。 |
長條圖 | geom_col() |
類別數據的加總或聚合呈現。 |
計數條 | geom_bar() |
自動計數的長條圖。 |
熱圖 | geom_tile() |
二維交叉比較,用顏色表示大小。 |
圓餅 / 甜甜圈圖 | geom_bar() + coord_polar() |
類別占比的特殊呈現。 |
標籤 | geom_text() / geom_label() |
在圖上加註文字,輔助解讀。 |
除了幾何圖層,同時也採用了 輔助與修飾的語法:
position_jitter()
、position_dodge()
、position_nudge()
scale_x_continuous(labels=scales::comma)
、scale_fill_xx()
、scale_colour_xx()
theme()
微調,或用 from_theme()
統一設定facet_wrap()
、facet_grid()
幫助多組比較coord_flip()
、coord_polar()
提供不同視角annotate()
、guides()
、after_stat()
這些構成了 ggplot2 的核心能力,讓我們能靈活應對大部分的基礎需求。
雖然 ggplot2 本身功能強大,但在實務中常會遇到一些限制,以下場景就需要 ggplot2 extensions 的幫助:
geom_text()
無法處理複雜排版。Extensions 可以理解成「擴充包」,它們不是取代 ggplot2,而是 在原有框架上,提供更便捷或更進階的功能。
接下來的篇章,我將以 Spotify 提供的音樂特徵資料(Spotify Audio Features) 作為案例,搭配 ggplot2 extensions 展示新的可能性。
這份資料集包含了歌曲的多種屬性,例如:
這些數值本身就很適合做資料視覺化,特別是透過 ggplot2 extensions,我們能從新的角度來探索音樂的特徵與差異。
今天這篇文章是 承先啟後的關鍵一篇:
接下來,將會挑選一些適合的 extensions,實際示範如何讓視覺化更具延展性與故事感。
This article serves as a bridge in my Ironman series. Over the past 20+ days, I have explored a wide range of ggplot2 geoms—scatter plots, histograms, density plots, boxplots, violin plots, bar charts, heatmaps, pie charts—and supporting tools such as scales, themes, facets, positions, and annotations. Together, these form the foundation of data visualization in ggplot2.
However, real-world scenarios often demand more: animated graphics, smarter label placement, statistical integration, or flexible multi-plot layouts. This is where ggplot2 extensions come into play. They expand ggplot2’s capabilities while keeping the same grammar of graphics philosophy.
In the upcoming articles, I will introduce Spotify Audio Features as a new dataset, which describes each track’s characteristics such as popularity, energy, danceability, valence, loudness, and tempo. With this dataset, we will explore ggplot2 extensions to create more powerful, flexible, and storytelling-oriented visualizations.