# ggplot如何構造連環餅圖
## 前言
在數據可視化領域,餅圖(Pie Chart)是一種常見的展示比例關系的圖表類型。而"連環餅圖"(Nested Pie Chart或Donut Chart with Layers)則是在傳統餅圖基礎上發展出的進階形式,它通過多層環形結構展示數據的層級關系,適用于呈現具有父子結構的分類數據。
本文將詳細介紹如何使用R語言中的ggplot2包構建連環餅圖,涵蓋數據準備、基礎餅圖繪制、環形圖轉換、多層嵌套實現以及高級美化的完整流程。文章包含以下核心內容:
1. 理解連環餅圖的數據結構要求
2. 使用geom_col()+coord_polar()創建基礎餅圖
3. 通過調整參數轉換為環形圖(甜甜圈圖)
4. 實現多層嵌套的連環餅圖
5. 顏色、標簽、圖例等視覺元素的優化
6. 交互式連環餅圖的實現方案
---
## 一、數據結構準備
連環餅圖需要特定的數據組織形式。理想的數據結構應包含:
- 分類變量(至少兩個層級)
- 數值變量(用于確定扇形角度)
- 層級標識變量
### 示例數據集構造
```r
library(tidyverse)
# 創建示例數據
set.seed(123)
hierarchical_data <- tibble(
category_l1 = rep(c("A", "B", "C"), each = 4),
category_l2 = paste0(rep(c("X", "Y", "Z", "W"), 3), 1:12),
value = sample(10:50, 12, replace = TRUE)
) %>%
group_by(category_l1) %>%
mutate(prop_l1 = sum(value)) %>%
ungroup() %>%
mutate(prop_l2 = value)
該數據結構包含: - 一級分類(category_l1) - 二級分類(category_l2) - 原始數值(value) - 計算得到的一級比例(prop_l1) - 二級比例(prop_l2)
ggplot2本身沒有直接的geom_pie()
函數,但可以通過geom_col()
+coord_polar()
組合實現:
basic_pie <- ggplot(hierarchical_data, aes(x = "", y = value, fill = category_l2)) +
geom_col(width = 1) +
coord_polar(theta = "y") +
theme_void()
print(basic_pie)
x = ""
:固定x軸為單一值theta = "y"
:指定使用y值作為角度基準width = 1
:確保條形寬度填滿整個半徑將餅圖轉為環形圖(甜甜圈圖)的核心是調整x軸映射:
donut_plot <- ggplot(hierarchical_data, aes(x = 2, y = value, fill = category_l2)) +
geom_col(width = 1) +
coord_polar(theta = "y") +
xlim(0.5, 2.5) + # 關鍵調整:控制內徑和外徑
theme_void()
print(donut_plot)
xlim(0.5, 2.5)
:0.5決定空心部分大小構建多層連環餅圖需要巧妙組合多個幾何對象:
nested_pie <- ggplot() +
# 第一層(外層)
geom_col(
data = hierarchical_data,
aes(x = 1.8, y = prop_l1, fill = category_l1),
width = 0.8
) +
# 第二層(內層)
geom_col(
data = hierarchical_data,
aes(x = 1.0, y = value, fill = category_l2),
width = 0.4
) +
coord_polar(theta = "y") +
scale_fill_manual(
values = c(
RColorBrewer::brewer.pal(3, "Set1"),
RColorBrewer::brewer.pal(12, "Paired")
)
) +
theme_void() +
theme(legend.position = "right")
print(nested_pie)
nested_pie_with_labels <- nested_pie +
# 外層標簽
geom_text(
data = hierarchical_data %>%
group_by(category_l1) %>%
summarize(ypos = sum(value)/2),
aes(x = 1.8, y = ypos, label = category_l1),
size = 5, color = "white"
) +
# 內層標簽
geom_text(
data = hierarchical_data %>%
mutate(ypos = cumsum(value) - value/2),
aes(x = 1.0, y = ypos, label = category_l2),
size = 3
)
print(nested_pie_with_labels)
nested_pie +
guides(fill = guide_legend(
title = "分類層級",
ncol = 2,
override.aes = list(size = 4)
)) +
theme(
legend.title = element_text(face = "bold"),
legend.text = element_text(size = 10)
)
使用plotly增強交互性:
library(plotly)
ggplotly(nested_pie_with_labels, tooltip = c("fill", "y"))
sales_data <- tibble(
region = rep(c("North", "South", "East", "West"), each = 3),
product = paste0(rep(c("Basic", "Pro", "Premium"), 4), "_", region),
revenue = c(120, 180, 250, 90, 130, 200, 80, 110, 160, 150, 210, 300)
)
ggplot(sales_data) +
geom_col(aes(x = 2, y = revenue, fill = region), width = 0.6) +
geom_col(aes(x = 1, y = revenue, fill = product), width = 0.4) +
coord_polar(theta = "y") +
xlim(0.5, 2.5) +
labs(title = "Regional Sales by Product Tier") +
theme_void()
time_data <- tibble(
year = rep(2020:2022, each = 4),
quarter = paste0("Q", rep(1:4, 3)),
value = c(15, 22, 18, 25, 17, 24, 20, 28, 20, 30, 25, 35)
)
ggplot(time_data) +
geom_col(aes(x = 2, y = value, fill = factor(year)), width = 0.7) +
geom_col(aes(x = 1, y = value, fill = quarter), width = 0.5) +
coord_polar(theta = "y") +
scale_fill_viridis_d(option = "plasma") +
xlim(0.5, 2.5)
A:可使用ggrepel
包或手動調整標簽位置:
library(ggrepel)
# 在geom_text中使用geom_text_repel替代
A:確保數據中的因子水平正確設置:
data$category <- factor(data$category, levels = c("A", "B", "C"))
A:使用annotate:
+ annotate("text", x = 0, y = 0, label = "核心指標", size = 8)
連環餅圖作為一種信息密集型的可視化形式,在展現層級結構數據時具有獨特優勢。通過ggplot2的靈活組合,我們可以構建出既美觀又富有洞察力的多層環形可視化。關鍵要把握:
隨著ggplot2生態的不斷發展(如ggforce等擴展包),連環餅圖的實現方式還將繼續豐富。建議讀者在實踐中多嘗試不同參數組合,開發出更適合特定場景的可視化方案。 “`
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。