R语言-饼图-pie()
程序员文章站
2022-05-26 21:35:58
...
R语言-饼图-pie()
饼图可以用于分析某项指标占总体大小,以及指标之间的比较。具体而言,饼图是将值表示为具有不同颜色的圆的切片, 切片被标记,并且对应于每个片的数字也在图表中表示。在R语言中,饼图是使用**pie()**函数创建的,它使用正数作为向量输入。 附加参数用于控制标签,颜色,标题等。函数为:pie(x, labels = names(x), edges = 200, radius = 0.8, clockwise = FALSE, init.angle = f(clockwise) 90 else 0, density = NULL, angle = 45, col = NULL, border = NULL, lty = NULL, main = NULL, ...)
下面逐一介绍参数含义:
- x:非负的数值型向量,其中x中每个值的大小决定占据扇形面积的大小。
- labels:字符串或者表达式,为每个每个扇形加标签,若不需标签,可以是
NA
。 - edges:在绘制饼图时,外轮廓又多边形近似得到,edges的数值越大,饼图看上去越圆。
- radius:饼图被画在以radius为边长的正方形中,值越大,饼图越大。
- clockwise:逻辑值,FALSE表示出扇形按照逆时针方向排序,TRUE表示按照顺时针方向排序。
- init.angle:开始绘制时的初始角度。
- density:图形阴影线的密度。
- angle:阴影线的斜率,默认为45度。
- col:图形填充颜色。但是当设置了阴影线,则改为线条颜色。
- border:扇形边框的颜色。
- lty:扇形区域的线形。(0:无,1:实线;2:短划线;3:点线;4:点划线;5:长划线;6:双划线)
- main:标题
#下面代码是对每一个参数的实现,可以一句一句执行,查看效果
library(RColorBrewer)
a <- 1:5 #生成数据
pie(a) #最简单的饼图
pie(a,labels = c("a","b","c","d","e")) #设置扇形标签
pie(a,labels = c("a","b","c","d","e"),
edges = 1000) #值越大越圆滑,一般可以默认即可
pie(a,labels = c("a","b","c","d","e"),edges = 1000,
radius = 1) #设置圆的大小
pie(a,labels = c("a","b","c","d","e"),edges = 1000,
radius = 1,clockwise = TRUE) #设置顺时针方向绘图
pie(a,labels = c("a","b","c","d","e"),edges = 1000,
radius = 1,clockwise = TRUE,init.angle = 30) #绘图初始角度
pie(a,labels = c("a","b","c","d","e"),edges = 1000,
radius = 1,clockwise = TRUE,init.angle = 30,
density = 5,angle = 70) #设置阴影线及其角度
pie(a,labels = c("a","b","c","d","e"),edges = 1000,
radius = 1,clockwise = TRUE,init.angle = 30,
col = brewer.pal(5,"Set3")) #图形填充颜色,也可以只设置一种颜色
pie(a,labels = c("a","b","c","d","e"),edges = 1000,
radius = 1,clockwise = TRUE,init.angle = 30,
col = brewer.pal(5,"Set3"),border = "red") #边框颜色
pie(a,labels = c("a","b","c","d","e"),edges = 1000,
radius = 1,clockwise = TRUE,init.angle = 30,
col = brewer.pal(5,"Set3"),border = "red",
lty = 2,main = "饼图") #设置线型,加标题
如果需要绘制3D饼图或者扇形图,可以利用plotrix
包中的plot3D()
以及fan.plot()
函数。
上一篇: 外汇行情软件排行榜(炒外汇开户流程)