看教程不够直观,那就看视频吧! >>点击加载视频
采用pie()绘制饼图
基本形式:
pie(x, labels = names(x), radius = ,main=" ", col = , clockwise = FALSE, density = , angle = , lty = , border = , edges = , …)
参数
x:表示一个非负的数值向量,表示每个扇形的面积。
labels:代表各扇形面积标签的字符型向量。
radius:表示饼图的圆的半径,特别是在字符标签过长的情况尤其适用。
main:用来表示图标的标题的。
col:表示每个扇形的颜色表示每个扇形的颜色
clockwise:一个逻辑值,TRUE顺时针做出分割,FALSE逆时针做出分割(默认)
density:底纹的密度。默认值为NULL。
angle:设置底纹的斜率
lty:设置边线类型
border:设置边线颜色
edges:设置多边形的边数(圆的轮廓是具有很多边的多边形近似)。
简单饼图
x <- c(50,100,80,130,150)
label <- c("黑龙江","北京", "山东", "广州", "浙江")
pie(x, labels = label)
细化:
pie(x, labels = label,main="饼图",radius =0.8,col =c("cadetblue","cadetblue1","cadetblue2","cadetblue3","cadetblue4"),clockwise = TRUE)
pie(x, labels = label,main="饼图",radius =0.8,col =c("cadetblue","cadetblue1","cadetblue2","cadetblue3","cadetblue4"),clockwise = TRUE,density =100,angle =45,edges = 3)
标注各扇形的百分比以及添加图表图例
percent<-round(100*x/sum(x),2) #保留两位小数
percent <-paste(percent, "%", sep = "") #将不同数据类型放在一起
pie(x, labels = percent,main="饼图",radius =0.8,col =c("cadetblue","cadetblue1","cadetblue2","cadetblue3","cadetblue4"),clockwise = TRUE)
legend("topright",label, cex=0.8, fill=c("cadetblue","cadetblue1","cadetblue2","cadetblue3","cadetblue4"))
手动设置图例位置(选定位置双击鼠标):locator()参数
pie(x, labels = percent,main="饼图",radius =0.8,col =c("cadetblue","cadetblue1","cadetblue2","cadetblue3","cadetblue4"),clockwise = TRUE)
legend(locator(1),,label, cex=0.8, fill=c("cadetblue","cadetblue1","cadetblue2","cadetblue3","cadetblue4"))
采用pie3D()绘制3D饼图:
install.packages("plotrix")
library(plotrix)
基本形式:
pie3D(x, labels= , main=" ", explode= , radius= , height= ,col= ,...)
参数:
labels:各个“块”的标签
main:饼图主标题
explode:各个“块”之间的间隔,默认值为0
radius:整个“饼”的大小,默认值为1,0~1为缩小
height:饼块的高度,默认值为0.1
绘制简单3D饼图:
pie3D(x,labels=label)
美化:
pie3D(x,labels=label,explode=0.1, radius=0.9 , height=0.07,col=c("#f20c00","#f05654","#ffb3a7","#8c4356","#ff2d51"))
标注各扇形百分比及添加图例:
percent<-round(100*x/sum(x),2) #保留两位小数
percent <-paste(percent, "%", sep = "") #将不同数据类型放在一起
pie3D(x,labels=percent,explode=0.1, radius=0.9 , height=0.07,col=c("#f20c00","#f05654","#ffb3a7","#8c4356","#ff2d51"))
legend("topright",label, cex=0.8, fill=c("#f20c00","#f05654","#ffb3a7","#8c4356","#ff2d51"))
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!