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.
well, you don't have any problem concerning that. all you just need to use is the
let's see how its work.
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.
To use the
barplot function we declare a variable that will hold all the height given then we can insert it inside the function.
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
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.
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
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 :
- Vectors: is the data will want to use to plot the graph which is important to be summarised by table() function.
- col: this is use to color your bar(the rectangular shape).
- main: this is the main title of your graph.
- xlab: to label on X-axis
- ylab: to label on Y-axis.
- col.main: this will give main title the preferable colour.
Now let's quickly take alook at how to implement this in our codes.
use the data in example1 to plot a bar graph.
so, in this example we are going to make use of all the parameters above so that our chart look amazing.
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')
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:
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