# R语言基础教程4：柱形图

• A+

### 一、基本柱形图：

set.seed(1000)
# 查看 airquality 数据集的基本结构
str(airquality)
## 'data.frame':	153 obs. of  6 variables:
##  $Ozone : int 41 36 12 18 NA 28 23 19 8 NA ... ##$ Solar.R: int  190 118 149 313 NA NA 299 99 19 194 ...
##  $Wind : num 7.4 8 12.6 11.5 14.3 14.9 8.6 13.8 20.1 8.6 ... ##$ Temp   : int  67 72 74 62 56 66 65 59 61 69 ...
##  $Month : int 5 5 5 5 5 5 5 5 5 5 ... ##$ Day    : int  1 2 3 4 5 6 7 8 9 10 ...
# 可以看到“airquality”为数据框类型，有NA值。去掉NA，并把它转成matrix数据：
datax <- as.matrix(na.omit(airquality))
str(datax)
##  num [1:111, 1:6] 41 36 12 18 23 19 8 16 11 14 ...
##  - attr(*, "dimnames")=List of 2
##   ..$: chr [1:111] "1" "2" "3" "4" ... ## ..$ : chr [1:6] "Ozone" "Solar.R" "Wind" "Temp" ...
sel <- sample(1:nrow(datax), 5)
y1 <- datax[sel, 1]
y2 <- datax[sel, 2]
y3 <- datax[sel, 3]
labels <- paste("S", 1:length(y1), sep = "")
legs <- c("Ozone", "Solar.R", "Wind")

plot函数绘制散点图和折线图时，我们要使用成对的 x,y 数据来作图， 如果只有提供一个系列的数据，R把它当成y值，x值自动用序号表示；如果type参数设为h，再设置一下线宽和线末端性状就可以得到柱形图：

plot(y1, type = "h", lwd = 15, lend = 2)

barplot(y1)

barplot(y1, ylim = c(0, max(y1) * 1.1), offset = 0, axis.lty = 1, names.arg = labels)
box()

### 二、多系列柱形图

dataxx <- rbind(y1, y2, y3)
ylim <- c(0, max(y1 + y2 + y3) * 1.1)
cols <- terrain.colors(3)
barplot(dataxx, ylim = ylim, offset = 0, axis.lty = 1, names.arg = labels, col = cols)
box()

barplot(as.data.frame(dataxx), ylim = ylim, offset = 0, axis.lty = 1, names.arg = labels,
col = cols)
## Error: 'height' must be a vector or a matrix

ylim <- c(0, max(dataxx) * 1.15)
barplot(dataxx, ylim = ylim, offset = 0, axis.lty = 1, names.arg = labels, col = cols,
beside = TRUE)
box()

barplot(t(dataxx), ylim = ylim, offset = 0, axis.lty = 1, names.arg = legs,
beside = TRUE, col = terrain.colors(length(labels)))
box()

### 三、标题和图例

x <- barplot(dataxx, ylim = ylim, offset = 0, axis.lty = 1, names.arg = labels,
col = cols, beside = TRUE)
box()
legend("topleft", legend = legs, fill = cols, box.col = "transparent")
title(main = "An example of barplot", xlab = "Sample", ylab = "Value")

### 四、添加柱形图误差线：

plot函数是没有返回值的，而barplot函数把柱形图绘制时的x坐标作为返回值。我们前面已经保存了barplot的返回值，看看什么情况：

x
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.5 5.5 9.5 13.5 17.5
## [2,] 2.5 6.5 10.5 14.5 18.5
## [3,] 3.5 7.5 11.5 15.5 19.5
dataxx
## 66 125 17 114 89
## y1 64.0 78.0 34 9.0 82.0
## y2 175.0 197.0 307 36.0 213.0
## y3 4.6 5.1 12 14.3 7.4

x的顺序和绘制柱形图所用数据dataxx的顺序是一样的。

R基本包没有提供误差线绘制的函数，但这完全可以自己做一个，代码很简单，下面用的是arrows函数，也可以用segments函数：

plot.error <- function(x, y, sd, len = 1, col = "black", horiz = FALSE) {
len <- len * 0.05
if (!horiz) {
arrows(x0 = x, y0 = y, x1 = x, y1 = y - sd, col = col, angle = 90, length = len)
arrows(x0 = x, y0 = y, x1 = x, y1 = y + sd, col = col, angle = 90, length = len)
} else {
arrows(x0 = y, y0 = x, x1 = y - sd, y1 = x, col = col, angle = 90, length = len)
arrows(x0 = y, y0 = x, x1 = y + sd, y1 = x, col = col, angle = 90, length = len)
}
}

sd <- dataxx * 0.1
for (i in 1:3) plot.error(x[i, ], dataxx[i, ], sd = sd[i, ])

### 五、barplot的其他参数

axis.lty：用于设置坐标轴线的类型，如果不设置则x轴不绘制

names.arg：设置柱形图下面的标签名

angle：如果柱子内斜线的角度

legend.text：图例的名称。可以在barplot函数里面直接设定。

ncol：设置图例排列的列数，默认为1，即图标竖排。

horiz：横向柱形

x <- barplot(dataxx, xlim = ylim, offset = 0, axis.lty = 1, names.arg = labels,
col = cols, beside = TRUE, horiz = T)
box()
legend("bottomright", legend = legs, fill = cols, box.col = "transparent")
title(xlab = "Value", ylab = "Sample")
sd <- dataxx * 0.1
for (i in 1:3) plot.error(x[i, ], dataxx[i, ], sd = sd[i, ], horiz = TRUE)