R Plotting - Bar Chart
A bar chart, also known as a bar graph, is a statistical graph that uses the length of rectangular bars to represent variables.
Bar charts can be horizontal or vertical, and each bar can have a different color.
In R, the barplot()
function is used to create bar charts, with the following format:
barplot(H, xlab, ylab, main, names.arg, col, beside)
Parameter descriptions:
- H: A vector or matrix containing numeric values used in the chart, where each value represents the height of a bar.
- xlab: The label for the x-axis.
- ylab: The label for the y-axis.
- main: The title of the chart.
- names.arg: The names for each bar.
- col: The color for each bar.
Next, we create a simple bar chart:
Example
# Prepare a vector
cvd19 = c(83534, 2640626, 585493)
# Display the bar chart
barplot(cvd19)
Executing the plotting program will generate a PDF file (Rplots.pdf) in the current directory. Opening the file will show the graphical output as follows:
To better convey information, we can add a title, colors, and names for each bar on the chart.
Below, we create a bar chart showing the number of COVID-19 cases in China, the USA, and India on July 1, 2020.
Chinese characters require setting the font parameter family='GB1'
:
Example
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, we generate a bar chart comparing COVID-19 cases in June and July.
First, prepare the data:
China | USA | India | |
---|---|---|---|
June | 83017 | 1794546 | 190535 |
July | 83534 | 2640626 | 585493 |
Convert to a matrix, generate the bar chart, and display in a side-by-side format with color samples.
Here, we set our own font library. For details, refer to R Plotting - Chinese Support.
Example
library(showtext);
font_add("SyHei", "SourceHanSansSC-Bold.otf");
cvd19 = matrix(
c(83017, 83534, 1794546, 2640626, 190535, 585493),
2, 3
)
# Set file name, output as png
png(file = "tutorialpro-bar-1.png")
# Load 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')
# Remove font
showtext_end();
The following code will generate a tutorialpro-bar-1.png file in the current program directory, as shown below:
We set the color samples to be for each group:
Example
library(plotrix)
library(showtext);
font_add("SyHei", "SourceHanSansSC-Bold.otf");
cvd19 = matrix(
c(83017, 83534, 1794546, 2640626, 190535, 585493),
2, 3
)
# Set file name, output as png
png(file = "tutorialpro-bar-2.png")
# Load 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')
# Remove font
showtext_end();
The following code will generate a tutorialpro-bar-2.png file in the current program directory, as shown below:
beside Parameter
The beside
parameter sets the stacking method for the bars, with the default being FALSE:
- beside=FALSE: The heights of the bars are the values of the matrix, and the bars are stacked horizontally.
- beside=TRUE: The heights of the bars are the values of the matrix, and the bars are displayed side by side.
library(showtext); font_add("SyHei", "SourceHanSansSC-Bold.otf"); cvd19 = matrix( c(83017, 83534, 1794546, 2640626, 190535, 585493), 2, 3 ) # Set file name, output as png png(file = "tutorialpro-bar-3.png") # Load 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') # Remove font showtext_end();
The following code will generate a tutorialpro-bar-3.png file in the current program directory, as shown below: