3D饼图

饼图是一种常见的数据可视化图表,适用于展示数据的分布情况,尤其是用于比较各部分与整体的关系。本文展示使用plotrix包中的pie3D函数绘制3D饼图。

生成模拟数据并保存到指定目录

set.seed(123)  # 设置随机种子,保证结果可重复

# 定义细胞类型和组别
cell_types <- c("Type1", "Type2", "Type3", "Type4", "Type5")
groups <- c("BM", "GM")

# 创建模拟数据框
simulated_data <- data.frame(
  group = rep(groups, each = length(cell_types)),
  celltype = rep(cell_types, times = length(groups)),
  frequency = sample(50:150, length(cell_types) * length(groups), replace = TRUE)
)

# 保存模拟数据为RDS文件
saveRDS(simulated_data, "D:/干活还电脑/R语言/Downloads/simulated_human_data.rds")

 生成的模拟数据如下:

> simulated_data
   group celltype frequency
1     BM    Type1        80
2     BM    Type2       128
3     BM    Type3       100
4     BM    Type4        63
5     BM    Type5       116
6     GM    Type1        91
7     GM    Type2        99
8     GM    Type3        92
9     GM    Type4       150
10    GM    Type5        63

读取模拟数据并整理

# 加载必要的库
#install.packages("Seurat")
#install.packages("plotrix")
library(Seurat)
library(plotrix)  # 用于绘制3D饼图

# 读取模拟数据
human_data <- readRDS("D:/干活还电脑/R语言/Downloads/simulated_human_data.rds")

# 计算每个组别和细胞类型的频数
cell_frequency <- as.data.frame(table(human_data$group, human_data$celltype))

# 根据组别拆分数据
BM_group <- subset(cell_frequency, Var1 == 'BM')
GM_group <- subset(cell_frequency, Var1 == 'GM')
# 设置画布为1行2列
par(mfrow = c(1, 2), xpd = TRUE)

 

绘制3D饼图

# 定义绘制3D饼图的函数
draw_3d_pie <- function(data, title, colors) {
  pie3D(x = data$Freq,
        radius = 1,    # 饼图半径
        height = 0.1,  # 饼图高度
        theta = pi / 6,# 视角
        explode = 0,   # 饼图块错位展示
        main = title,  # 饼图标题
        col = colors,  # 分组颜色
        border = "black",  # 边框线颜色
        shade = 0.5,   # 图形阴影
        labels = paste0(data$Var2, "/n", round(data$Freq / sum(data$Freq) * 100, 2), "%"),  # 图形标签
        mar = c(2, 2, 2, 3),  # 饼图周围边距
        labelcol = "black",   # 标签颜色
        labelcex = 0.8        # 标签字体大小
  )
}

# 绘制BM组的3D饼图
draw_3d_pie(BM_group, "Celltype Fraction of BM", c("#d2981a", "#a53e1f", "#457277", "#8f657d", "#8dcee2"))

# 绘制GM组的3D饼图
draw_3d_pie(GM_group, "Celltype Fraction of GM", c("#d2981a", "#a53e1f", "#457277", "#8f657d", "#8dcee2"))

 

调整标签位置

# 调整标签位置
label_positions <- pie3D(x = BM_group$Freq,
                         radius = 1,
                         height = 0.1,
                         theta = pi / 6,
                         explode = 0,
                         main = "BM",
                         col = c("#d2981a", "#a53e1f", "#457277", "#8f657d", "#8dcee2"),
                         border = "black",
                         shade = 0.5,
                         labels = paste0(BM_group$Var2, "/n", round(BM_group$Freq / sum(BM_group$Freq) * 100, 2), "%"),
                         mar = c(2, 2, 2, 3),
                         labelcol = "black",
                         labelcex = 0.8)

# 手动调整标签位置
label_positions[5] <- 6.5

# 使用调整后的标签位置绘制3D饼图
pie3D(x = BM_group$Freq,
      labelpos = label_positions,
      radius = 1,
      height = 0.1,
      theta = pi / 6,
      explode = 0,
      main = "BM",
      col = c("#d2981a", "#a53e1f", "#457277", "#8f657d", "#8dcee2"),
      border = "black",
      shade = 0.5,
      labels = paste0(BM_group$Var2, "/n", round(BM_group$Freq / sum(BM_group$Freq) * 100, 2), "%"),
      mar = c(2, 2, 2, 3),
      labelcol = "black",
      labelcex = 0.8)

 

扇形错位展示

# 实现扇形错位展示
pie3D(x = BM_group$Freq,
      labelpos = label_positions,
      radius = 1,
      height = 0.1,
      theta = pi / 6,
      explode = 0.1,  # 设置错位展示
      main = "BM",
      col = c("#d2981a", "#a53e1f", "#457277", "#8f657d", "#8dcee2"),
      border = "black",
      shade = 0.5,
      labels = paste0(BM_group$Var2, "/n", round(BM_group$Freq / sum(BM_group$Freq) * 100, 2), "%"),
      mar = c(2, 2, 2, 3),
      labelcol = "black",
      labelcex = 0.8)

 

 

 

 

阅读剩余
THE END