R语言ggplot2绘制饼图中心出现尖锐凸起,如何解决?
解决ggplot2饼图保存后中心出现尖锐凸起的问题
问题原因
这个凸起是因为极小的切片(比如你的数据中Australia的value仅为5)在极坐标转换时,边框线条在中心区域出现渲染异常导致的。
解决方案
方法1:移除边框线条
如果不需要黑色边框,直接去掉geom_bar里的color="black"参数即可消除凸起:
data <- data.frame( continent=c("Europe", "N. America", "S. America", "Asia", "Africa", "Australia"), value=c(80, 30, 20, 110, 40, 5) ) library(ggplot2) ggplot(data, aes(x="", y=value, fill=continent)) + geom_bar(stat="identity", width=1) + coord_polar("y") + theme_void() ggsave("piechart.png")
方法2:让边框颜色匹配切片填充色
若需要边框但不想看到凸起,可将边框颜色与切片填充色绑定,隐藏异常凸起:
ggplot(data, aes(x="", y=value, fill=continent, color=continent)) + geom_bar(stat="identity", width=1, size=0.5) + coord_polar("y") + theme_void() + scale_color_manual(values = scales::hue_pal()(6))
方法3:调小线条粗细弱化视觉影响
如果要保留黑色边框,通过减小size参数的值来弱化凸起的视觉效果:
ggplot(data, aes(x="", y=value, fill=continent)) + geom_bar(stat="identity", width=1, color="black", size=0.2) + coord_polar("y") + theme_void() ggsave("piechart.png")
方法4:使用geom_col替代geom_bar(更简洁)
geom_col是geom_bar(stat="identity")的简写,代码更简洁,也能避免部分潜在渲染问题:
ggplot(data, aes(x="", y=value, fill=continent)) + geom_col(width=1, color="black", size=0.2) + coord_polar("y") + theme_void() ggsave("piechart.png")
内容的提问来源于stack exchange,提问作者mkranj
相关产品推荐
相关产品推荐

