R 简明教程
R - Bar Charts
条形图以矩形条形表示数据,条形的长度与变量的值成正比。R 使用 barplot() 函数创建条形图。R 可以在条形图中绘制垂直条形和水平条形。在条形图中,每个条形都可以赋予不同的颜色。
A bar chart represents data in rectangular bars with length of the bar proportional to the value of the variable. R uses the function barplot() to create bar charts. R can draw both vertical and Horizontal bars in the bar chart. In bar chart each of the bars can be given different colors.
Syntax
在 R 中创建条形图的基本语法为:
The basic syntax to create a bar-chart in R is −
barplot(H,xlab,ylab,main, names.arg,col)
以下是所用参数的描述 -
Following is the description of the parameters used −
-
H is a vector or matrix containing numeric values used in bar chart.
-
xlab is the label for x axis.
-
ylab is the label for y axis.
-
main is the title of the bar chart.
-
names.arg is a vector of names appearing under each bar.
-
col is used to give colors to the bars in the graph.
Example
仅使用输入向量和每个条形的名称创建简单的条形图。
A simple bar chart is created using just the input vector and the name of each bar.
如下脚本将在当前 R 工作目录中创建并保存条形图。
The below script will create and save the bar chart in the current R working directory.
# Create the data for the chart
H <- c(7,12,28,3,41)
# Give the chart file a name
png(file = "barchart.png")
# Plot the bar chart
barplot(H)
# Save the file
dev.off()
当我们执行以上代码时,会产生以下结果 -
When we execute above code, it produces following result −

Bar Chart Labels, Title and Colors
可以通过添加更多参数来扩展条形图的功能。 main 参数用于添加 title 。 col 参数用于给条形着色。 args.name 是一个向量,其值数与输入向量相同,用于描述每个条形的含义。
The features of the bar chart can be expanded by adding more parameters. The main parameter is used to add title. The col parameter is used to add colors to the bars. The args.name is a vector having same number of values as the input vector to describe the meaning of each bar.
Example
如下脚本将在当前 R 工作目录中创建并保存条形图。
The below script will create and save the bar chart in the current R working directory.
# Create the data for the chart
H <- c(7,12,28,3,41)
M <- c("Mar","Apr","May","Jun","Jul")
# Give the chart file a name
png(file = "barchart_months_revenue.png")
# Plot the bar chart
barplot(H,names.arg=M,xlab="Month",ylab="Revenue",col="blue",
main="Revenue chart",border="red")
# Save the file
dev.off()
当我们执行以上代码时,会产生以下结果 -
When we execute above code, it produces following result −

Group Bar Chart and Stacked Bar Chart
我们可以通过使用矩阵作为输入值,创建带有条形组和每个条形堆栈的条形图。
We can create bar chart with groups of bars and stacks in each bar by using a matrix as input values.
超过两个变量将表示为一个矩阵,用于创建组条形图和堆叠条形图。
More than two variables are represented as a matrix which is used to create the group bar chart and stacked bar chart.
# Create the input vectors.
colors = c("green","orange","brown")
months <- c("Mar","Apr","May","Jun","Jul")
regions <- c("East","West","North")
# Create the matrix of the values.
Values <- matrix(c(2,9,3,11,9,4,8,7,3,12,5,2,8,10,11), nrow = 3, ncol = 5, byrow = TRUE)
# Give the chart file a name
png(file = "barchart_stacked.png")
# Create the bar chart
barplot(Values, main = "total revenue", names.arg = months, xlab = "month", ylab = "revenue", col = colors)
# Add the legend to the chart
legend("topleft", regions, cex = 1.3, fill = colors)
# Save the file
dev.off()
