A bar chart represents data in rectangular bars with length of the bar proportional to the value of the variable. R uses the function barplot( ) to create bar charts. R can draw both vertical and horizontal bars in the bar chart. In bar chart each of the bars can be given different colors. A bar graph is a chart that uses bars to show comparisons between categories of data. A bar graph will have two axes. One axis will describe the types of categories being compared, and the other will have numerical values that represent the values of the data. It does not matter which axis is which, but it will determine what bar graph is shown. If the descriptions are on the horizontal axis, the bars will be oriented vertically, and if the values are along the horizontal axis, the bars will be oriented horizontally.

Syntax:- barplot(H, xlab, ylab, main, names.arg, col)

To explain the bar plot, let us use a dataset mtcars.

This dataset comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973–74 models).

head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
str(mtcars)
## 'data.frame':    32 obs. of  11 variables:
##  $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
##  $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
##  $ disp: num  160 160 108 258 360 ...
##  $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
##  $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
##  $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
##  $ qsec: num  16.5 17 18.6 19.4 17 ...
##  $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
##  $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
##  $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
##  $ carb: num  4 4 1 1 2 1 4 2 2 4 ...

Now, let’s consider the gear colomn of mtcars and plot it using barplot() function.

1. Basic bar plot.

Example 1: Draw a simple bar plot of gear colomn of mtcars table.

counts<-table(mtcars$gear)
print(counts)
## 
##  3  4  5 
## 15 12  5
barplot(counts,main="car distribution", xlab="number of gears")

Example 2: Some more functionality in barplot function.

counts<-table(mtcars$gear)
print(counts)
## 
##  3  4  5 
## 15 12  5
barplot(counts,main="car Distribution",horiz=TRUE,ylab="number of gears",names.arg=c("3 Gears","4Gears","5 Gears"),col=rainbow(3))

Grouped Bar Plot or multiple bar plot.

Example 3: Plot a bar graph between number of gears and number of cylinder. Use mtcars database.Note: All bars should be plotted separately.

Multiple bar chart is an extension of simple bar chart. Grouped bars are used to represent related sets of data. Each bar in a group is shaded or coloured differently for the sake of distinction. For example below, The grouped bar plot is plotted between number of gears and cylinders of mtcars.If beside=TRUE, then each bar is plotted separately. Else, all are ploted one above another.

print(mtcars$cyl)
##  [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4
print (mtcars$gear)
##  [1] 4 4 4 3 3 3 3 4 4 4 4 3 3 3 3 3 3 4 4 4 3 3 3 3 3 4 5 5 5 5 5 4
counts<-table(mtcars$cyl,mtcars$gear)

print(counts)
##    
##      3  4  5
##   4  1  8  2
##   6  2  4  1
##   8 12  0  2
barplot(counts,xlab="Gears",
        main="Car distribution by Gear and Cylinder",
        col=c("red","blue","green"),
        beside=TRUE, 
        names.arg=c("3 Gears","4Gears","5 Gears"))

legend("topright",c("4 cylinders","6 cylinders","8 cylinders"), fill=c("red","blue","green"))

3. Sub-divided Bar Diagram:-

This chart consists of bars which are sub-divided into two or more parts.Sub-divided bar plot is also known as component bar chart or staked chart.

Example 6: Plot the above graph one above the another.

counts<-table(mtcars$cyl,mtcars$gear)
print(counts)
##    
##      3  4  5
##   4  1  8  2
##   6  2  4  1
##   8 12  0  2
barplot(counts,xlab="Gears",main="Car distribution by Gear and Cylinder",col=c("red","blue","green"),names.arg=c("3 Gears","4Gears","5 Gears"))
legend("topright",c("4 cylinders","6 cylinders","8 cylinders"),fill=c("red","blue","green"))

4. Deviation Bar Plot

A graph displays a deviation relationship when it features how one or more sets of quantitative values differ from a reference set of values. The graph does this by directly expressing the differences between two sets of values

Ex.:Deviation bars are used to represent net quantities - excess or deficit i.e. net profit, net loss, net exports or imports, swings in voting etc. Such bars have both positive and negative values. Positive values lie above the base line and negative values lie below it.

cars <- c(12,-4,56,2,-12,45)
barplot(cars,col="red")

Advantages of bar plot

Disadvantages of bar plot