R Bar Charts
A bar chart, also known as a bar graph, is a statistical chart that uses the length of rectangles as a variable.
Bar charts can be horizontal or vertical, and each rectangle can have a different color.
The R language uses the `barplot()` function to create bar charts, with the following format:
```r
barplot(H, xlab, ylab, main, names.arg, col, beside)
Parameter Description:
* **H**: A vector or matrix containing the numerical values for the chart. Each value represents the height of a rectangular bar.
* **xlab**: Label for the x-axis.
* **ylab**: Label for the y-axis.
* **main**: Title of the chart.
* **names.arg**: Names for each rectangular bar.
* **col**: Colors for each rectangular bar.
Next, we create a simple bar chart:
## Example
```r
# Prepare a vector
cvd19 = c(83534, 2640626, 585493)
# Display the bar chart
barplot(cvd19)
When you run the plotting program, it will generate a PDF file (Rplots.pdf) in the current directory. Opening the file will show the graphical effect as follows:
!(#)
To better convey information, we can add a title, colors, and names for each rectangular bar to the chart.
Below, we create a statistical chart of confirmed COVID-19 cases in China, the United States, and India as of July 1, 2020.
For Chinese fonts, you need to set the font parameter `family='GB1'`:
## Example
```r
cvd19 = c(83534, 2640626, 585493)
barplot(cvd19,
main = "COVID-19 Bar Chart",
col = c("#ED1C24", "#22B14C", "#FFC90E"),
names.arg = c("China", "USA", "India"),
family = 'GB1'
)
!(#)
The data in `barplot` can be either a vector or a matrix. Now, let's generate a comparison chart of COVID-19 cases for June and July.
First, prepare the data:
| | China | USA | India |
|-------|-------|----------|--------|
| June | 83017 | 1794546 | 190535 |
| July | 83534 | 2640626 | 585493 |
Convert it into a matrix and generate a bar chart in a side-by-side format, also displaying a color legend.
Here, we set our own font library. For details, please refer to (#).
## Example
```r
library(showtext);
font_add("SyHei", "SourceHanSansSC-Bold.otf");
cvd19 = matrix(
c(83017, 83534, 1794546, 2640626, 190535, 585493),
2, 3
)
# Set the filename, output as png
png(file = "-bar-1.png")
# Load the font
showtext_begin();
colnames(cvd19) = c("China", "USA", "India")
rownames(cvd19) = c("June", "July")
barplot(cvd19, main = "COVID-19 Bar Chart", beside = TRUE, legend = TRUE, family = 'SyHei')
# Unload the font
showtext_end();
The following code will generate a `-bar-1.png` file in the current program directory, as shown below:
!(#)
The color legend we set will be the color sample for each group:
## Example
```r
library(plotrix)
library(showtext);
font_add("SyHei", "SourceHanSansSC-Bold.otf");
cvd19 = matrix(
c(83017, 83534, 1794546, 2640626, 190535, 585493),
2, 3
)
# Set the filename, output as png
png(file = "-bar-2.png")
# Load the font
showtext_begin();
colnames(cvd19) = c("China", "USA", "India")
rownames(cvd19) = c("June", "July")
barplot(cvd19, main = "COVID-19 Bar Chart", beside = TRUE, legend = TRUE, col = c("blue", "green"), family = 'SyHei')
# Unload the font
showtext_end();
The following code will generate a `-bar-2.png` file in the current program directory, as shown below:
!(#)
### The `beside` Parameter
The `beside` parameter sets the stacking method of the rectangular bars. The default is `FALSE`:
* When **`beside=FALSE`**, the height of the bar chart is the value of the matrix, and the rectangular bars are stacked horizontally.
* When **`beside=TRUE`**, the height of the bar chart is the value of the matrix, and the rectangular bars are placed side-by-side.
## Example
```r
library(showtext);
font_add("SyHei", "SourceHanSansSC-Bold.otf");
cvd19 = matrix(
c(83017, 83534, 1794546, 2640626, 190535, 585493),
2, 3
)
# Set the filename, output as png
png(file = "-bar-3.png")
# Load the font
showtext_begin();
colnames(cvd19) = c("China", "USA", "India")
rownames(cvd19) = c("June", "July")
barplot(cvd19, main = "COVID-19 Bar Chart", beside = FALSE, legend = TRUE, col = c("blue", "green"), family = 'SyHei')
# Unload the font
showtext_end();
The following code will generate a `-bar-3.png` file in the current program directory, as shown below:
!(#)
YouTip