# 定义每个饼图的数据
data_list <- list(
c(Xian = 15),
c(Xian = 33, cA = 5, cB = 3, Wild = 3),
c(Xian = 12, Geng = 1, cB = 1, Wild = 1),
c(Xian = 2, cA = 3, Wild = 1),
c(Xian = 3),
c(Xian = 24, Geng = 17, cA = 4, cB = 4, Wild = 2),
c(Xian = 9, Geng = 1, cA = 2, cB = 3, Wild = 1)
)
# 获取所有唯一的类别
all_categories <- unique(unlist(lapply(data_list, names)))
# 为每个类别分配颜色
color_mapping <- setNames(rainbow(length(all_categories)), all_categories)
# 设置绘图布局,3 行 3 列
par(mfrow = c(3, 3))
# 循环绘制每个饼图
for (i in seq_along(data_list)) {
current_data <- data_list[[i]]
counts <- as.numeric(current_data)
categories <- names(current_data)
# 获取当前饼图中各类别的颜色
current_colors <- color_mapping[categories]
# 绘制饼图
pie(counts, labels = counts, main = paste("Pie Chart", i), col = current_colors)
# 在右下角添加标签
legend("bottomright", legend = categories, fill = current_colors, cex = 0.6)
}
# 恢复默认绘图布局
par(mfrow = c(1, 1))
指定颜色
# 定义每个饼图的数据
data_list <- list(
c(Xian = 15),
c(Xian = 33, cA = 5, cB = 3, Wild = 3),
c(Xian = 12, Geng = 1, cB = 1, Wild = 1),
c(Xian = 2, cA = 3, Wild = 1),
c(Xian = 3),
c(Xian = 24, Geng = 17, cA = 4, cB = 4, Wild = 2),
c(Xian = 9, Geng = 1, cA = 2, cB = 3, Wild = 1)
)
# 获取所有唯一的类别
all_categories <- unique(unlist(lapply(data_list, names)))
# 初始化颜色映射
color_mapping <- setNames(rainbow(length(all_categories)), all_categories)
# 修改特定类别的颜色
color_mapping["Xian"] <- "red"
color_mapping["Geng"] <- "blue"
color_mapping["Wild"] <- "black"
# 设置绘图布局,3 行 3 列
par(mfrow = c(3, 3))
# 循环绘制每个饼图
for (i in seq_along(data_list)) {
current_data <- data_list[[i]]
counts <- as.numeric(current_data)
categories <- names(current_data)
# 获取当前饼图中各类别的颜色
current_colors <- color_mapping[categories]
# 绘制饼图
pie(counts, labels = counts, main = paste("Pie Chart", i), col = current_colors)
# 在右下角添加标签
legend("bottomright", legend = categories, fill = current_colors, cex = 0.6)
}
# 恢复默认绘图布局
par(mfrow = c(1, 1))
只需要修改数值版
setwd("D:\\科研助理工作\\data\\3-\\3-20\\结构图\\ORF3")
# 定义每个饼图的数据
data_list <- list(
c(Xian=2,Geng=12,cA=6,cB=3,Wild=1),
c(Xian=51,Geng=7,cA=2,cB=3,Wild=1),
c(cB=2,Wild=2),
c(Xian=1,cB=1,Wild=5),
c(Wild=2),
c(Xian=3,Geng=11,cA=7,cB=2,Wild=6)
)
# 获取所有唯一的类别
all_categories <- unique(unlist(lapply(data_list, names)))
# 初始化颜色映射
color_mapping <- setNames(rainbow(length(all_categories)), all_categories)
color_mapping["Xian"] <- "red" # 修改 Xian 为红色
color_mapping["Geng"] <- "blue" # 修改 Geng 为蓝色
color_mapping["Wild"] <- "black" # 修改 Wild 为黑色
color_mapping["cA"] <- "#AAFF00FF"
color_mapping["cB"] <- "green"
# 打开 SVG 设备(指定文件名和尺寸)
svg("pie_charts_orf3.svg", width = 12, height = 22)
# 设置绘图布局,3 行 3 列
par(mfrow = c(3, 4))
# 循环绘制每个饼图
for (i in seq_along(data_list)) {
current_data <- data_list[[i]]
counts <- as.numeric(current_data)
categories <- names(current_data)
# 获取当前饼图中各类别的颜色
current_colors <- color_mapping[categories]
# 绘制饼图
pie(counts, labels = counts, main = paste("Pie Chart", i), col = current_colors)
# 在右下角添加标签
legend("bottomright", legend = categories, fill = current_colors, cex = 0.6)
}
# 关闭 SVG 设备
dev.off()
# 打开 Pdf 设备(指定文件名和尺寸)
pdf("pie_charts—orf3.pdf", width = 7, height = 32)
# 设置绘图布局,3 行 4 列
par(mfrow = c(13, 1))
# 循环绘制每个饼图
for (i in seq_along(data_list)) {
current_data <- data_list[[i]]
counts <- as.numeric(current_data)
categories <- names(current_data)
# 获取当前饼图中各类别的颜色
current_colors <- color_mapping[categories]
# 绘制饼图
pie(counts, labels = counts, main = paste("Pie Chart", i), col = current_colors)
# 在右下角添加标签
legend("bottomright", legend = categories, fill = current_colors, cex = 0.6)
}
# 关闭 Pdf 设备
dev.off()