R语言绘图:大道至简系列——barplot
前言
我知道,关于R语言的绘图教程满天飞,随便一搜一大堆,一方面我想打好基本功,另一方面,我想把这个做成一个系列,然后方便大家学习,整个系列可能不会有耳目一新的感觉,但我尽量多的运用帮助文档中的参数。
提示:以下是本篇文章正文内容,下面案例可供参考
一、barplot参数简介
条形图可垂直或水平展示分类变量的概率分布、频数分布或者百分比分布。
此篇教程分为一组observations的条形图和多组别observations的条形图。
参数详解:
1.height,main,sub:条形图的高度,也就是各观测发生的频数,可为向量或矩阵。main,sub为主标题和副标题。
2.width:条形图的条宽。若width向量的长度少于条形数,width会循环使用。若是一个数,则宽度不会变,因为相当于各个条形宽度还是1:1:1:1…,除非设置了xlim。
3.sapce:space为条形之间的空隙(空隙=平均条形宽 × sapce)。若height是矩阵,beside=TRUE,也即展示的是多组别observations的条形图时,space若为从c(m,n),m为组内的间隙,n为组间的间隙。
4.names.arg:height为向量时:names.arg为各条形命名。height为矩阵时:names.arg为各组命名。
5.legend.text:自动根据条形图顺序设置图例,且legend.text对应着图例的标签名,若legend.text为TRUE,则图例标签名对应与向量的names或矩阵的行名。
6.beside:TRUE or FALSE,当height为矩阵时,条形图时并列还是堆叠。
7.horize:是否水平放置。
8.density,angle:条形中填充斜线的密度及角度。
9.col,border:设置条形填充及边框颜色。
10.xlab,ylab:设置x,y轴标题。
11.xlim,ylim,xpd:设置横纵坐标轴限制,xpd:条形图是否能超出绘图范围。
12.log:‘x’,‘y’,‘xy’,定义x轴,y轴是否对数变换。
13.axes:logical,是否绘制频数对应的坐标轴。
14.axisnames:是否显示横坐标的名字。
15.cex.axis,cex.names:纵坐标标签大小,cex.names设置横坐标标签的大小。
16.axis.lty:横坐标坐标轴的直线形态,虚线?实线?。。。
17.offset:对指定的条形图中某个条形向上或向下位移。
18.ann: 是否展示(main, sub, xlab, ylab) 。
19.args.legend:值为列表,列表内包含legend()函数中的参数,可以对条形图的图例进行细节设置。
20.data,subset:数据集,数据集的子集。
21.na.action:对NA值进行的处理方式。
22.add:是否加入到别的图中。
23.inside此参数,目前还没发现有什么用!,按照帮助文档中的方法使用即可。
示例数据:
a <- c(1,2,3,4,3,2,1)
names(a) <- c('a','b','c','d','e','f','g')
b <- matrix(c(1,2,3,4,2,3,1,4,1),nrow = 3,,byrow = T)
colnames(b) <- c('c1','c2','c3')
rownames(b) <- c('r1','r2','r3')
#运行a,b查看数据
> a
a b c d e f g
1 2 3 4 3 2 1
> b
c1 c2 c3
r1 1 2 3
r2 4 2 3
r3 1 4 1
二、barplot参数详解
1.height,main,sub
height即为数据来源,代表频数,可为向量或者矩阵。
main参数设置标题,sub设置副标题,在图最下方(后续不再介绍)
代码如下:
opar <- par(mfrow=c(1,2))
barplot(height = a,main = 'a',sub = '向量')
barplot(height = b,main = 'b',sub = '矩阵')
2.width
设置条形的宽度,可以发现设置单个值时不会改变宽度,除非设置xlim(此处为字符形式的横坐标,所以未进一步探索)
代码如下:
##设置图形布局
opar <- par(mfrow=c(2,2))
barplot(height = a,main = 'width=c(0.5,1)',sub = 'a:向量',width = c(0.5,1))
barplot(height = a,main = 'width=0.5',sub = 'a:向量',width = 0.5)
barplot(height = a,main = 'width=1',sub = 'a:向量',width = 1)
barplot(height = b,main = 'width=c(0.5,1)',sub = 'b:矩阵',width = c(0.5,1))
3.space
若height为向量,space为条形左边的空隙(空隙=平均条形宽 × sapce)。若height是矩阵,beside=TRUE,也即展示的是多组别observations的条形图时,space若为c(m,n),则m为组内的间隙,n为组间的间隙。
代码如下:
##设置图形布局
opar <- par(mfrow=c(2,2))
barplot(height = a,main = 'space=c(0,1,2,3)',sub = 'a:向量',space = c(0,1,2,3))
barplot(height = a,main = 'space=1',sub = 'a:向量',space = 1)
barplot(height = b,main = 'space=c(0.5,3)',sub = 'b:矩阵',space = c(0.5,3),
beside = T,col = c('red','lightblue','green') )
legend(legend = rownames(b),x = 15,y=4,fill = c('red','lightblue','green'),cex = 0.6,horiz = T)
barplot(height = b,main = 'space=c(3,0.5)',sub = 'b:矩阵',space = c(3,0.5),
beside = T,col = c('red','lightblue','green') )
legend(legend = rownames(b),x = 20,y=4,fill = c('red','lightblue','green'),cex = 0.6,horiz = T)
4.names.arg
height为向量时:names.arg为各条形命名
height为矩阵时:names.arg为各组命名
代码如下:
opar <- par(mfrow=c(2,2))
barplot(height = a,main = 'names.arg = c(1,2,3,4,5,6,7)',sub = 'a:向量',names.arg = c(1,2,3,4,5,6,7))
barplot(height = a,main = 'names.arg = c(7,6,5,4,3,2,1)',sub = 'a:向量',names.arg = c(7,6,5,4,3,2,1))
barplot(height = b,main = 'names.arg = c(1,2,3)',sub = 'b:矩阵',beside = T,names.arg = c(1,2,3))
barplot(height = b,main = 'names.arg = c(3,2,1)',sub = 'b:矩阵',beside = T,names.arg = c(3,2,1))
5.legend.text
自动根据条形图顺序设置图例,且legend.text对应着图例的标签名,若legend.text为TRUE,则图例标签名对应与向量的names或矩阵的行名
代码如下:
opar <- par(mfrow=c(2,2))
barplot(height = a,main = 'legend.text = c(1,2,3,4,5,6,7)',sub = 'a:向量',col = c('red','yellow','blue','lightblue','green','orange','purple'),legend.text = c(1,2,3,4,5,6,7))
barplot(height = a,main = 'legend.text = T',sub = 'a:向量',legend.text = T)
barplot(height = b,main = 'legend.text = c(1,2,3)',col = c('red','yellow','blue'),sub = 'b:矩阵',beside = T,legend.text = c(1,2,3))
barplot(height = b,main = 'legend.text = T',sub = 'b:矩阵',beside = T,legend.text = T)
6.beside
是否堆叠
代码如下:
opar <- par(mfrow=c(1,2))
barplot(height = b,main = 'beside = F',sub = 'b:矩阵',beside = F,legend.text = T)
barplot(height = b,main = 'beside = T',sub = 'b:矩阵',beside = T,legend.text = T)
7.horiz
是否横置
代码如下:
opar <- par(mfrow=c(2,2))
barplot(height = a,main = 'horiz = F',sub = 'a:向量',horiz = F)
barplot(height = a,main = 'horiz = T',sub = 'a:向量',horiz = T)
barplot(height = b,main = 'horiz = F',sub = 'b:矩阵',horiz = F,legend.text = T)
barplot(height = b,main = 'horiz = T',sub = 'b:矩阵',horiz = T,legend.text = T)
8.density,angle
设置条形填充的斜线密度及角度
代码如下:
opar <- par(mfrow=c(1,1))
barplot(height = a,main = 'density = c(5,10,20,50,100,50,20) \n angle = c(0,30,60,90,120,150,180)',sub = 'a:向量',density = c(5,10,20,50,100,50,20),angle = c(0,30,60,90,120,150,180))
9.col,border
设置条形填充及边框颜色
代码如下:
opar <- par(mfrow=c(1,2))
barplot(height = a,main = "col = c('red','green','blue')",sub = 'a:向量',col = c('red','green','blue'))
barplot(height = a,main = "border = c('blue','green','red')",col = 'white',sub = 'a:向量',border = c('blue','green','red'))
10.xlab,ylab
设置x,y轴标题
代码如下:
opar <- par(mfrow=c(1,1))
barplot(height = a,main = "xlab = '类别'\nylab = '频数'",sub = 'a:向量',xlab = '类别',ylab = '频数')
11.xlim,ylim,xpd
设置横纵坐标轴限制,xpd:条形图是否能超出绘图范围
代码如下:
opar <- par(mfrow=c(1,3))
barplot(height = a,main = "xlim = c(0,5)\nylim = c(2,4)\nxpd = F",sub = 'a:向量',xlim = c(0,5),ylim = c(2,4),xpd = F)
barplot(height = a,main = "xlim = c(0,5)\nylim = c(2,4)\nxpd = T",sub = 'a:向量',xlim = c(0,5),ylim = c(2,4),xpd = T)
barplot(height = a,main = "默认参数")
12.log
x轴或者y轴是否行log转换
代码如下:
opar <- par(mfrow=c(2,2))
barplot(height = a,main = "log = 'y'",sub = 'a:向量',log = 'y')
barplot(height = a,main = "log = 'xy'",sub = 'a:向量',log = 'xy')
barplot(height = a,main = "默认参数",sub = 'a:向量')
barplot(height = a,main = "log = 'x'",sub = 'a:向量',log = 'x')
13.axes
axes:可控制纵坐标轴的显示
代码如下:
opar <- par(mfrow=c(1,2))
barplot(height = a,main = "axes = T",sub = 'a:向量',axes = T)
barplot(height = a,main = "axes = F",sub = 'a:向量',axes= F)
14.axisnames
设置:是否显示横坐标的名字
代码如下:
opar <- par(mfrow=c(1,2))
barplot(height = a,main = "axisnames = T",sub = 'a:向量',axisnames = T)
barplot(height = a,main = "axisnames = F",sub = 'a:向量',axisnames = F)
15.cex.axis,cex.names
cex.axis设置纵坐标标签大小,cex.names设置横坐标标签的大小。
代码如下:
opar <- par(mfrow=c(2,2))
barplot(height = a,main = "cex.axis = 1",sub = 'a:向量',cex.axis = 1)
barplot(height = a,main = "cex.axis = 2",sub = 'a:向量',cex.axis = 2)
barplot(height = a,main = "cex.names = 1",sub = 'a:向量',cex.names = 1)
barplot(height = a,main = "cex.names = 2",sub = 'a:向量',cex.names = 2)
16.axis.lty
横坐标坐标轴的直线形态
代码如下:
opar <- par(mfrow=c(2,2))
barplot(height = a,main = "axis.lty = 1",sub = 'a:向量',axis.lty = 1)
barplot(height = a,main = "axis.lty = 2",sub = 'a:向量',axis.lty = 2)
barplot(height = a,main = "axis.lty = 3",sub = 'a:向量',axis.lty = 3)
barplot(height = a,main = "axis.lty = 4",sub = 'a:向量',axis.lty = 4)
17.offset
对指定的条形图中某个条形向上或向下位移
代码如下:
barplot(height = a,main = "offset = c(-1,0,1,2,3,4,5)",sub = 'a:向量',offset = c(-1,0,1,2,3,4,5))
18.ann
ann设置条形图是否显示标题,副标题,x,y轴的轴标题
左图ann=F,可发现其没有上述四个内容
代码如下:
opar <- par(mfrow=c(1,2))
barplot(height = a,main = "ann = F",sub = 'a:向量',xlab = 'x',ylab = 'y',ann = F)
barplot(height = a,main = "ann = T",sub = 'a:向量',xlab = 'x',ylab = 'y',ann = T)
19.args.legend
args.legend:值为列表,列表内包含legend()函数中的参数,可以对条形图的图例进行细节设置
代码如下:
barplot(height = b,main = 'horiz = F',sub = 'b:矩阵',horiz = F,legend.text = T,args.legend = list(x=4,y=6,title='group'),xpd = T)
20.data,subset,na.action,add
以上参数不多赘述。
21.inside:
此参数,目前还没发现有什么用!按照帮助文档中的方法使用即可
总结
下一篇写直方图——hist()实力有限,如有错误,欢迎大家批评指正。