Looking For Anything Specific?

R Graphics (Using Base Package) || Multiple Bar Diagram || Article - 2

Statistical Knowledge | R-Graphics (Using Base Package) | Article - 2

Hello Friends, Welcome to the second article of my new series of Articles named R Graphics (Using Base Package). The main motto of this series of articles is to give you a basic understanding of creating several frequently used diagrams, charts and plots in Descriptive Statistics by using R-Software. So, without wasting our time, Just dive into the article 😊

Aim of Article


Introduction

Multiple Bar Diagram is used to compare values (generally frequencies or relative frequencies) or informations across a few categories of a single categorical variable in multiplele aspects. It is generally used when the order of categories are not important

In this diagram, we draw Rectangular Bars on a two dimensional cartesian plane to compare values across a few categories of a single categorical variable in multiple aspects. These bars can be placed either vertically on X-axis, if X-axis represents different categories, or horizontally on Y-axis, if Y-axis represents different categories. The height (in case, bars are placed on X-axis) or length (in case, bars are placed on Y-axis) of a bar, representing an aspect of a category, is proprtional to the value of that particular category. Here, we draw multiple rectangular bars, one for each aspect, corresponding to a particular category of the categorical variable.

If a multiple bar diagram contains vertical bars, that is, bars placed on X-axis, then the diagram is also known as Multiple Column or Pillar Diagram.

Example : Consider the following data -

Data of Statistics Batch 2018

Data of Statistics Batch 2019

Data of Statistics Batch 2020

The Multiple Bar Diagram (Multiple Column Diagram) will look like as follows -
Multiple-Bar-Diagram

In this diagram, we are comparing values (that are, frequencies) across a few categories (that are, Male and Female) of a single categorical variable (that is, Gender) in multiple aspects (that are, Statistics Batch 2018, 2019 and 2020).


Multiple Bar Diagram in R

Now, we will make Multiple Bar Diagram using base package of R-software. 

There are two ways to create multiple bar diagram in R depending on the method of Loading Dataset in R. I'll explain both methods.

First Method

Suppose we have collected data on variable "Gender" of six individuals as follows -

  • For Statistics Batch 2018 : "Male", "Female", "Female", "Male", "Male", "Male", "Female", "Female", "Male", "Male", "Male", "Male"
  • For Statistics Batch 2019 : "Male", "Female", "Female", "Male", "Male", "Male"
  • For Statistics Batch 2020 : "Male", "Female", "Female", "Male", "Male", "Male", "Male", "Female", "Female", "Female", "Female", "Female", "Female", "Female"

That means, we have not given the frequencies corresponding to each possible categories of the categorical variable "Gender" for each batch. 

This is a small dataset, so you may think why can't we count number of Males and Females ? Ofcourse, you may count but in case of large dataset, say 10000 individuals, you can't count, because then counting will very tedious and time consuming and that's why i am telling you this method.

Then,

1. Load Dataset :  Use the following R-code to load dataset -
# Loading Dataset for each batch
Gender_Data_2018 = c("Male","Female","Female","Male","Male","Male","Female","Female","Male","Male","Male","Male")
Gender_Data_2019 = c("Male","Female","Female","Male","Male","Male")
Gender_Data_2020 = c("Male","Female","Female","Male","Male","Male","Male","Female","Female","Female","Female","Female","Female","Female")

Here, Gender_Data_2018, Gender_Data_2019 and Gender_Data_2020 are vectors of length 12, 6, and 14 that contains information regarding "Gender" variable for Statistics batch 2018, 2019 and 2020 respectively.

If you Load Data in such manner, then to create multiple bar diagram, first of all, you will have to create a R-object of class "matrix"  that will represent a bivariate frequency table for the loaded dataset and the variables are "Gender" and "Statistics Batch". This is done using matrix() function in R as follows - 
# Creating Bivariate Frequency Table
freq_table = matrix(data = c(table(Gender_Data_2018)['Male'], table(Gender_Data_2018)['Female'],
                             table(Gender_Data_2019)['Male'], table(Gender_Data_2019)['Female'],
                             table(Gender_Data_2020)['Male'], table(Gender_Data_2020)['Female']), 
                    nrow = 3, ncol = 2, byrow = T)

# Giving names to Rows of Matrix object
row.names(freq_table) = c("2018", "2019", "2020")

# Giving names to Columns of Matrix object
colnames(freq_table) = c("Male", "Female")
We'll use R-object freq_table in next step as an argument of a R-function to create Multiple Bar Diagram.

2. Create Multiple Bar Diagram : barplot() function is used to create Multiple Bar Diagram in R. In this function, we'll give two arguments -
  • x : R-object of class 'matrix' representing bivariate frequency table. In our case it is freq_table
  • beside : a logical argument. Give its value as TRUE to create multiple bar diagram.
# Creating Multiple Bar Diagram
x = barplot(freq_table, beside = TRUE)
The output will look like as follows -
Multiple Bar Diagram

Second Method

Now, Suppose we have already given bivariate frequency table. In such situation there is no need to create bivariate frequency table. We'll directly create multiple bar diagram using R-matrix as follows -

1. Load Dataset : Use the following R-code to load dataset -
# Loading dataset (Bivariate Frequency Table)
freq_table = matrix(data = c(8, 4, 4, 2, 5, 9), nrow = 3, ncol = 2, byrow = T)

# Giving names to Rows
row.names(freq_table) = c("2018", "2019", "2020")

# Giving names to Columns
colnames(freq_table) = c("Male", "Female")

2. Create Multiple Bar Diagram : Again, barplot() function is used to create Multiple Bar Diagram in R. In this function, we'll give two arguments -
  • x : R-object of class 'matrix' representing bivariate frequency table. In our case it is freq_table
  • beside : a logical argument. Give its value as TRUE to create multiple bar diagram.
# Creating Multiple Bar Diagram
x = barplot(freq_table, beside = TRUE)
The output will again look like as follows -
Multiple Bar Diagram

Make it Informative

To make the above created multiple bar diagram more informative, we'll do the following things -

  • Adjust Y-axis limit, if necessary
  • Add different Titles (Graph Title, Axis Titles, Sub Title)
  • Add Data Labels on bars
  • Add Legend, that is, category name for each bar
  • Rename X-axis Labels, if necessary.


Adjusting Y-axis Limit : A better practise is to keep upper Y-axis limit always one more than the maximum frequency in Simple Bar Diagram with vertical bars. Use ylim argument in barplot() function to adjust Y-axis limit.
# Adjusting Y-axis Limit for further use
x = barplot(freq_table, beside = TRUE, ylim=c(0,10))
In the above code 0 and 10 (that is, one more than maximum frequency, 9) are representing lower and upper limit of Y-axis.

The output will look like as follows -
Multiple Bar Diagram

Adding Different Titles : Use title() function after barplot() function in R to add different titles. We'll also use the following arguments of title() function to add Graph Title, Axis Titles and Sub Title on diagram - 

  • main : To add graph title.
  • xlab : To add X-axis title.
  • ylab : To add Y-axis title.
  • sub : To add sub title.

R-code for the same will be as follows -
# Creating Multiple Barplot
x = barplot(freq_table, beside = TRUE ylim = c(0,10))

# Adding different titles
title(main = "Gender-wise Classification Statistics Batch 2018-20 ", 
      xlab = "Gender", 
      ylab = "Frequency", 
      sub = "Figure - 1 : Multiple Bar Diagram")
The output will look like as follows -
Multiple Bar Diagram


Adding Data Labels : Use text() function after barplot() function in R. Arguments of text() function are as follows -
  • x : R-object that contains Multiple Bar Diagram
  • y : a vector that represents the position of data labels in Multiple Bar Diagram. Suitable position of labels for each bar will be length or height of bar + some additional distance between bar and data label. You may take additional distance of 0.1, 0.2, 0.3 or more if necessary, according to the need.
  • labels : Represents a vector of characters to be shown on diagram.
# Creating Multiple Barplot
x = barplot(freq_table, beside = TRUE, ylim=c(0,10))

# Adding different titles
title(main = "Gender-wise Classification Statistics Batch 2018-20 ", 
      xlab = "Gender", 
      ylab = "Frequency", 
      sub = "Figure - 1 : Multiple Bar Diagram")

# Adding Data Labels
text(x , y = as.numeric(freq_table) + 0.5, labels = c(as.character(freq_table)))
The output will look like as follows -
Multiple Bar Diagram


Adding Legend : Use two more arguments in barplot() function. These arguments are as follows -
  • legend : a logical argument. Give its value as TRUE to show legend.
  • args.legend : It is a list object that will contain several more arguments regarding legend. At this step, I have provided list of single element x and this element indicates the position of legend on plot. It may take values - ''bottomright'', ''bottom'', ''bottomleft'', ''left'', ''topleft'', ''top'', ''topright'', ''right'', ''center''  or any integer value according to your need. I'll use its value as "right" because i like to show legend on right side of plot. You may use other values.
# Creating Multiple Barplot with legend
x = barplot(freq_table, beside = TRUE, ylim=c(0,10), legend = TRUE, args.legend = list(x='right'))

# Adding different titles
title(main = "Gender-wise Classification Statistics Batch 2018-20 ", 
      xlab = "Gender", 
      ylab = "Frequency", 
      sub = "Figure - 1 : Multiple Bar Diagram")

# Adding Data Labels
text(x , y = as.numeric(freq_table) + 0.5, labels = c(as.character(freq_table)))
The output will look like as follows -
Multiple Bar Diagram

Notice in the above plot that the legend is on a bar. So now adjust the above plot for better view of legend. We'll use one more argument xlim in barplot() function for better view of above plot as follows - 
# Creating Multiple Barplot with legend at suitable position
x = barplot(freq_table, beside = TRUE, ylim=c(0,10), legend = TRUE, args.legend = list(x='right'), xlim = c(1,9.2))

# Adding different titles
title(main = "Gender-wise Classification Statistics Batch 2018-20 ", 
      xlab = "Gender", 
      ylab = "Frequency", 
      sub = "Figure - 1 : Multiple Bar Diagram")

# Adding Data Labels
text(x , y = as.numeric(freq_table) + 0.5, labels = c(as.character(freq_table)))
The output will look like as follows -
Multiple Bar Diagram

Now, it is better than previous one !

Renaming X-axis Labels : If category name for each bar is not correct or you want to make any changes in category name, then you must use argument names.arg in barplot() function and then use title() and text() functions to create informative simple bar diagram.

In our case category names (Male and Female) are correct. Just consider that I want to change category name "Male" by "Male Students" and "Female" by "Female Students", then what will be the procedure ?
# Creating Multiple Barplot with legend at suitable position and renaming x-axis labels
x = barplot(freq_table, beside = TRUE, ylim=c(0,10), legend = TRUE, args.legend = list(x='right'), xlim = c(1,9.2), names.arg = c("Male Students","Female Students"))

# Adding different titles
title(main = "Gender-wise Classification Statistics Batch 2018-20 ", 
      xlab = "Gender", 
      ylab = "Frequency", 
      sub = "Figure - 1 : Multiple Bar Diagram")

# Adding Data Labels
text(x , y = as.numeric(freq_table) + 0.5, labels = c(as.character(freq_table)))
The output will look like as follows -
Multiple Bar Diagram

Notice that X-axis labels has now been changed.

Now, I am again going to convert X-axis labels as previous one, because i don't want to change X-axis labels.
# Creating Multiple Barplot with legend at suitable position and converting x-axis labels as previous one
x = barplot(freq_table, beside = TRUE, ylim=c(0,10), legend = TRUE, args.legend = list(x='right'), xlim = c(1,9.2))

# Adding different titles
title(main = "Gender-wise Classification Statistics Batch 2018-20 ", 
      xlab = "Gender", 
      ylab = "Frequency", 
      sub = "Figure - 1 : Multiple Bar Diagram")

# Adding Data Labels
text(x , y = as.numeric(freq_table) + 0.5, labels = c(as.character(freq_table)))
The output will look like as follows -
Multiple Bar Diagram


Make it Decorative

To make the above created informative mutiple bar diagram more decorative, we'll do the following things -

  • Rotate Y-axis Labels
  • Decorate Rectangular Bars
  • Decorate All Titles
  • Decorate Axis Labels
  • Decorate Data Labels
  • Decorate Legend


Rotating Y-axis Labels : Use las argument in barplot() function and set its value as 1 as follows -
# Creating Multiple Barplot with legend and rotating Y-axis labels
x = barplot(freq_table, beside = TRUE, ylim=c(0,10), legend = T, args.legend = list(x='right'), xlim = c(1,9.2), las = 1)

# Adding different titles
title(main = "Gender-wise Classification Statistics Batch 2018-20 ", 
      xlab = "Gender", 
      ylab = "Frequency", 
      sub = "Figure - 1 : Multiple Bar Diagram")

# Adding Data Labels
text(x , y = as.numeric(freq_table) + 0.5, labels = c(as.character(freq_table)))
The ouput will look like as follows -
Multiple Bar Diagram


Decorating Rectangular Bars : We'll use three more arguments in barplot() function to decorate rectangular bars. These arguments are -

  • col : To give specific colour to rectangular bars
  • border : To give specific colour to border of bars
  • density : To control density of lines in each rectangular bars

Apply all these arguments as follows - 
# Creating Multiple Barplot with legend, rotated Y-axis labels and decorating rectangular bars
x = barplot(freq_table, beside = TRUE, ylim=c(0,10), legend = T, args.legend = list(x='right'), xlim = c(1,9.2), las = 1, col = c("red","green", "yellow"), border = c("black","blue", "magenta"), density = 32)

# Adding different titles
title(main = "Gender-wise Classification Statistics Batch 2018-20 ", 
      xlab = "Gender", 
      ylab = "Frequency", 
      sub = "Figure - 1 : Multiple Bar Diagram")

# Adding Data Labels
text(x , y = as.numeric(freq_table) + 0.5, labels = c(as.character(freq_table)))
The output will look like as follows - 
Multiple Bar Diagram

Looks nice ! Notice - 

  • Border colour of each bar.
  • Colour of each rectangular bars.
  • Density of lines in each bar is low. I have used density value 32, you may use any other value according to your need.

In this way, you may use these three arguments according to your need. Now, I am going to redecorate the above plot because i don't like such decoration 😃
# Creating Multiple Barplot with legend, rotated Y-axis labels and decorating rectangular bars
x = barplot(freq_table, beside = TRUE, ylim=c(0,10), legend = T, args.legend = list(x='right'), xlim = c(1,9.2), las = 1, col = c("red","green", "yellow"), border = c("red","green", "yellow"))
# Adding different titles title(main = "Gender-wise Classification Statistics Batch 2018-20 ", xlab = "Gender", ylab = "Frequency", sub = "Figure - 1 : Multiple Bar Diagram") # Adding Data Labels text(x , y = as.numeric(freq_table) + 0.5, labels = c(as.character(freq_table)))
The output will look like as follows - 
Multiple Bar Diagram

It is better for me 😊

Decorating All Titles : We'll use nine more arguments in title() function to decorate all titles. These arguments are -

  • cex.main : To resize Graph title.
  • cex.sub : To resize sub title.
  • cex.lab : To resize axis titles.
  • font.main : To change font style of Graph title.
  • font.sub : To change font style of sub title.
  • font.lab : To change font style of Axis titles.
  • col.main : To change color of Graph title.
  • col.sub : To change color of sub title.
  • col.lab : To change color of Axis titles.

Apply all arguments as follows - 
# Creating Multiple Barplot with legend, rotated Y-axis labels, decorating rectangular bars and now titles
x = barplot(freq_table, beside = TRUE, ylim=c(0,10), legend = T, args.legend = list(x='right'), xlim = c(1,9.2), las = 1, col = c("red","green", "yellow"), border = c("red","green", "yellow")) # Adding different titles title(main = "Gender-wise Classification Statistics Batch 2018-20 ", xlab = "Gender", ylab = "Frequency", sub = "Figure - 1 : Multiple Bar Diagram", cex.main = 1.5, cex.sub = 1.15, cex.lab = 1.3, font.main = 2, font.sub = 2, font.lab = 2, col.main = "red", col.sub = "blue", col.lab = "orange") # Adding Data Labels text(x , y = as.numeric(freq_table) + 0.5, labels = c(as.character(freq_table)))
The output will look like as follows -
Multiple Bar Diagram
Compare this plot with previous plot and notice the change in style, size and colour of all titles.

Decorating Axis Labels : We'll use four more arguments in barplot() function to decorate axis labels. These arguments are -

  • cex.axis : To resize Y-axis (in case of vertical bars) or X-axis (in case of horizontal bars) labels 
  • cex.names : To resize X-axis labels (in case of vertical bars) or Y-axis (in case of horizontal bars) labels 
  • font.axis : To change font style of axis labels. Use its value 2 for bold, 3 for italic and 4 for bold and italic.
  • col.axis : To change colour of axis labels

Apply all arguments as follows -
# Creating Multiple Barplot with legend, rotated Y-axis labels, decorating rectangular bars, titles and now axis labels
x = barplot(freq_table, beside = TRUE, ylim=c(0,10), legend = T, args.legend = list(x='right'), xlim = c(1,9.2), las = 1, col = c("red","green", "yellow"), border = c("red","green", "yellow"),
            cex.axis = 1.2, cex.names = 1.2, font.axis = 3, col.axis = "dark blue")

# Adding different titles
title(main = "Gender-wise Classification Statistics Batch 2018-20 ", 
      xlab = "Gender", 
      ylab = "Frequency", 
      sub = "Figure - 1 : Multiple Bar Diagram",
      cex.main = 1.5, cex.sub = 1.15, cex.lab = 1.3,
      font.main = 2, font.sub = 2, font.lab = 2,
      col.main = "red", col.sub = "blue", col.lab = "orange")

# Adding Data Labels
text(x , y = as.numeric(freq_table) + 0.5, labels = c(as.character(freq_table)))
The output will look like as follows -
Multiple Bar Diagram

Notice the change in font style, size and colour of axis labels.

Decorating Data Labels : We'll use three more arguments in text() function to decorate axis labels. These arguments are -

  • cex : To resize data labels
  • font : To change font style of data labels
  • col : To change colour of data labels

Apply all arguments as follows -
# Creating Multiple Barplot with legend, rotated Y-axis labels, decorating rectangular bars, titles, axis labels and now data labels
x = barplot(freq_table, beside = TRUE, ylim=c(0,10), legend = T, args.legend = list(x='right'), xlim = c(1,9.2), las = 1, col = c("red","green", "yellow"), border = c("red","green", "yellow"),
            cex.axis = 1.2, cex.names = 1.2, font.axis = 3, col.axis = "dark blue")

# Adding different titles
title(main = "Gender-wise Classification Statistics Batch 2018-20 ", 
      xlab = "Gender", 
      ylab = "Frequency", 
      sub = "Figure - 1 : Multiple Bar Diagram",
      cex.main = 1.5, cex.sub = 1.15, cex.lab = 1.3,
      font.main = 2, font.sub = 2, font.lab = 2,
      col.main = "red", col.sub = "blue", col.lab = "orange")

# Adding Data Labels
text(x , y = as.numeric(freq_table) + 0.5, labels = c(as.character(freq_table)),
     cex = 1.2, font = 2, col = "magenta")
The output will look like as follows -
Multiple Bar Diagram
 
Notice the change in font style, size and colour of data labels.

Decorating Legend : We'll add four more elements in list object args.legend in barplot() function. These arguments are -

  • border : a vector of colours of border of each boxes corresponding to each legend text.
  • bty : Give its value as "n" if you do not want to show legend overall box border.
  • text.fontTo change font style of legend text
  • text.colTo change colour of legend text

Apply all arguments as follows -
# Creating Multiple Barplot with legend, rotated Y-axis labels, decorating rectangular bars, titles, axis labels, data labels and now legends
x = barplot(freq_table, beside = TRUE, ylim=c(0,10), legend = T, args.legend = list(x='right', border = c("red", "green","yellow"), bty = "n", text.font = 2, text.col = "blue"), xlim = c(1,9.2), las = 1, col = c("red","green", "yellow"), border = c("red","green", "yellow"),
            cex.axis = 1.2, cex.names = 1.2, font.axis = 3, col.axis = "dark blue")

# Adding different titles
title(main = "Gender-wise Classification Statistics Batch 2018-20 ", 
      xlab = "Gender", 
      ylab = "Frequency", 
      sub = "Figure - 1 : Multiple Bar Diagram",
      cex.main = 1.5, cex.sub = 1.15, cex.lab = 1.3,
      font.main = 2, font.sub = 2, font.lab = 2,
      col.main = "red", col.sub = "blue", col.lab = "orange")

# Adding Data Labels
text(x , y = as.numeric(freq_table) + 0.5, labels = c(as.character(freq_table)),
     cex = 1.2, font = 2, col = "magenta")
The output will look like as follows -
Multiple Bar Diagram

Notice the change in font style, colour  and box border of legend.

Note - 

  • The values for arguments like cexcex.maincex.subcex.axiscex.names and density are chosen by hit and trial method and use a positive integer value according to your need. (Never learn these values)
  • The values for arguments font and col again depends on your choice.

Multiple Bar Diagram with Horizontal Bars

Multiple Bar Diagram with horizontal bars are generally prefered to Multiple Bar Diagram with vertical bars, when there are a large number of categories to be compared.

For horizontal bars, use one more argument horiz = TRUE in barplot() function. This will create multiple bar diagram with horizontal bars and then use other functions like title() and text() to make it more informative and decorative as i have done in this article according to your need. I am leaving this part for your practise. Just do it for better understanding.

Conclusion

That's all about creating informative and decorative Multiple Bar Diagram in R. There are many more arguments and functions that are also used to make it more decorative and informative. But as a beginner, this is sufficient for you. Just practise it and explore more about other arguments and functions by yourself.

So, I want to stop here in this article. In next article we'll discuss Stacked Bar Diagram using R-Software. Till then, Good Bye !

Happy Learning ! ðŸ˜Š

If you find any mistake or have any suggestions,  just let me know using Suggestion Form given below (for mobile users) or in sidebar (for laptop users)Thank you in Advance ! ðŸ˜Š

Next Article : Link (Available Soon)

Share this Article Via

Post a Comment

0 Comments