barplot函数--R语言

1. 函数功能:

Creates a bar plot with vertical or horizontal bars.

绘制水平方向的条形图或者垂直方向的柱状图

2. 函数语法

barplot(height, width = 1, space = NULL,
        names.arg = NULL, legend.text = NULL, beside = FALSE,
        horiz = FALSE, density = NULL, angle = 45,
        col = NULL, border = par("fg"),
        main = NULL, sub = NULL, xlab = NULL, ylab = NULL,
        xlim = NULL, ylim = NULL, xpd = TRUE, log = "",
        axes = TRUE, axisnames = TRUE,
        cex.axis = par("cex.axis"), cex.names = par("cex.axis"),
        inside = TRUE, plot = TRUE, axis.lty = 0, offset = 0,
        add = FALSE, ann = !add && par("ann"), args.legend = NULL, ...)

3 .函数参数

3.1: height

height	
either a vector or matrix of values describing the bars which make up the plot. 
If height is a vector, the plot consists of a sequence of rectangular bars 
with heights given by the values in the vector. 
If height is a matrix and beside is FALSE then each bar of the plot 
corresponds to a column of height, with the values in the column 
giving the heights of stacked sub-bars making up the bar. 
If height is a matrix and beside is TRUE, 
then the values in each column are juxtaposed rather than stacked.

条块的高度,必须为向量或矩阵

3.1.1 当height为向量

> # 当height为向量
> a <- sample(20:90,5)
> a
[1] 90 61 26 51 36
> barplot(height=a)

barplot函数--R语言_第1张图片

3.2 :beside

beside	
a logical value. If FALSE, the columns of height are portrayed as stacked bars, 
and if TRUE the columns are portrayed as juxtaposed bars.

逻辑值。 如果为FALSE,则将绘制堆积柱状图;如果为TRUE,则绘制并列柱状图。

3.1.2 当height为矩阵,默认beside=FALSE,展示堆积柱状图

> # 当height为矩阵
> a <- sample(20:80,15)
> a
 [1] 55 32 49 58 25 80 37 21 57 72 35 60 75 39 71
> b <- matrix(a,nrow=3)
> b
     [,1] [,2] [,3] [,4] [,5]
[1,]   55   58   37   72   75
[2,]   32   25   21   35   39
[3,]   49   80   57   60   71
> barplot(height=b)

barplot函数--R语言_第2张图片

3.1.2 当height为矩阵,默认beside=TRUE,展示并列柱状图

barplot(height=b,beside=TRUE)

barplot函数--R语言_第3张图片

3.3 :width

width	
optional vector of bar widths. Re-cycled to length the number of bars drawn. 
Specifying a single value will have no visible effect unless xlim is specified.

条块的宽度。这个参数的值是可以循环使用的,当我们只设置一个值时,会自动循环,生成长度和柱子数目相同的向量,效果就是所有柱子的宽度是一样的,其实也可以指定多个值,使每个柱子的长度不一样。除非指定了xlim,否则指定单个值将没有可见的效果。

3.3.1 未指定xlim,设定单个width值看不出效果

> a <- sample(10:20,5)
> a
[1] 14 15 20 12 11
> barplot(a)

barplot函数--R语言_第4张图片

> barplot(a,width=2)

barplot函数--R语言_第5张图片
当数据为矩阵:

> barplot(height=b,beside=TRUE,width=5)

barplot函数--R语言_第6张图片
当width为向量,width的宽度与矩阵的行数保持一致

barplot(height=b,beside=TRUE,width=1:3)

barplot函数--R语言_第7张图片

 barplot(a,width=1:5)

barplot函数--R语言_第8张图片

3.4: space

space	
the amount of space (as a fraction of the average bar width) left before each bar. 
May be given as a single number or one number per bar. 
If height is a matrix and beside is TRUE, space may be specified by two numbers, 
where the first is the space between bars in the same group, 
and the second the space between the groups. 
If not given explicitly, it defaults to c(0,1) if height is a matrix and beside is TRUE, 
and to 0.2 otherwise.

每个条形柱子左侧间隔。可以市单个数值也可以是为每根柱子制定间隔的向量。若height参数为矩阵形式,且为并列设置,间隔可通过两个数字指定,第一个数字为组内柱子之间的间隔,第二个数字为组间柱子的间隔。这个值为一个百分比,对于向量型数据默认值为0.2, 实际的间隔 = 0.2 * 1(所有柱子的平均宽度) = 0.2。对于矩阵型数据默认值为c(0,1),即组内间隔为0,组间间隔比例为1

3.4.1 向量型数据

barplot(a,width=1) #默认0.2
barplot(a,width=1,space=0.4)
barplot(a,width=1,space=1)

展示图形分别如下:
barplot函数--R语言_第9张图片
barplot函数--R语言_第10张图片

barplot函数--R语言_第11张图片

3.4.2 矩阵型数据

默认情况,矩阵型取值为c(0,1)

# 3.3.1 矩阵型数据且beside=T
barplot(d,width=1,beside=T) #默认c(0,1)

barplot函数--R语言_第12张图片
组内间隔0.2倍,组间间隔1倍

barplot(d,width=1,beside=T,space=c(0.2,1))

barplot函数--R语言_第13张图片
组内间隔柱子宽度的0.5倍,组间间隔1.2倍

barplot(d,width=1,beside=T,space=c(0.5,1.2))

barplot函数--R语言_第14张图片

3.5 :names.arg

names.arg	
a vector of names to be plotted below each bar or group of bars. 
If this argument is omitted, then the names are taken from 
the names attribute of height if this is a vector, 
or the column names if it is a matrix.

每根柱子下面的名称,若省略,对于height参数为向量来说,将取向量的names属性作为柱子名称;对于height参数为矩阵来说,则列名为柱子名称

3.5.1 height为向量

names(a) <- c('li','wang','sun','zhao','qin')
a
barplot(a,width=1)

barplot函数--R语言_第15张图片

3.5.2 height为矩阵

d <- matrix(b,nrow=3,dimnames=list(c('r1','r2','r3'),
                                   c('c1','c2','c3','c4','c5')))

barplot(d,width=1,beside=T,space=c(0.2,1))

barplot函数--R语言_第16张图片

3.6:legend.text

legend.text	
a vector of text used to construct a legend for the plot, 
or a logical indicating whether a legend should be included. 
This is only useful when height is a matrix. 
In that case given legend labels should correspond to the rows of height; 
if legend.text is true, the row names of height will be used as labels 
if they are non-null.

图例的文字说明, 只有当height参数的值是matrix 时,才起作用,默认显示的是matrix的rownames 属性,其值有两种指定形式,

第一种:逻辑值,是否需要显示图例,默认为NULL

图例的文字说明, 只有当height参数的值是matrix 时,才起作用,默认显示的是matrix的rownames 属性,其值有两种指定形式,

第一种:逻辑值,是否需要显示图例,默认为FALSE

barplot函数--R语言_第17张图片

barplot(d,width=1,beside=T,space=c(0.2,1),
        legend.text=c('math','English','Chinese') )

barplot函数--R语言_第18张图片

3.7 :horiz

horiz	
a logical value. If FALSE, the bars are drawn vertically with the first bar to the left. If TRUE, the bars are drawn horizontally with the first at the bottom.

逻辑值,默认取值FALSE,绘制垂直的柱形图,第一根柱子在左侧;若取值为TRUE,绘制水平的条形图,第一根柱子在底部。

barplot(d,width=1,beside=T,space=c(0.2,1),
        legend.text=c('math','English','Chinese'),
        horiz = TRUE)

barplot函数--R语言_第19张图片

barplot(a,width=1,horiz=T)

barplot函数--R语言_第20张图片

3.8 :density

density	
a vector giving the density of shading lines, in lines per inch, 
for the bars or bar components. 
The default value of NULL means that no shading lines are drawn. 
Non-positive values of density also inhibit the drawing of shading lines.

密度:
指定柱子中斜线的密度(每英寸),默认值为NULL,无斜线填充

barplot(a,width=1,horiz=T,density = 8)

barplot函数--R语言_第21张图片

barplot(d,width=1,beside=T,space=c(0.2,1),
        legend.text=c('math','English','Chinese'),
        horiz = TRUE,density = 4)

barplot函数--R语言_第22张图片

3.9: angle

angle	
the slope of shading lines, given as an angle in degrees (counter-clockwise), 
for the bars or bar components.

阴影斜线的角度,与density参数搭配使用

barplot(a,width=1,horiz=T,density = 10,angle=90)

barplot函数--R语言_第23张图片

barplot(a,width=1,horiz=T,density=10,angle=60)

barplot函数--R语言_第24张图片

3.10: col

col	
a vector of colors for the bars or bar components. 
By default, grey is used if height is a vector, 
and a gamma-corrected grey palette if height is a matrix.

柱子颜色,若height参数为向量,默认为灰色;若height参数为矩阵,则使用经过伽玛校正的灰色调色板。

3.10.1 当height为向量

barplot(a,width=1,horiz=T,density=10,angle=60,col='yellow')

barplot函数--R语言_第25张图片
当没有斜线时,填充整个柱子区域

barplot(a,width=1,horiz=T,col='yellow')

barplot函数--R语言_第26张图片

barplot(a,width=1,horiz=T,col=c('yellow','green'))

barplot函数--R语言_第27张图片

3.10.2 当height为矩阵


barplot(d,width=1,beside=T,
        legend.text=c('math','English','Chinese'),
        horiz = TRUE,density = 14,
        col=c('yellow','green','red'))

barplot函数--R语言_第28张图片

barplot(d,width=1,beside=T,
        legend.text=c('math','English','Chinese'),
        horiz = TRUE,
        col='red')

barplot函数--R语言_第29张图片

3.11 :border

border	
the color to be used for the border of the bars. 
Use border = NA to omit borders. 
If there are shading lines, border = TRUE means use the same colour 
for the border as for the shading lines.

柱子边界的颜色。当border=NA ,意味着删除边界线;若border=TRUE,则意味着对边框使用与阴影线相同的颜色

barplot(a,width=1,horiz=T,col=c('yellow','green','red'),
        border='blue')

barplot函数--R语言_第30张图片
border=NA,删除边界

barplot(a,width=1,horiz=T,col=c('yellow','green','red'),
        border=NA)

barplot函数--R语言_第31张图片

barplot(a,width=1,horiz=T,density=10,col=c('yellow','green','red'), border=T)

barplot函数--R语言_第32张图片

barplot(d,width=1,beside=T,
        legend.text=c('math','English','Chinese'),
        horiz = TRUE,density = 14,
        col=c('yellow','green','red'),
        border=T)

barplot函数--R语言_第33张图片

3.12: main,sub

main,sub	
overall and sub title for the plot.

图形的主标题和次标题

barplot(d,width=1,beside=T,
        legend.text=c('math','English','Chinese'),
        horiz = TRUE,density = 14,
        col=c('yellow','green','red'),
        border=T,main='分数')

barplot函数--R语言_第34张图片

barplot(d,width=1,beside=T,
        legend.text=c('math','English','Chinese'),
        horiz = TRUE,density = 14,
        col=c('yellow','green','red'),
        border=T,main='分数',sub='图1')

barplot函数--R语言_第35张图片

3.13: xlab ;ylab

xlab	
a label for the x axis.

ylab	
a label for the y axis.

x,y轴标签

barplot(a,width=1,horiz=F,density=10,col=c('yellow','green','red'),
        border=T,xlab='name',ylab='score')

barplot函数--R语言_第36张图片

3.14:xlim,ylim

xlim	
limits for the x axis.

ylim	
limits for the y axis.

x轴边界,y轴边界

barplot(a,width=1,horiz=F,density=10,col=c('yellow','green','red'),
        border=T,xlab='name',ylab='score',
        ylim=c(0,50))

barplot函数--R语言_第37张图片

3.15:xpd

xpd	
logical. Should bars be allowed to go outside region?

逻辑值,是否允许柱子延伸到区域之外,默认取值为T

barplot(a,width=1,horiz=F,density=10,col=c('yellow','green','red'),
        border=T,xlab='name',ylab='score',
        ylim=c(0,30),xpd=T)

barplot函数--R语言_第38张图片

barplot(a,width=1,horiz=F,density=10,col=c('yellow','green','red'),
        border=T,xlab='name',ylab='score',
        ylim=c(0,30),xpd=F)

barplot函数--R语言_第39张图片

3.16 :log

log	
string specifying if axis scales should be logarithmic
a character string which contains "x" if the x axis is to be logarithmic, 
"y" if the y axis is to be logarithmic 
and "xy" or "yx" if both axes are to be logarithmic.

指定对数化的坐标轴,当取值为x,则x轴坐标对数化;当取值为y,则y轴坐标对数化;当取值为xy或者yx,则x,y轴坐标被对数化。

注意: 当使用log参数时,不能有阴影线条density参数

> barplot(a,width=1,horiz=F,density=10,col=c('yellow','green','red'),
+         border=T,xlab='name',ylab='score',log='x')

Error in barplot.default(a, width = 1, horiz = F, density = 10, col = c("yellow",  : 
  用对数尺度时条里不能用带阻影的线条

当不取对数时:

a <-c(exp(1),exp(2),exp(3),exp(4),exp(5))
a
names(a) <- c('li','wang','sun','zhao','qin')
a
barplot(a,width=1,horiz=F,col=c('yellow','green','red'),
        border=T,xlab='name',ylab='score')

barplot函数--R语言_第40张图片

当对y轴取对数

barplot(a,width=1,horiz=F,col=c('yellow','green','red'),
        border=T,xlab='name',ylab='score',log='y')

barplot函数--R语言_第41张图片

3.17:axes

axes	
logical. If TRUE, a vertical (or horizontal, 
if horiz is true) axis is drawn.

逻辑值,是否显示刻度线;当绘制垂直的柱形图时,若axes取值为TRUE,则绘制垂直的刻度线(默认绘制),axes取值为FALSE,不显示垂直方向的刻度线;当绘制水平方向的条形图(horiz=T),则axes取值为TRUE,则绘制水平的刻度线,axes取值为FALSE,不绘制水平方向的刻度线

当绘制柱形图(horiz=F),且axes参数取值为F,不绘制垂直方向的额度线

a <- sample(10:50,5)
a
names(a) <- c('li','wang','sun','zhao','qin')
a
barplot(a,width=1,horiz=F,col=c('yellow','green','red'),
        border=T,xlab='name',ylab='score',axes=F)

barplot函数--R语言_第42张图片
当绘制柱形图(horiz=F),且axes参数取值为T,绘制垂直方向的额度线

barplot(a,width=1,horiz=F,col=c('yellow','green','red'),
        border=T,xlab='name',ylab='score',axes=T)

barplot函数--R语言_第43张图片
当绘制条形图(horiz=T),且axes参数取值为T,绘制水平方向的额度线

barplot(a,width=1,horiz=T,col=c('yellow','green','red'),
        border=T,xlab='name',ylab='score',axes=T)

barplot函数--R语言_第44张图片

barplot(a,width=1,horiz=T,col=c('yellow','green','red'),
        border=T,xlab='name',ylab='score',axes=F)

barplot函数--R语言_第45张图片

3.18:axisnames

axisnames	
logical. If TRUE, and if there are names.arg (see above), 
the other axis is drawn (with lty = 0) and labeled.

逻辑值,若取值为TRUE,且已有每根柱子的名称,则,另一根轴将被绘制且赋予标签

3.19: cex.axis;cex.names

cex.axis	
expansion factor for numeric axis labels.

cex.names	
expansion factor for axis names (bar labels).

3.20:inside

inside	
logical. If TRUE, the lines which divide adjacent (non-stacked!) bars will be drawn. Only applies when space = 0 (which it partly is when beside = TRUE).

3.21: plot

plot	
logical. If FALSE, nothing is plotted.

逻辑值,当取值为FALSE时,不绘制图形

3.22:axis.lty

axis.lty	
the graphics parameter lty applied to the axis and tick marks of 
the categorical (default horizontal) axis. 
Note that by default the axis is suppressed.

3.23 :offset

offset	
a vector indicating how much the bars should be shifted 
relative to the x axis.

设置柱子的起始位置在y轴上的位置, 默认值为0

a <- sample(10:50,3)
a
names(a) <- c('li','wang','sun')
a
barplot(a,width=1,horiz=T,col=c('yellow','green','red'),
        border=T,offset=10)

barplot函数--R语言_第46张图片

3.24:add

add	
logical specifying if bars should be added to 
an already existing plot; defaults to FALSE.

官方文档: barplot

你可能感兴趣的:(R)