R Plotting - Bar Charts
A bar chart, also called a bar graph, is a statistical chart that uses the length of rectangles as the variable.
Bar charts can be horizontal or vertical, and each rectangle can have a different color.
R uses the barplot() function to create bar charts, with the following format:
barplot(H,xlab,ylab,main, names.arg,col,beside)
Parameter Description:
- H A vector or matrix containing the numeric values for the chart; each value represents the height of a bar.
- xlab Label for the x-axis.
- ylab Label for the y-axis.
- main Chart title.
- names.arg Name of each bar.
- col Color of each bar.
Next, we create a simple bar chart:
Example
Prepare a vector
cvd19
=
c
(
83534
,
2640626
,
585493
)
Display the bar chart
barplot
(
cvd19
)
Running the plotting program creates a PDF file (Rplots.pdf) in the current directory; opening the file shows the following graphic:

To convey information better, we can add a title, colors, and the name of each bar to the chart.
Below we create a chart of confirmed COVID-19 cases in China, the United States, and India on July 1, 2020.
For Chinese fonts, you need to set 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"
,
"United States"
,
"India"
)
,
family
=
'GB1'
)

The data in barplot can be either a vector or a matrix; now we generate a comparison chart of COVID-19 cases for June and July.
First, prepare the data:
| China | United States | India | |
|---|---|---|---|
| June | 83017 | 1794546 | 190535 |
| July | 83534 | 2640626 | 585493 |
Convert to a matrix, generate the bar chart, display it side by side, and show the color legend.
Here we set up 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 the filename and output as png
png
(
file
=
"runoob-bar-1.png"
)
# Load font
showtext_begin
(
)
;
colnames
(
cvd19
)
=
c
(
"China"
,
"United States"
,
"India"
)
rownames
(
cvd19
)
=
c
(
"June"
,
"July"
)
barplot
(
cvd19, main
=
"COVID-19 Bar Chart"
, beside
=
TRUE,
legend
=
TRUE,
family
=
'SyHei'
)
Remove the font
showtext_end
(
)
;
The following code creates a runoob-bar-1.png file in the current program directory, as shown below:

The color legend we set will be the color legend 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 the filename and output as png
png
(
file
=
"runoob-bar-2.png"
)
# Load font
showtext_begin
(
)
;
colnames
(
cvd19
)
=
c
(
"China"
,
"United States"
,
"India"
)
rownames
(
cvd19
)
=
c
(
"June"
,
"July"
)
barplot
(
cvd19, main
=
"COVID-19 Bar Chart"
, beside
=
TRUE,
legend
=
TRUE,
col
=
c
(
"blue"
,
"green"
)
,
family
=
'SyHei'
)
Remove the font
showtext_end
(
)
;
The following code creates a runoob-bar-2.png file in the current program directory, as shown below:

The beside parameter
beside sets how the bars are stacked; the default is FALSE:
- beside=FALSE When set, the height of the bars is the matrix values, and the bars are stacked horizontally.
- beside=TRUE When set, the height of the bars is the matrix values, and the bars are placed side by side.
Example
library
(
showtext
)
;
font_add
(
"SyHei"
,
"SourceHanSansSC-Bold.otf"
)
;
cvd19
=
matrix
(
c
(
83017
,
83534
,
1794546
,
2640626
,
190535
,
585493
)
,
2
,
3
)
Set the filename and output as png
png
(
file
=
"runoob-bar-3.png"
)
# Load font
showtext_begin
(
)
;
colnames
(
cvd19
)
=
c
(
"China"
,
"United States"
,
"India"
)
rownames
(
cvd19
)
=
c
(
"June"
,
"July"
)
barplot
(
cvd19, main
=
"COVID-19 Bar Chart"
, beside
=
FALSE,
legend
=
TRUE,
col
=
c
(
"blue"
,
"green"
)
,
family
=
'SyHei'
)
Remove the font
showtext_end
(
)
;
The following code creates a runoob-bar-3.png file in the current program directory, as shown below:
