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

R语言geom_arc_bar制作分离饼图异常:explode参数问题求助

关于ggforce包geom_arc_bar的explode参数导致饼图扇区分离不均的问题

我尝试用R语言制作各扇区均匀分离的饼图,原本计划通过ggforce包的geom_arc_bar()结合ggplot的explode参数实现,但结果出现异常。

问题复现:全局设置explode后分离不均

运行以下代码后,得到的饼图扇区分离不均匀:

library(ggforce)
library(ggplot2)

pie <- data.frame( days_in_each_month = c(31,  28,  31,  30,  31,  30,  31,  31,  30,  31,  30,  31),
                    months = c("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"))

p <- ggplot() + theme_no_axes() + coord_fixed() + theme(legend.position="none")
#define the inner set of background wedges
p <- p + geom_arc_bar(data = pie, stat = 'pie', aes(x0 = 0, y0 = 0, r0 = .1, r = .5, amount = days_in_each_month, fill = months, explode = 0.02))

p

部分设置为0时效果改善

经过测试发现,若为每个扇区单独指定explode参数,且最后三个扇区设为0时,分离效果有所改善:

pie <- data.frame( days_in_each_month = c(31,  28,  31,  30,  31,  30,  31,  31,  30,  31,  30,  31),
                   months = c("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"))

expl <- c(0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0, 0, 0)

p <- ggplot() + theme_no_axes() + coord_fixed() + theme(legend.position="none")
#define the inner set of background wedges
p <- p + geom_arc_bar(data = pie, stat = 'pie', aes(x0 = 0, y0 = 0, r0 = .1, r = .5, amount = days_in_each_month, fill = months, explode = expl))

p

修改非零值后再次出现不均

但如果将expl中的任意一个0改为其他值(比如最后一个设为0.2),饼图的分离效果又会变得不均匀:

pie <- data.frame( days_in_each_month = c(31,  28,  31,  30,  31,  30,  31,  31,  30,  31,  30,  31),
                   months = c("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"))

expl <- c(0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0, 0, 0.2)

p <- ggplot() + theme_no_axes() + coord_fixed() + theme(legend.position="none")
#define the inner set of background wedges
p <- p + geom_arc_bar(data = pie, stat = 'pie', aes(x0 = 0, y0 = 0, r0 = .1, r = .5, amount = days_in_each_month, fill = months, explode = expl))

p

我无法解释这种现象,请问这是什么原因?


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 08:21:59