【R语言 可视化】R语言 ggplot2 一张图画多个饼

author:laidefa
date:2016-01-18
椰丝,终于画好了,要画好多个饼不容易啊 ,画都画饱了。
【R语言 可视化】R语言 ggplot2 一张图画多个饼_第1张图片

【R语言 可视化】R语言 ggplot2 一张图画多个饼_第2张图片

# data:2016-1-18
# author:laidefa
rm(list=ls())
gc()
a=c(15,25,40,12,8)
b=c(5,18,12,45,10)
c=c(7,24,13,36,20)
d=c(3,27,40,21,9)
type=c("A","B","C","D","E")
library(ggplot2)
library(plyr)
library(gridExtra)
data1<- data.frame(a,type)
data1<-arrange(data1,-a)
data2<-data.frame(b,type)
data2<-arrange(data2,-b)
data3<-data.frame(c,type)
data3<-arrange(data3,-c)
data4<-data.frame(d,type)
data4<-arrange(data4,-d)


pie1<-ggplot(data1,aes(x=1,y=a,fill=type))+geom_bar(stat = "identity")+coord_polar(theta = "y")+
  scale_fill_discrete(breaks=data1$type)+labs(title="Different Goods In Rate\n(2015)")+
  theme(axis.title=element_blank(),axis.text=element_blank(),axis.ticks=element_blank())+
  geom_text(aes(x=1.2,y=cumsum(a)-a/2,label=paste(type,":",round(a/sum(a)*100,2),"%")),size=4)



pie2<-ggplot(data2,aes(x=1,y=b,fill=type))+geom_bar(stat = "identity")+coord_polar(theta = "y")+
  scale_fill_discrete(breaks=data2$type)+labs(title="Different Goods In Rate\n(2015)")+
  theme(axis.title=element_blank(),axis.text=element_blank(),axis.ticks=element_blank())+
  geom_text(aes(x=1.2,y=cumsum(b)-b/2,label=paste(type,":",round(b/sum(b)*100,2),"%")),size=4)


pie3<-ggplot(data3,aes(x=1,y=c,fill=type))+geom_bar(stat = "identity")+coord_polar(theta = "y")+
  scale_fill_discrete(breaks=data3$type)+labs(title="Different Goods In Rate\n(2015)")+
  theme(axis.title=element_blank(),axis.text=element_blank(),axis.ticks=element_blank())+
  geom_text(aes(x=1.2,y=cumsum(c)-c/2,label=paste(type,":",round(c/sum(c)*100,2),"%")),size=4)


pie4<-ggplot(data4,aes(x=1,y=d,fill=type))+geom_bar(stat = "identity")+coord_polar(theta = "y")+
  scale_fill_discrete(breaks=data4$type)+labs(title="Different Goods In Rate\n(2015)")+
  theme(axis.title=element_blank(),axis.text=element_blank(),axis.ticks=element_blank())+
  geom_text(aes(x=1.2,y=cumsum(d)-d/2,label=paste(type,":",round(d/sum(d)*100,2),"%")),size=4)


grid.arrange(pie1,pie2,pie3,pie4,ncol=2,nrow=2)

你可能感兴趣的:(数据科学--R语言)