R语言ggplot2饼图标签外移、图例调整及可视化优化咨询
R语言ggplot2饼图问题解决方案

1. 扇区数值标签外移
极坐标下x值越大越靠近画布外缘,只需修改geom_text的x映射值为大于1的数值即可将标签外移,额外添加check_overlap = TRUE参数可自动隐藏重叠的标签,修改示例:
geom_text(aes(x = 1.3, label = value), size=4, position = position_stack(vjust = 0.5), alpha = 1, color = "black", check_overlap = TRUE)
如果需要完全避免标签重叠,可使用ggrepel包的geom_text_repel,会自动为重叠标签添加引导线。
2. 图例位置上移
通过theme中的legend.position参数配置图例相对位置,坐标取值范围为0-1,(0,0)对应画布左下角,(1,1)对应画布右上角。如需将图例移动到右上角区域,添加如下配置即可:
theme( legend.position = c(0.9, 0.95), legend.justification = "top", legend.box.just = "right" )
可根据实际显示效果微调坐标数值。
3. 可视化优化建议
- 改用环形饼图(甜甜圈图):将
geom_col(width = 1)修改为width = 0.6,中间空白区域可添加总内存占用数值,视觉上更清爽,也能留出更多空间放标签 - 处理过长图例文本:用
stringr::str_wrap()对Legend字段自动换行,避免图例横向占用过多空间,比如fill = fct_inorder(str_wrap(Legend, 25)) - 移除多余视觉元素:在theme中添加
panel.grid = element_blank()去掉极坐标网格线,降低视觉干扰 - 优化标签可读性:数值标签添加单位,改为
label = paste0(value, "MB") - 如重点是对比各类别数值大小,建议改用水平条形图,对小占比类别的数值差异展示会比饼图清晰很多
完整可运行修改后代码
df <- structure(list(group = c("Other", "Pool", "VNC", "File", "EtwB", "Thre", "HTab", "Vkmc", "Ntfx", "MmCa"), value = c(20.32, 6.16, 4.03, 2.2, 2.05, 2.01, 1.72, 1.7, 1.56, 1.27), FileName = c("", NA, "C:\\Windows\\System32\\Drivers\\netvsc.sys", NA, NA, NA, NA, "C:\\Windows\\System32\\Drivers\\vmbkmcl.sys", NA, NA), Description = c("", "<unknown> (Pool tables, etc.)", "Virtual NDIS Miniport", "<unknown> (File objects)", "nt!etw (Etw Buffer)", "nt!ps (Thread objects)", "<unknown> (Hash Table pool)", "Hyper-V VMBus KMCL", NA, "nt!mm (Mm control areas for mapped files)"), Legend = c("Other", "Pool - <unknown> (Pool tables, etc.)", "VNC - Virtual NDIS Miniport - C:\\Windows\\System32\\Drivers\\netvsc.sys", "File - <unknown> (File objects)", "EtwB - nt!etw (Etw Buffer)", "Thre - nt!ps (Thread objects)", "HTab - <unknown> (Hash Table pool)", "Vkmc - Hyper-V VMBus KMCL - C:\\Windows\\System32\\Drivers\\vmbkmcl.sys", "Ntfx", "MmCa - nt!mm (Mm control areas for mapped files)")), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame")) df2 <- structure(list(group = c("Other", "Pool", "VNC", "File", "EtwB", "Thre", "HTab", "Vkmc", "Ntfx", "MmCa"), value = c(20.32, 6.16, 4.03, 2.2, 2.05, 2.01, 1.72, 1.7, 1.56, 1.27), Legend = c("Other", "Pool - <unknown> (Pool tables, etc.)", "VNC - Virtual NDIS Miniport - C:\\Windows\\System32\\Drivers\\netvsc.sys", "File - <unknown> (File objects)", "EtwB - nt!etw (Etw Buffer)", "Thre - nt!ps (Thread objects)", "HTab - <unknown> (Hash Table pool)", "Vkmc - Hyper-V VMBus KMCL - C:\\Windows\\System32\\Drivers\\vmbkmcl.sys", "Ntfx", "MmCa - nt!mm (Mm control areas for mapped files)"), csum = c(43.02, 22.7, 16.54, 12.51, 10.31, 8.26, 6.25, 4.53, 2.83, 1.27), pos = c(32.86, 19.62, 14.525, 11.41, 9.285, 7.255, 5.39, 3.68, 2.05, 0.635)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame")) library(ggplot2) library(tidyverse) library(RColorBrewer) # 如需使用带引线的标签可加载ggrepel # library(ggrepel) nb.cols <- 10 mycolors <- colorRampPalette(brewer.pal(8, "Dark2"), interpolate='spline')(nb.cols) font_size <- 20 # 自动换行图例文本 df$Legend <- str_wrap(df$Legend, width = 25) plot_to_save <- ggplot(df, aes(x = "", y = value, fill = fct_inorder(Legend))) + # 改为环形饼图可把width改成0.6 geom_col(width = 1, color = 1, alpha= 0.8) + # 标签外移,添加防重叠参数 geom_text(aes(x = 1.3, label = paste0(value, "MB")), size=4, position = position_stack(vjust = 0.5), alpha = 1, color = "black", check_overlap = TRUE) + # 如需用带引线的标签,替换上面的geom_text为下面的代码 # geom_text_repel(aes(label = paste0(value, "MB")), size=4, # position = position_stack(vjust = 0.5), segment.size = 0.3) + coord_polar(theta = "y") + guides(fill = guide_legend(title = "Driver")) + scale_y_continuous(breaks = df2$pos, labels = df$group) + theme_minimal() + theme( axis.ticks = element_blank(), axis.title = element_blank(), axis.text = element_text(size = font_size-4), panel.background = element_rect(fill = "white"), # 移除网格线 panel.grid = element_blank(), text = element_text(family = "Segoe UI", size = font_size), # 调整图例到右上角 legend.position = c(0.9, 0.95), legend.justification = "top", legend.box.just = "right" ) + scale_fill_manual(values = mycolors) + ggtitle("Memory NonPagedUsed, MB") ggsave( file = "c:\\temp\\pie_chart.png", plot = plot_to_save, width = 16, height = 8, dpi = 150, units = "in", type = "cairo-png" )
内容的提问来源于stack exchange,提问作者Maxim
相关产品推荐
相关产品推荐

