我想使用 R 中的 ggplot2 和 geom_col 来创建一个仅显示饼图圆的饼图,以便整个绘图只是饼图圆,每边都没有任何空间。下面是我的代码示例:
library('ggplot2')
df = data.frame(group=c("a","b","c"),
value=c(0.2,0.4,0.4))
p=ggplot(df,aes(x="", y=value, fill=group)) +
geom_col(position="fill") +
coord_polar(theta = "y") +
guides(fill=F) +
theme(
panel.background = element_rect(fill = "grey", colour = NA),
#"grey" is just to show the panel area
plot.background = element_rect(fill = "transparent", colour = NA),
panel.grid = element_blank(),
panel.border = element_blank(),
plot.margin = unit(c(0, 0, 0, 0), "null"),
panel.spacing = unit(0, "null"),
axis.ticks = element_blank(),
axis.text = element_blank(),
axis.title = element_blank(),
axis.line = element_blank(),
legend.position = "none",
axis.ticks.length = unit(0, "null"),
axis.ticks.margin = unit(0, "null"),
legend.margin = margin()
)
下图是它创建的图。我想要的是让圆圈的顶部和底部位于灰色区域的顶部和底部边缘。我该怎么做?
最佳答案
我认为这是在 ggplot2 中硬编码的。您可以通过在控制台中输入 trace(ggplot2:::r_rescale, edit = TRUE)
来深入了解底层内容(请参阅 this GH page 底部未导出的 r_rescale
的函数定义) )并将重新缩放值从 0.4
更改为 0.5
。
编辑函数后重新运行代码的结果:
完成后,运行untrace(ggplot2:::r_rescale)
,一切将恢复正常。
附:需要注意的是,如果您指定 theme_void()
(覆盖默认的 theme_grey()
),则可以跳过代码中的所有主题规范。
关于r - 如何在R中的ggplot2中填充整个面板中的饼图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64362209/