How to draw a Histograms
When visualizing a single numerical variable, a histogram will be our go-to tool, which can be created in R using the hist()
function.
Let's quickly take a look at some examples.
Example1
construct a histogram for the following score of some students in thier first C.A.T
Codes
studentscore=c( 8,5,6,1,11,8,10, 15,4,3, 10,1,4,12,3, 14, 10,6,3,9,1,5,
20,4,9,6,1,1,1,8,4,23,3,12,3,5, 16, 10,2,8,3, 22, 16,2
,1,1,5,4,2,3)
hist(studentscore)
you can see histogram already done. but this is not perfect as it will be nice to add some design to it. To do that we make use of some parameters. inside the his()
function.
some of the parameters are:
- main : this is used to add title to your graph
- col : this is used to add colour to your grapah(all the bar.)
- xlab: to label the x-axis
- ylab: to label the y-axis
- col.main: this is to give the font-color to our Title.
- probability: to find the probabilty and use it to plot the graph i.e the y-axis will range from 0 to 1. it is usually false by default.
Now let's take a look at some examples
Example2.
the following show the weigth of male living in an estate. represent them on histogram.
69,132,83,18,44,57,12, 131,34,26,84, 147,84,10,17,75,
83, 142, 158,25, 3, 2,55,34, 119,23, 202,157, 141,75,53,
157, 174,39,43,109, 115, 107, 120,64,87, 118,20,41, 130, 52, 67, 13, 17,81
Solution.
weight=c( 69,132,83,18,44,57,12, 131,34,26,84, 147,84,10,17,75,
83, 142, 158,25, 3, 2,55,34, 119,23, 202,157, 141,75,53,
157, 174,39,43,109, 115, 107, 120,64,87, 118,20,41, 130, 52, 67, 13, 17,81)
hist(weight,main='The weight of males in Estate', col='pink',xlab='Weight(Kg)',ylab='Frequency')
run the codes, did you get the same result?? Yeah, we just make use of col,main,xlab and ylab parameters. How about others?? well let's make use of them and see the result.
Example3
we are going to use the last example above to construct a beautiful one.
Codes
weight=c( 69,132,83,18,44,57,12, 131,34,26,84, 147,84,10,17,75,
83, 142, 158,25, 3, 2,55,34, 119,23, 202,157, 141,75,53,
157, 174,39,43,109, 115, 107, 120,64,87, 118,20,41, 130, 52, 67, 13, 17,81)
hist(weight,main='The weight of males in Estate', col='pink',xlab='Weight(Kg)',ylab='Frequency',probability=TRUE,col.main='red')
can you see the result?? did you notice some changes?? what are they?? yes, the color of the title change from black to red. eh eh?? what other change can you see?? No!No! there is one more. umh? Now you get it. the yaxis has been change to some number less than 1. that is the work of probability when we assign it to "TRUE".
How do I give each of the bar of histogram different color using R??
well, if you are someone who love to use different color like me, then you can assign different color for each of the rectangle on the graph(the bar). let's see the example below.
Example
Here I will be using the mark of some student in statistics below:
32, 38, 13, 31, 60,56, 5,2, 14,9,33, 31,3, 24, 10, 33, 6,10,57, 22, 51,4, 12,11, 47, 10, 30, 31, 19, 44,8, 16,9, 49,8,2,5,4, 15,8,7, 37,2, 54, 47,1, 45,4,46, 20, 69
Codes>>
marks=c(32, 38, 13, 31, 60,56, 5,2, 14,9,33, 31,3, 24, 10, 33, 6,10,57, 22, 51,4, 12,11, 47, 10, 30, 31, 19, 44,8, 16,9, 49,8,2,5,4, 15,8,7, 37,2, 54, 47,1, 45,4,46, 20, 69)
Color=c('red','orange','blue','yellow','violet','pink')
hist(marks,main='The marks of students in Statistics',col=Color,xlab='Marks',ylab='Frequency',probability=TRUE,col.main='red')
This result above look differents, notice I listed all my colors inside a vector named 'Colour' so the name was assigned to col parameters inside the histogram.
I hope you find this article helpful. consider to follow, or like on this platform. you can also chat me up if you have any addition or question on WhatsApp. follow me on instagram and don't forget to follow me on Twitter
Top comments (0)