R语言环形分组柱状图定制:替换标签为国家名称
问题解决:将环形柱状图的Individual标签替换为国家名称
核心改动说明
要把图表里的Individual标签换成真实国家名,核心是替换模拟数据为你的真实国家数据,并调整代码中引用标签的字段:
1. 导入真实数据
替换原来生成模拟数据的代码,读取你的国家数据(假设表格包含country(国家名)、group(分组:Am/As/E/Af)、value三个核心列):
# 先安装并加载必要包 install.packages("dplyr") install.packages("ggplot2") install.packages("googlesheets4") library(dplyr) library(ggplot2) library(googlesheets4) # 读取你的表格数据 data <- read_sheet("1hxZROTPoSb8bmVrdpvZsYzbSjjkuCzBw") # 调整分组名称为目标的Am、As、E、Af(根据表格实际分组名修改映射) data <- data %>% mutate(group = recode(group, "America" = "Am", "Asia" = "As", "Europe" = "E", "Africa" = "Af")) %>% arrange(group, value)
2. 调整标签引用
原代码中所有引用individual的地方,替换为你的国家名列(假设列名为country),尤其是geom_text里的标签参数。
3. 修正代码小错误
原代码里ggplot的数据源写的是global,必须改为data,否则会报错。
修改后的完整代码
# 加载必要包 library(dplyr) library(ggplot2) library(googlesheets4) # 读取真实国家数据 data <- read_sheet("1hxZROTPoSb8bmVrdpvZsYzbSjjkuCzBw") # 匹配目标分组名称 data <- data %>% mutate(group = recode(group, "America" = "Am", "Asia" = "As", "Europe" = "E", "Africa" = "Af")) %>% arrange(group, value) # 设置每组末尾的空栏数 empty_bar <- 3 to_add <- data.frame(matrix(NA, empty_bar * nlevels(factor(data$group)), ncol(data))) colnames(to_add) <- colnames(data) to_add$group <- rep(levels(factor(data$group)), each = empty_bar) data <- rbind(data, to_add) data <- data %>% arrange(group) data$id <- seq(1, nrow(data)) # 准备标签角度与位置 label_data <- data number_of_bar <- nrow(label_data) angle <- 90 - 360 * (label_data$id - 0.5) / number_of_bar label_data$hjust <- ifelse(angle < -90, 1, 0) label_data$angle <- ifelse(angle < -90, angle + 180, angle) # 准备分组基线数据 base_data <- data %>% group_by(group) %>% summarize(start = min(id), end = max(id) - empty_bar) %>% rowwise() %>% mutate(title = mean(c(start, end))) # 准备网格刻度数据 grid_data <- base_data grid_data$end <- grid_data$end[c(nrow(grid_data), 1:nrow(grid_data)-1)] + 1 grid_data$start <- grid_data$start - 1 grid_data <- grid_data[-1,] # 绘制环形柱状图 p <- ggplot(data, aes(x = as.factor(id), y = value, fill = group)) + geom_bar(stat = "identity", alpha = 0.5) + # 添加水平刻度线 geom_segment(data = grid_data, aes(x = end, y = 80, xend = start, yend = 80), colour = "grey", alpha = 1, size = 0.3, inherit.aes = FALSE) + geom_segment(data = grid_data, aes(x = end, y = 60, xend = start, yend = 60), colour = "grey", alpha = 1, size = 0.3, inherit.aes = FALSE) + geom_segment(data = grid_data, aes(x = end, y = 40, xend = start, yend = 40), colour = "grey", alpha = 1, size = 0.3, inherit.aes = FALSE) + geom_segment(data = grid_data, aes(x = end, y = 20, xend = start, yend = 20), colour = "grey", alpha = 1, size = 0.3, inherit.aes = FALSE) + # 添加刻度数值标签 annotate("text", x = rep(max(data$id), 4), y = c(20, 40, 60, 80), label = c("20", "40", "60", "80"), color = "grey", size = 3, angle = 0, fontface = "bold", hjust = 1) + ylim(-100, 120) + theme_minimal() + theme( legend.position = "none", axis.text = element_blank(), axis.title = element_blank(), panel.grid = element_blank(), plot.margin = unit(rep(-1, 4), "cm") ) + coord_polar() + # 替换为国家名称标签 geom_text(data = label_data, aes(x = id, y = value + 10, label = country, hjust = hjust), color = "black", fontface = "bold", alpha = 0.6, size = 2.5, angle = label_data$angle, inherit.aes = FALSE) + # 添加分组基线与名称 geom_segment(data = base_data, aes(x = start, y = -5, xend = end, yend = -5), colour = "black", alpha = 0.8, size = 0.6, inherit.aes = FALSE) + geom_text(data = base_data, aes(x = title, y = -18, label = group), hjust = c(1,1,0,0), colour = "black", alpha = 0.8, size = 4, fontface = "bold", inherit.aes = FALSE) # 显示图表 p
关键注意点
- 如果表格中国家名列不是
country,请把代码里的country替换为实际列名 - 若读取表格遇到权限问题,可先将表格导出为CSV,用
read.csv("你的文件路径.csv")读取 - 调整
recode函数里的分组映射,确保和你的表格原始分组名称匹配
内容的提问来源于stack exchange,提问作者Ana-Maria
相关产品推荐
相关产品推荐

