English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

R Plot - Bar Chart

Bar chart, also known as column chart, is a statistical chart that uses the length of the rectangle as a variable.

Bar charts can be horizontal or vertical, and each rectangle can have different colors.

R language uses the barplot() function to create a bar chart, the format is as follows:

barplot(H,xlab,ylab,main, names.arg,col,beside)

Parameter Description:

  • H Vector or matrix, containing the numerical values used in the chart, each value represents the height of the bar.

  • xlab X-axis label.

  • ylab Y-axis label.

  • main Chart title.

  • names.arg The name of each bar.

  • col The color of each bar.

Next, we create a simple bar chart:

# 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, open the file to see the graphic effect as follows:

To better express information, we can add titles, colors, and names of each bar to the chart.

Next, we create 2020 Year 7 Month 1 Statistical chart of confirmed cases of COVID-19 in China, the United States and India. 2020 Year

Chinese characters need to set the font parameter family='GB1':

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 a vector or a matrix, now we generate a COVID-19 6 and 7 Monthly comparison chart.

First prepare the data:

 ChinaUnited StatesIndia
6 Month830171794546190535
7 Month835342640626585493

Converted to a matrix, generate a bar chart, displayed in parallel format, and color samples must be displayed.

Here we set our own font library, detailed content can be referred to R Plotting - Chinese Support

library(showtext);
font_add("SyHei", "SourceHanSansSC-Bold.otf");
cvd19 = matrix(
  c(83017, 83534, 1794546, 2640626, 190535, 585493,
  2, 3
,
)
# Set filename, output as png3codebox-bar-1.png)
# Load font
showtext_begin();
colnames(cvd19) = c("China", "United States", "India")
rownames(cvd19) = c("6month", "7month)
barplot(cvd19, main = "COVID-19 Bar Chart", beside=TRUE, legend=TRUE,  family='SimSun')
# Remove font
showtext_end();

The following code will create a file named w in the current program directory3codebox-bar-1.png file, as shown below:


The color samples we set will be the color samples of each group:

library(plotrix)
library(showtext);
font_add("SyHei", "SourceHanSansSC-Bold.otf");
cvd19 = matrix(
  c(83017, 83534, 1794546, 2640626, 190535, 585493,
  2, 3
,
)
# Set filename, output as png3codebox-bar-2.png)
# Load font
showtext_begin();
colnames(cvd19) = c("China", "United States", "India")
rownames(cvd19) = c("6month", "7month)
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 create a file named w in the current program directory3codebox-bar-2.png file, as shown below:

beside parameter

beside sets the stacking method of the rectangular bars, the default is FALSE:

  • beside=FALSE At this time, the height of the bar chart is the value of the matrix, and the rectangular bars are horizontally stacked.

  • beside=TRUE At this time, the height of the bar chart is the value of the matrix, and the rectangular bars are side by side.

library(showtext);
font_add("SyHei", "SourceHanSansSC-Bold.otf");
cvd19 = matrix(
  c(83017, 83534, 1794546, 2640626, 190535, 585493,
  2, 3
,
)
# Set filename, output as png3codebox-bar-3.png)
# Load font
showtext_begin();
colnames(cvd19) = c("China", "United States", "India")
rownames(cvd19) = c("6month", "7month)
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 create a file named w in the current program directory3codebox-bar-3.png file, as shown below: