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

如何在R中用ggforce实现嵌套圆切片、上色及文本调整?

问题解决:为嵌套圆形的内层添加彩色扇形切片并调整文本位置

需求说明

已有基于ggforce的嵌套圆形调查进度图,需要实现:

  • 将最内层圆形分割为3个扇形切片,分别为Group A(红色)、Group B(绿色)、Group C(黄色)上色
  • 优化切片内文本的位置,使其更居中美观

关键修改步骤

  1. 修正角度计算逻辑
    原代码中角度计算使用了不合理系数(如1.4*pi、8*pi),需改为按分组数量占总响应数的比例分配完整的2*pi圆周角度,确保扇形比例准确。

  2. 添加扇形填充层
    使用ggforce::geom_arc_bar绘制带填充色的扇形切片,替代原有的仅线段分割,通过start和end参数定义每个扇形的角度范围,绑定分组对应的填充色。

  3. 优化文本位置计算
    调整文本距离中心的半径比例,同时根据中点角度的正负修正Y轴方向,确保文本始终在扇形内部居中显示。

  4. 统一颜色映射
    为三个分组指定目标颜色,在scale_fill_manual中明确对应关系,避免颜色混乱。

修改后的完整代码

total_participants = 7500  
undelivered = 2500
total_invited = 5000  
total_responses = 2000  
total_unanswered = 1100  
group_c = 900 
group_a = 650  
group_b = 450 
partially_completed = 300  
library(ggplot2)
library(ggforce)

# Define total responses in the pie chart
total_count = group_a + group_b + group_c

# 修正角度计算:按比例分配2π圆周
angle_a = 2 * pi * (group_a / total_count)  
angle_b = 2 * pi * (group_b / total_count)  
angle_c = 2 * pi - (angle_a + angle_b)  

# Cumulative angles for the pie slices
angles = c(0, angle_a, angle_a + angle_b, 2 * pi)

# Midpoint angles for text placement
mid_angles = c((angles[1] + angles[2]) / 2,  
               (angles[2] + angles[3]) / 2,  
               (angles[3] + angles[4]) / 2) 

# Pie chart position and radius
pie_center_x = 2.7
pie_center_y = -0.6
pie_radius = 3

# Data frame for the pie chart
pie_data = data.frame(
  category = c("Group A", "Group B", "Group C"),
  value = c(group_a, group_b + total_unanswered, group_c),
  start = angles[1:3],
  end = angles[2:4],
  mid_angle = mid_angles,
  x_start = pie_center_x,
  y_start = pie_center_y,
  # 优化文本位置:调整半径比例,根据角度修正Y方向
  text_x = pie_center_x + (pie_radius * 0.5) * cos(mid_angles),
  text_y = pie_center_y + (pie_radius * 0.5) * sin(mid_angles) * ifelse(mid_angles > pi, -1, 1)
)

# Bubble chart data
bubble_data = data.frame(
  group = c(paste0("Total Participants: ", total_participants),
            paste0("Invited: ", total_invited),
            paste0("Responded: ", total_unanswered + group_a + group_b + group_c),
            paste0("Not Delivered: ", undelivered),
            paste0("Partial Responses: ", partially_completed)),
  radius = c(7, 5, 3, 0.8, 0.8),
  x0 = c(0, 1, 2.7, -5.4, -1),
  y0 = c(0, 0, -0.6, 1, 1.4)
)

# Final plot
plot_bubble_pie = ggplot() +
  
  # Bubble chart
  ggforce::geom_circle(data = bubble_data, aes(x0 = x0, y0 = y0, r = radius, 
                                               fill = factor(group, group)), 
                       alpha = 1) +
  geom_text(data = bubble_data, aes(x = x0, y = y0 + radius + 0.2, label = group), 
            size = 3.2, fontface = "bold") +
  
  # 添加扇形填充层
  ggforce::geom_arc_bar(data = pie_data,
                        aes(x0 = x_start, y0 = y_start, r0 = 0, r = pie_radius,
                            start = start, end = end, fill = category),
                        color = "black", linewidth = 1) +
  
  # Labels inside pie slices(合并分组名和数值)
  geom_label(data = pie_data, 
             aes(x = text_x, y = text_y, label = paste0(category, ":\n", value)), 
             size = 3.2, fontface = "bold", label.size = 0, fill = NA) +
  
  # 自定义颜色映射:对应需求的红、绿、黄
  scale_fill_manual(values = c("Group A" = "#e74c3c", 
                               "Group B" = "#2ecc71", 
                               "Group C" = "#f1c40f",
                               '#77bca2', '#e1926b', '#a09cc8', "grey", "orange"),
                    guide = 'none') +
  
  coord_equal() +
  theme_void()

# Display the plot
plot_bubble_pie

代码说明

  • 角度计算:删除错误系数,改为基于2*pi的比例分配,保证扇形占比准确
  • 扇形绘制:使用geom_arc_bar实现填充,r0=0表示从中心开始绘制完整扇形
  • 文本位置:调整距离中心的比例为0.5倍半径,通过ifelse修正下半圆文本的Y轴方向,避免偏移出扇形
  • 颜色映射:明确指定三个分组的颜色,同时保留原有嵌套圆形的颜色配置

内容的提问来源于stack exchange,提问作者Homer Jay Simpson

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 06:44:53