iT邦幫忙

2025 iThome 鐵人賽

DAY 18
0

Density plot(密度圖) 是一種常見用來呈現資料分布的圖形。它透過 核密度估計(Kernel Density Estimation, KDE),將每個資料點估計為一條小曲線,最後再加總成一條平滑的分布曲線。影響曲線平滑程度的參數稱為 帶寬(bandwidth, bw),在 ggplot2 中可透過 geom_density(bw = ...) 來控制。

本篇範例資料來自 palmerpenguins 套件penguins,該資料包含三種企鵝(Adelie、Chinstrap、Gentoo)的體重、翅膀長度、喙部大小等連續變數。


不分種類的體重分布

若不考慮品種差異,可以看出企鵝體重主要集中在 3500–4000 g 之間。

ggplot(data = penguins_new,                                 
       aes(x = body_mass_g)) +
  geom_density()+
  scale_y_continuous(labels = percent_format(accuracy = 0.01))+
  scale_x_continuous(labels = comma)+
  labs(
    x = 'Weight(g)'
  )

https://ithelp.ithome.com.tw/upload/images/20250918/20177964KCj5Bw57bG.png

分種類的體重分布

若考慮品種差異,可以看到更清楚的分布差異:

  • Gentoo 企鵝整體體重分布較大,平均體重比 Adelie 與 Chinstrap 高出許多。
  • Adelie 與 Chinstrap 體重範圍重疊,但 Chinstrap 的分布更集中。
ggplot(data = penguins_new,                                 
       aes(x = body_mass_g,
           color = species)) +
  geom_density()+
  scale_y_continuous(labels = percent_format(accuracy = 0.01))+
  scale_x_continuous(labels = comma)+
  labs(
    x = 'Weight(g)'
  )

透過密度圖,我們可以直觀比較不同企鵝種類的體重差異,從中發現 Gentoo 體型明顯較大,而 Adelie 與 Chinstrap 則有高度相似性

https://ithelp.ithome.com.tw/upload/images/20250918/20177964m4fDaXKlhA.png


🔎 English Abstract

This article demonstrates the use of density plots with the palmerpenguins dataset in R. Density plots apply Kernel Density Estimation (KDE) to visualize the overall distribution of continuous variables. Using body mass data, we observe that penguin weights cluster between 3500–4000 g overall. When grouped by species, Gentoo penguins show higher body mass values, while Adelie and Chinstrap penguins overlap in range but Chinstrap exhibits a sharper concentration. Density plots provide an intuitive way to compare species-specific distributions.


上一篇
面對重疊資料的挑戰:Overplotting 的處理策略
下一篇
Histogram 與 Density Plot 互補呈現 - 以三種企鵝體重的分布狀況為例
系列文
資料視覺化的探索之旅:從 ggplot2 技術到視覺化設計22
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言