如何在R中绘制堆叠条形图?团队特征数据可视化需求
团队特征数据可视化方案
1. 堆叠条形图(绝对值)
要实现每支团队的主条形堆叠三个特征数值,首先需要将数据转换为长格式(tidy data),这是ggplot2的最优数据格式。
数据转换与绘图代码
# 加载所需包 library(ggplot2) library(dplyr) library(tidyr) # 原始数据 Team <- c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M") total <- c(557, 3, 3116, 201, 167, 1348, 877, 1444, 2, 1003, 25, 732, 2532) characteristic1 <- c(111, 0, 566, 45, 77, 600, 356, 300, 0, 402, 3, 278, 312) characteristic2 <- c(20, 0, 231, 14, 15, 27, 30, 78, 0, 48, 0, 29, 111) characteristic3 <- c(1, 0, 29, 1, 0, 10, 11, 3, 0, 2, 9, 1, 3) df <- data.frame(Team, total, characteristic1, characteristic2, characteristic3) # 转换为长格式,提取特征列 df_long <- df %>% select(Team, characteristic1, characteristic2, characteristic3) %>% pivot_longer(cols = -Team, names_to = "Characteristic", values_to = "Value") # 绘制堆叠条形图 ggplot(df_long, aes(x = Team, y = Value, fill = Characteristic)) + geom_bar(stat = "identity") + labs(title = "各团队特征数值堆叠条形图", x = "团队", y = "数值", fill = "特征类型") + theme_minimal()
如果需要对比特征数值与总人数,可以添加参考线和标注:
ggplot(df_long, aes(x = Team, y = Value, fill = Characteristic)) + geom_bar(stat = "identity") + # 添加总人数标注 geom_text(data = df, aes(x = Team, y = total, label = total), vjust = -0.5, size = 3) + # 添加总人数虚线参考线 geom_hline(data = df, aes(yintercept = total), linetype = "dashed", color = "gray50") + labs(title = "各团队特征数值与总人数对比", x = "团队", y = "数值", fill = "特征类型") + theme_minimal()
2. 堆叠条形图(百分比)
要转换为占总人数的百分比,只需在数据预处理阶段计算特征占比即可:
数据转换与绘图代码
# 计算特征占总人数的百分比,处理NA值(总人数为0的团队) df_percent <- df %>% mutate( pct1 = characteristic1 / total * 100, pct2 = characteristic2 / total * 100, pct3 = characteristic3 / total * 100 ) %>% select(Team, pct1, pct2, pct3) %>% pivot_longer(cols = -Team, names_to = "Characteristic", values_to = "Percentage") %>% mutate( Characteristic = recode(Characteristic, "pct1" = "特征1", "pct2" = "特征2", "pct3" = "特征3"), Percentage = replace_na(Percentage, 0) ) # 绘制百分比堆叠条形图 ggplot(df_percent, aes(x = Team, y = Percentage, fill = Characteristic)) + geom_bar(stat = "identity") + labs(title = "各团队特征占总人数百分比堆叠图", x = "团队", y = "占比 (%)", fill = "特征类型") + theme_minimal() + scale_y_continuous(limits = c(0, 100)) # 强制Y轴范围为0-100%
3. 非条形图替代方案
雷达图
适合对比多维度特征在不同团队间的分布,尤其适配百分比数据:
library(fmsb) # 整理雷达图所需格式:添加最大值、最小值行 df_radar <- df_percent %>% pivot_wider(names_from = Team, values_from = Percentage) radar_data <- rbind(rep(100, 13), rep(0, 13), df_radar %>% select(-Characteristic)) rownames(radar_data) <- c("Max", "Min", df_radar$Characteristic) # 绘制雷达图 radarchart(radar_data, pcol = c("#00AFBB", "#E7B800", "#FC4E07"), pfcol = scales::alpha(c("#00AFBB", "#E7B800", "#FC4E07"), 0.3), plwd = 2, cglcol = "gray", cglty = 1, axislabcol = "gray", title = "各团队特征占比雷达图") legend(x = 1.3, y = 1, legend = rownames(radar_data)[3:5], col = c("#00AFBB", "#E7B800", "#FC4E07"), lty = 1, lwd = 2)
热力图
清晰展示不同团队与特征间的占比差异,适合快速定位极值:
ggplot(df_percent, aes(x = Team, y = Characteristic, fill = Percentage)) + geom_tile(color = "white") + geom_text(aes(label = sprintf("%.1f%%", Percentage)), color = "black", size = 3) + scale_fill_viridis_c(option = "plasma") + labs(title = "各团队特征占比热力图", x = "团队", y = "特征类型", fill = "占比 (%)") + theme_minimal()
散点图矩阵
探索总人数与各特征、特征之间的相关性:
library(GGally) ggpairs(df, columns = 2:5, aes(color = Team)) + labs(title = "团队总人数与特征数值散点图矩阵") + theme_minimal()
内容的提问来源于stack exchange,提问作者marty
相关产品推荐
相关产品推荐

