# 安装包
if (!requireNamespace("data.table", quietly = TRUE)) {
install.packages("data.table")
}
if (!requireNamespace("jsonlite", quietly = TRUE)) {
install.packages("jsonlite")
}
if (!requireNamespace("patchwork", quietly = TRUE)) {
install.packages("patchwork")
}
if (!requireNamespace("ggplotify", quietly = TRUE)) {
install.packages("ggplotify")
}
if (!requireNamespace("cowplot", quietly = TRUE)) {
install.packages("cowplot")
}
# 加载包
library(data.table)
library(jsonlite)
library(patchwork)
library(ggplotify)
library(cowplot)分组饼图
注记
Hiplot 网站
本页面为 Hiplot Pie Group 插件的源码版本教程,您也可以使用 Hiplot 网站实现无代码绘图,更多信息请查看以下链接:
环境配置
系统: Cross-platform (Linux/MacOS/Windows)
编程语言: R
依赖包:
data.table;jsonlite;patchwork;ggplotify;cowplot
sessioninfo::session_info("attached")─ Session info ───────────────────────────────────────────────────────────────
setting value
version R version 4.5.2 (2025-10-31)
os Ubuntu 24.04.3 LTS
system x86_64, linux-gnu
ui X11
language (EN)
collate C.UTF-8
ctype C.UTF-8
tz UTC
date 2026-01-28
pandoc 3.1.3 @ /usr/bin/ (via rmarkdown)
quarto 1.8.27 @ /usr/local/bin/quarto
─ Packages ───────────────────────────────────────────────────────────────────
package * version date (UTC) lib source
cowplot * 1.2.0 2025-07-07 [1] RSPM
data.table * 1.18.0 2025-12-24 [1] RSPM
ggplotify * 0.1.3 2025-09-20 [1] RSPM
jsonlite * 2.0.0 2025-03-27 [1] RSPM
patchwork * 1.3.2 2025-08-25 [1] RSPM
[1] /home/runner/work/_temp/Library
[2] /opt/R/4.5.2/lib/R/site-library
[3] /opt/R/4.5.2/lib/R/library
* ── Packages attached to the search path.
──────────────────────────────────────────────────────────────────────────────
数据准备
# 加载数据
data <- data.table::fread(jsonlite::read_json("https://hiplot.cn/ui/basic/pie-group/data.json")$exampleData$textarea[[1]])
data <- as.data.frame(data)
# 整理数据格式
data[,"genre"] <- factor(data[,"genre"], levels = unique(data[,"genre"]))
data[,"mpaa"] <- factor(data[,"mpaa"], levels = unique(data[,"mpaa"]))
# 查看数据
head(data) title year length budget rating
1 Shawshank Redemption, The 1994 142 25 9.1
2 Lord of the Rings: The Return of the King, The 2003 251 94 9.0
3 Lord of the Rings: The Fellowship of the Ring, The 2001 208 93 8.8
4 Lord of the Rings: The Two Towers, The 2002 223 94 8.8
5 Pulp Fiction 1994 168 8 8.8
6 Schindler's List 1993 195 25 8.8
votes mpaa genre
1 149494 R Drama
2 103631 PG-13 Action
3 157608 PG-13 Action
4 114797 PG-13 Action
5 132745 R Drama
6 97667 R Drama
可视化
# 分组饼图
col <- c("#E64B35FF","#4DBBD5FF","#00A087FF","#3C5488FF","#F39B7FFF","#8491B4FF",
"#91D1C2FF","#DC0000FF","#7E6148FF","#B09C85FF")
plist <- list()
for (i in 1:length(unique(data[,"mpaa"]))) {
data_tmp <- data[data[,"mpaa"] == unique(data[,"mpaa"])[i],]
x <- table(data_tmp[,"genre"])
ptmp <- as.ggplot(function(){
par(oma=c(0,0,0,0))
pie(x,
labels = sprintf("%s\n(n=%s, %s%%)", names(x), x,
round(x / sum(x) * 100, 0)),
col = col,
main = paste0("mpaa", ":", unique(data[,"mpaa"])[i]),
edges = 200,
radius = 0.8,
clockwise = F
)
})
plist[[i]] <- ptmp
}
plot_grid(plotlist = plist, ncol = 2)
