DEV Community

maxwizard01
maxwizard01

Posted on

How to plot bar chart with R

How to plot bar graph in R.

One of the important aspect in statistics or data analysis is representing data in a graphical method. Today we are going to learn how to plot a common statistical graph called bar chart.

how can we plot bar chart with ease??

well, you don't have any problem concerning that. all you just need to use is the barplot() function.
let's see how its work.
Example 1
represent the following height of some student in primary one below in bar chart.
1,3, 6, 1,3,3, 4, 3, 3, 1, 3 ,5 ,1, 2, 1 ,4, 3, 2 ,3 ,1 ,1, 1 ,1, 4, 3, 1,5,5.
solution
To use the barplot function we declare a variable that will hold all the height given then we can insert it inside the function.

Codes>>

height= c(1,3, 6, 1,3,3, 4, 3, 3, 1, 3 ,5 ,1, 2, 1 ,4, 3, 2 ,3 ,1 ,1, 1 ,1, 4, 3, 1,5,5)
barplot(height) # this isn’t correct
Enter fullscreen mode Exit fullscreen mode

Result>>
Alt Text
Now, looking at the result above, you can see the behaviour of the barplot() function. it draw a rectangle for each of the data which is not what we actually want. so how do we correct this?? well, the solution is to sumamarize the data into mark and frequency i.e counting the duplicate. then we can plot the summarized data.
how do we summarize the data??
To summarize the data, use the table() function. this table() function helps to count the occurrence of each data.
Now let's try to plot the graph again.

Codes>>

height= c(1,3, 6, 1,3,3, 4, 3, 3, 1, 3 ,5 ,1, 2, 1 ,4, 3, 2 ,3 ,1 ,1, 1 ,1, 4, 3, 1,5,5)
summarizedHeight=table(height)
barplot(summarizedHeight) # this correct
Enter fullscreen mode Exit fullscreen mode

Result>>
Alt Text
Woow! we get exactly what we need!

However, what if we wish to design the graph by giving it title and color. won't that be more amazing??
well, that is just little thing.
How to label, title and color the graph with R.
the barplot() function accept more parameters apart from data which helps to do some certains task. the parameters are :

  1. Vectors: is the data will want to use to plot the graph which is important to be summarised by table() function.
  2. col: this is use to color your bar(the rectangular shape).
  3. main: this is the main title of your graph.
  4. xlab: to label on X-axis
  5. ylab: to label on Y-axis.
  6. col.main: this will give main title the preferable colour.

Now let's quickly take alook at how to implement this in our codes.
Example
use the data in example1 to plot a bar graph.
solution.
so, in this example we are going to make use of all the parameters above so that our chart look amazing.

Codes>>

height= c(1,3, 6, 1,3,3, 4, 3, 3, 1, 3 ,5 ,1, 2, 1 ,4, 3, 2 ,3 ,1 ,1, 1 ,1, 4, 3, 1,5,5)
summarizedHeight=table(height)
barplot(summarizedHeight,col = 'pink',main = 'The height of student in primary1',ylab="frequency",xlab = "Height",col.main='red')
Enter fullscreen mode Exit fullscreen mode

Result>>
Alt Text
woow! it looks beautiful right?? good!. However, we still have some inbuilt functions and parameters that could be use to make our graph more perfect they are:
title() function
font.main
font.axis
sub
cex.main

cex.sub
font.sub
col.sub e.tc

that is a simple way to represent our data in bar chart. I hope you find this article helpful. consider to like, join me on histagram or chat me up on whatsapp(07045225718)

Top comments (0)