# R条形图示例图解

## 本文概述

``barplot(h, x, y, main, names.arg, col)``
S.No Parameter Description
1. H 包含条形图中使用的数值的向量或矩阵。
2. xlab x轴的标签。
3. ylab y轴的标签。
4. main 条形图的标题。
5. names.arg 每个条形下出现的名称的向量。
6. col 用于为图表中的条形赋予颜色。

### 例子

``````# Creating the data for Bar chart
H<- c(12, 35, 54, 3, 41)
# Giving the chart file a name
png(file = "bar_chart.png")
# Plotting the bar chart
barplot(H)
# Saving the file
dev.off()``````

## 标签, 标题和颜色

### 例子

``````# Creating the data for Bar chart
H <- c(12, 35, 54, 3, 41)
M<- c("Feb", "Mar", "Apr", "May", "Jun")

# Giving the chart file a name
png(file = "bar_properties.png")

# Plotting the bar chart
barplot(H, names.arg=M, xlab="Month", ylab="Revenue", col="Green", main="Revenue Bar chart", border="red")
# Saving the file
dev.off()``````

## 组条形图和堆积条形图

### 例子

``````library(RColorBrewer)
months <- c("Jan", "Feb", "Mar", "Apr", "May")
regions <- c("West", "North", "South")
# Creating the matrix of the values.
Values <- matrix(c(21, 32, 33, 14, 95, 46, 67, 78, 39, 11, 22, 23, 94, 15, 16), nrow = 3, ncol = 5, byrow = TRUE)
# Giving the chart file a name
png(file = "stacked_chart.png")
# Creating the bar chart
barplot(Values, main = "Total Revenue", names.arg = months, xlab = "Month", ylab = "Revenue", col =c("cadetblue3", "deeppink2", "goldenrod1"))
# Adding the legend to the chart
legend("topleft", regions, cex = 1.3, fill = c("cadetblue3", "deeppink2", "goldenrod1"))

# Saving the file
dev.off()``````

• 回顶