How to plot bar chart with R
Tags: tutorial, analytics, database, devops
Published: 2021-05-29 10:52:53

### 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>>
```R
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
``` 
Result>>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i0ps6wwmpk6jjgl4oqrv.JPG)
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>>
```R
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
```
Result>>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/64kp00lmve6tvx8k4vow.JPG)
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>>
```R
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')
```
Result>>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f35gheswfq7aouposz65.JPG)
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)`