# 安装包
if (!requireNamespace("plotrix", quietly = TRUE)) {
install.packages("plotrix")
}if (!requireNamespace("ggplotify", quietly = TRUE)) {
install.packages("ggplotify")
}
# 加载包
library(plotrix)
library(ggplotify)
三维饼图
注记
Hiplot 网站
本页面为 Hiplot 3D Pie
插件的源码版本教程,您也可以使用 Hiplot 网站实现无代码绘图,更多信息请查看以下链接:
三维饼图是指具有三维外观的饼图。
环境配置
系统: Cross-platform (Linux/MacOS/Windows)
编程语言: R
依赖包:
plotrix
;ggplotify
数据准备
载入数据为一个矩阵。
# 加载数据
<- read.delim("files/Hiplot/138-pie-3d-data.txt", header = T)
data
# 整理数据格式
colnames(data) <- c("Group", "Value")
$Value <- as.numeric(data$Value)
data<- data[data$Value != 0,]
data
# 查看数据
head(data)
Group Value
1 Group1 13
2 Group2 34
3 Group3 21
4 Group4 43
可视化
# 三维饼图
pie3D(data$Value, radius = 0.8, height = 0.05, theta = 0.8,
labels = paste(data$Group, "\n(n=", data$Value, ", ",
round(data$Value / sum(data$Value) * 100, 2), "%)",
sep = ""),
explode = 0.1, main = "", labelcex = 1, shade = 0.4, labelcol = "black",
col = c("#E64B35FF","#4DBBD5FF","#00A087FF","#3C5488FF"))

该图通过三维形式,更美观的展示了 1 ~ 4 组的样本数量及样本数量所对应的占比。一组样本数量 13,占比 11.71%,二组样本数量 34,占比30.63%,三组样本数量 21,占比 18.92%,四组样本数量 43,占比 38.74%。