ggplot饼图:黑色分段的标签文字及填充色设为白色的需求
ggplot饼图标签样式优化实现
需求说明
- 除黑色填充的分段(
low类)外,其余分段的标签文字颜色与对应分段的填充色保持一致; - 黑色填充的分段,其标签文字及标签背景(内边距填充色)设置为白色,解决黑色标签文字无法看清的问题。
当前代码
ggplot(pigs2.proportions, aes(x="", y=comp_percent, fill = percent_class, width = 0.4)) + geom_bar(stat="identity") + geom_label( aes( label = ifelse(comp_percent %% 1 != 0, sprintf("%.2f%%", comp_percent), sprintf("%.0f%%", comp_percent))), position = position_stack(vjust = 0.5), size = 4, label.padding = unit(0.25, "lines"), show.legend = FALSE) + coord_polar("y", start=0) + facet_wrap(~factor(source, levels = c("fish", "soy", "skim")), labeller = custom_labeller("source")) + scale_fill_manual(values = c("low" = "black", "moderate" = "darkgray", "high" = "lightgray")) + theme( axis.text.x = element_blank(), # Hide labels on polar coordinate axis panel.grid = element_blank(), # Remove grid lines panel.border = element_blank(), # Remove round outline axis.text = element_blank(), # Remove axis text axis.title = element_blank() # Remove axis titles )
数据集结构
structure(list(source = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L), levels = c("fish", "soy", "skim"), class = "factor"), percent_class = structure(c(3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L), levels = c("low", "moderate", "high"), class = "factor"), count = c(3L, 2L, 5L, 1L, 3L, 6L, 1L, 3L, 5L), comp_percent = c(30, 20, 50, 10, 30, 60, 11.1111111111111, 33.3333333333333, 55.5555555555556 ), pos = c(15, 10, 25, 35, 35, 80, 45.5555555555556, 66.6666666666667, 137.777777777778)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA, -9L), groups = structure(list( percent_class = structure(1:3, levels = c("low", "moderate", "high"), class = "factor"), .rows = structure(list(c(2L, 5L, 8L), c(3L, 6L, 9L), c(1L, 4L, 7L)), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame" ), row.names = c(NA, -3L), .drop = TRUE))
当前效果

修改后的代码及解释
# 定义颜色映射规则 fill_colors <- c("low" = "black", "moderate" = "darkgray", "high" = "lightgray") text_colors <- ifelse(names(fill_colors) == "low", "white", fill_colors) label_bg_colors <- ifelse(names(fill_colors) == "low", "white", NA) ggplot(pigs2.proportions, aes(x="", y=comp_percent, fill = percent_class, width = 0.4)) + geom_bar(stat="identity") + geom_label( aes( label = ifelse(comp_percent %% 1 != 0, sprintf("%.2f%%", comp_percent), sprintf("%.0f%%", comp_percent)), # 映射标签文字颜色与背景色 color = percent_class, fill = percent_class ), position = position_stack(vjust = 0.5), size = 4, label.padding = unit(0.25, "lines"), show.legend = FALSE) + coord_polar("y", start=0) + facet_wrap(~factor(source, levels = c("fish", "soy", "skim")), labeller = custom_labeller("source")) + # 配置各元素颜色刻度 scale_fill_manual(values = fill_colors) + scale_color_manual(values = text_colors) + scale_fill_manual(values = label_bg_colors, na.value = "transparent", guide = "none") + theme( axis.text.x = element_blank(), panel.grid = element_blank(), panel.border = element_blank(), axis.text = element_blank(), axis.title = element_blank() )
关键改动说明
- 颜色规则定义:针对不同分段类,分别指定饼图填充色、标签文字色、标签背景色,其中
low类单独设置白色文字和白色背景; - 标签属性映射:在
geom_label的aes中新增color和fill映射,分别控制标签文字和标签背景的颜色; - 刻度配置:通过三个
scale_*_manual分别设置饼图填充色、标签文字色、标签背景色,非low类的标签背景设为透明,避免遮挡饼图本身的填充色。
内容的提问来源于stack exchange,提问作者Joe
相关产品推荐
相关产品推荐

