You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PieDonut图表优化需求:解决重叠、标签取整与自定义颜色问题

解决PieDonut图表的三个问题

首先修正原始代码中的笔误:aes(pies = Country_Income)需改为aes(pies = Income_Group),数据框中对应列名为Income_Group。

以下是针对三个问题的具体解决方案及完整代码:

问题1:让'High income'饼图的甜甜圈部分不重叠

通过调整爆炸规则、图表空间和甜甜圈半径,给该组预留足够显示空间:

  • 设置explode = c(0, 1, 0),仅对'High income'对应的第2个饼块进行爆炸
  • 调大maxx至2.2,扩展图表横向显示范围
  • 将r2改为1.3,增大甜甜圈外半径,避免重叠

问题2:将标签百分比改为整数

使用labelFunction自定义标签格式,把百分比值转换为整数后添加百分号:

labelFunction = function(x) paste0(round(x * 100), "%")

问题3:应用指定颜色

通过colors参数传入你指定的颜色向量,替换默认配色


完整修改后的代码

# 确保已安装并加载ggPieDonut包
library(ggPieDonut)

# 原始数据框
inward_2022_region_income <- data.frame(
  Region = c("South Asia", "Europe & Central Asia", "Latin America & Caribbean", "East Asia & Pacific", "Middle East & North Africa", "East Asia & Pacific", "Europe & Central Asia", "Sub-Saharan Africa", "Europe & Central Asia", "Latin America & Caribbean"),
  Income_Group = c("Lower middle income", "High income", "Upper middle income", "Upper middle income", "Lower middle income", "Lower middle income", "Lower middle income", "Lower middle income", "Upper middle income", "Lower middle income"),
  remittance_received = c(164363767273, 136183611220, 117465697000, 62839165858, 58043307652, 42473756742, 38171200000, 37431728299, 33900928280, 24964975447)
)

# 生成调整后的PieDonut图表
PieDonut(inward_2022_region_income,
         aes(pies = Income_Group,
             donuts = Region,
             count = remittance_received),
         explode = c(0, 1, 0),
         explodeDonut = TRUE,
         ratioByGroup = FALSE,
         addDonutLabel = FALSE,
         labelposition = 0.5,
         donutLabelSize = 2,
         pieLabelSize = 2,
         titlesize = 4,
         use.labels = FALSE,
         title = "Inward Remittances by Income Group and Region",
         maxx = 2.2,
         r0 = 0,
         r1 = 0.8,
         r2 = 1.3,
         start = pi/1,
         showPieName = FALSE, 
         showRatioThreshold = FALSE,
         labelFunction = function(x) paste0(round(x * 100), "%"),
         colors = c("#0197FF", "#5938E4", "#22B2A4", "#15A800", "#B0DB02", "#E8B321", "#DB5614", "#F66DBD")
)

内容的提问来源于stack exchange,提问作者Hanna

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 14:57:42