二维饼图
代码如下:
#绘制2维饼图x=read.delim("C:/Users/a/Desktop/sample.txt",header=FALSE) #读入文本数据
names(x)=c("word","count") #加表头
x=transform(x, pct=round(x$count/sum(x$count)*100)) #数据框增加百分比列
y=x[order(x[,2],decreasing=T),]#排序
z=head(y,n=10)#抽取前10行
percent <- paste(z$pct,"%",sep="") #add % to labels
lbls <- paste(z$word,percent,sep="\n") #换行
pie(z$pct,labels=lbls,col=rainbow(length(lbls)),main="Pie Chart of Word")
3D饼图
沿用前面的数据,绘制R语言绘制3D饼图的代码如下:
#3D饼图library("plotrix")lp=pie3D(z$pct,radius=0.8,height=0.1,labels=lbls,explode=0.1,main="3D pie DEMO")#调整标签位置(效果不太好)lp[1] <- 0.05 lp[3] <- 3.2 pie3D(z$pct,radius=0.8,height=0.1,labels=lbls,labelpos=lp,explode=0.1,main="3D pie DEMO")
本文出自 “点滴积累” 博客,请务必保留此出处http://tianxingzhe.blog.51cto.com/3390077/1640879