R语言饼图的绘制

时间:2021-07-03 15:00:40

先做数据框
type
Type of_mC percent
1 AA 15855 27.1%
2 BB 772959 13.2%
3 CC 3483549 59.6%
然后咧,具体代码如下
pie(type ofmC,labels=names(type of_mC)<-c(“”,”“,”“),col=c(“red”,”orange”,”skyblue”))
上面的意思是化饼图,然后把of_mC这一列的标注设定为空格,颜色为三种
text(0.3,0.5,type percent[1])text(0,0.3,type percent[3])
text(-0.3,0.5,type percent[2])legend(0.6,1.05,type Type, col = c(“red”,”orange”,”skyblue”), lty = 0,pch = 15, ncol = 4, cex = 0.8)
加上legend
很是简洁哈,下面介绍用ggplot2画pie图,先设定颜色
colour<-c(“skyblue”,”orange”,”lightgreen”)
p <- ggplot(type, aes(x = “”, y = of_mC, fill = Type)) +
geom_bar(width = 1,stat = “identity”) + labs(title = “LS”,x=”“,y=”“)
p + coord_polar(“y”, start = 0) + geom_text(aes(y = of_mC/2 +
c(0, cumsum(of_mC)[-length(of_mC)]),label = percent), size=8) + scale_fill_manual(values = colour) + theme(axis.ticks=element_blank())

最后一句话是去掉一个小短线,下面的参考博文中说双引号还没有找到怎么去掉,这里有一个方法就是在lab中直接把x,y设置为空即可,如加粗部分
geom_text是将百分比给画上去
其实还有参考其他的博文,刚才关掉了网页,等以后有机会找到了再加上吧,另外makrdown不能生成图,不知道csdn啥时候能迁入代码直接画图呢
ref:
1:http://joseph.yy.blog.163.com/blog/static/50973959201262681120903/
2: http://joseph.yy.blog.163.com/blog/static/509739592012718101838732/
3:http://www.r-bloggers.com/pie-charts-in-ggplot2/