前面我们讲了如何用ggplot2,ggpie以及ggPieDount等来绘制饼图。但是有时候想要把饼图分开好像不是太容易,并且对于已经计算好的比例的数据,实现空心的饼图也不太容易。今天我们测试了另一个报ggforce这个包,用他来做饼图相对方便很多。
library(ggforce)
librara(ggplot)
生成一个简单的测试数据:
data <- data.frame(Var=c("A","B","C","D"),value=c(30.52,32.72,36.76,0))
ggplot()+
geom_arc_bar(data=data,
stat = "pie",
aes(x0=0,y0=0,r0=0,r=2,
amount=value,fill=Var,
explode=c(0.05,0.05,0.05,0)), #来控制每个扇形中间的间隔
)
生成中间空心的,通过r0和r来控制外圈和内圈的直径
ggplot()+
geom_arc_bar(data=data,
stat = "pie",
aes(x0=0,y0=0,r0=1,r=2,
amount=value,fill=Var,
explode=c(0.05,0.05,0.05,0)),
)
做个基本的美化
ggplot()+
geom_arc_bar(data=data,
stat = "pie",
aes(x0=0,y0=0,r0=0,r=2,
amount=value,fill=Var,
explode=c(0.05,0.05,0.05,0)),
)+
scale_fill_manual(values = c("#80c97f","#a68dc8",
"#ffc000","#c00000"))+
annotate("text",x=0,y=-2.2,label="32.72%")+
annotate("text",x=-1.6,y=1.5,label="36.76%",angle=50)+
annotate("text",x=1.6,y=1.5,label="30.52%",angle=-50)+
theme_void()+
theme(legend.position = "none",
plot.title = element_text(hjust = 0.5,face="italic"))+
labs(title = "GDDH13")
网友评论