Looking For Anything Specific?

R Graphics (Using Base Package) || Simple Bar Diagram || Article - 1

Statistical Knowledge | R-Graphics (Using Base Package) | Article - 1
 
Hello Friends, Welcome to my new series of Articles named R Graphics (Using Base Package) and this is the first article. 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

Simple Bar Diagram is used to compare values (generally frequencies or relative frequencies) or informations across a few categories of a single categorical variable in a single aspect only. 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 a single aspect only. 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 a category, is proprtional to the value of that particular category. Here, we draw single rectangular bar, for an aspect under consideration, corresponding to a particular category of the categorical variable.

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

Example : Consider the following data -


The Simple Bar Diagram (Simple Column Diagram) will look like as follows -
Simple-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 a single aspect (that is, Statistics Batch 2019) only.


Simple Bar Diagram in R

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

There are two ways to create simple 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 -
"Male", "Female", "Female", "Male", "Male", "Male"
That means, we have not given the frequencies corresponding to each possible categories of the categorical variable "Gender". 

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
Gender_Data = c("Male","Female","Female","Male","Male","Male")
Here, Gender_Data is a vector of length 6 that contains information regarding "Gender" variable for Statistics batch 2019.

If you Load Data in such manner, then to create simple bar diagram, first of all, you will have to create a R-object of class "table"  that will represent frequency table for the loaded dataset and this is done using table(x) function in R, where argument x is a vector of data loaded previously as Gender_Data.
# Creating Frequency Table
freq_table = table(Gender_Data)
We'll use R-object freq_table in next step as an argument of a R-function to create Simple Bar Diagram.

2. Create Simple Bar Diagram : barplot(x) function is used to create Simple Bar Diagram in R. The argument x in barplot(x) function is nothing but the R-object freq_table.
# Creating Simple Bar Diagram
x = barplot(freq_table)
The output will look like as follows -
Simple-Bar-Diagram

Second Method

Now, Suppose we have already given all possible categories and their corresponding frequencies. In such situation there is no need to create frequency table. We'll directly create simple bar diagram using R-vectors only as follows -

1. Load Dataset : Use the following R-code to load dataset -
# Loading Dataset
Gender = c("Female","Male")
frequency = c(2,4)

2. Create Simple Bar Diagram : Again, barplot() function is used to create Simple Bar Diagram. But now, we'll use two arguments x and names.arg in barplot() function.

  • x : a vector of values describing the length or height of bars. In our case it is R-object frequency.
  • names.arg : a vector of names to be shown below each bar. In our case it is R-object Gender.

# Creating Simple Bar Diagram
x = barplot(frequency, names.arg = Gender)
The output will again look like as follows -
Simple Bar Diagram

Make it Informative

To make the above created simple 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 category name for each bar, if it is not correct 


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, ylim=c(0,5))
In the above code 0 and 5 (that is, one more than maximum frequency, 4) are representing lower and upper limit of Y-axis.

The output will look like as follows -
Simple 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 Simple Barplot using First method mentioned above
x = barplot(freq_table, ylim = c(0,5))

# Adding different titles
title(main = "Gender-wise Classification Statistics Batch 2019 ", 
      xlab = "Gender", 
      ylab = "Frequency", 
      sub = "Figure - 1 : Simple Bar Diagram")
The output will look like as follows -
Simple 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 Simple Bar Diagram
  • y : a vector that represents the position of data labels in Simple 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 Simple Barplot using First method mentioned above
x = barplot(freq_table, ylim=c(0,5))

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

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


Adding Category Name : 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 ?
# Renaming Categories
x = barplot(freq_table, ylim=c(0,5), names.arg = c("Female Students","Male Students"))

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

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

Make it Decorative

To make the above created informative simple 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


Rotating Y-axis Labels : Use las argument in barplot() function and set its value as 1 as follows -
# Creating Barplot using First method mentioned above with vertical Y-axis Labels
x = barplot(freq_table, ylim = c(0,5), las = 1)

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

# Adding Data Labels
text(x , y = as.numeric(freq_table) + 0.3, labels = c(as.character(freq_table)))
The ouput will look like as follows -
Simple 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 Barplot using First method mentioned above with vertical Y-axis Labels and Decorating Rectangular Bars
x = barplot(freq_table, ylim = c(0,5), las = 1, col = "blue", border = "red", density = 32)

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

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

Notice - 

  • Border colour of Bars is red.
  • Colour of rectangular bars is blue.
  • 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 Barplot using First method mentioned above with vertical Y-axis Labels and Decorating Rectangular Bars
x = barplot(freq_table, ylim = c(0,5), las = 1, col = "spring green", border = "spring green")

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

# Adding Data Labels
text(x , y = as.numeric(freq_table) + 0.3, labels = c(as.character(freq_table)))
The output will look like as follows - 
Simple 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 Barplot using First method mentioned above with vertical Y-axis Labels and Decorating Rectangular Bars and Titles
x = barplot(freq_table, ylim = c(0,5), las = 1, col = "spring green", border = "spring green")

# Adding different titles
title(main = "Gender-wise Classification Statistics Batch 2019 ", 
      xlab = "Gender", 
      ylab = "Frequency", 
      sub = "Figure - 1 : Simple 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.3, labels = c(as.character(freq_table)))
The output will look like as follows -
Simple 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 Barplot using First method mentioned above with vertical Y-axis Labels and Decorating Rectangular Bars, Titles and Axis Labels
x = barplot(freq_table, ylim = c(0,5), las = 1, col = "spring green", border = "spring green",
            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 2019 ", 
      xlab = "Gender", 
      ylab = "Frequency", 
      sub = "Figure - 1 : Simple 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.3, labels = c(as.character(freq_table)))
The output will look like as follows -
Simple 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 Barplot using First method mentioned above with vertical Y-axis Labels and Decorating Rectangular Bars, Titles, Axis and Data Labels
x = barplot(freq_table, ylim = c(0,5), las = 1, col = "spring green", border = "spring green",
            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 2019 ", 
      xlab = "Gender", 
      ylab = "Frequency", 
      sub = "Figure - 1 : Simple 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.3, labels = c(as.character(freq_table)), 
     cex = 1.2, font = 2, col = "magenta")
The output will look like as follows -
Simple Bar Diagram
 
Notice the change in font style, size and colour of data labels.

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.


Simple Bar Diagram with Horizontal Bars

Simple Bar Diagram with horizontal bars are generally prefered to Simple 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 simple 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 Simple 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 Multiple 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 

Share this Article Via

Post a Comment

0 Comments