Hello everyone.
In this video lecture we'll learn how to obtain a histogram of a dataset.
How to impose smooth density function over a histogram and
to change colors and line width in a histogram.
So I have entered the following data set and
called it small size data set because it only has 30 data points.
And we will try to find the histogram of this data set.
So histogram routine or command in R is basically hist.
And it's a function, and the argument of the function will be our data set.
If you basically write small.size.dataset here, and
without doing anything, by just entering, we will get our histogram.
But let me mention a few things about this histogram.
By default, on the y-axis, there is a frequency.
For example, there is only one number in my dataset between 0 and 20.
Usually, we'll like to have this as a probability or density.
So we'll change that.
By default the name of this histogram is histogram of the name of the variable name
of the dataset, we can play with it.
And by again, by default x label is just small.size.dataset in
other words it's just the variable name, the name of the dataset.
But we can change this, for example,
we can go back and we can change our x label.
So x label will accept a string, so
that's why we have these quotas, here and
I will, for example, put, My data points.
If I enter, everything stays same, its label has changed.
We can change the title to, so if we go back, and we say main.
Again, main accepts a string, and whatever the string is,
it will be it is title, the title of histogram.
Let me write 'Histogram of my data.'
And now the histogram title has
changed, and I have my labels.
Correct my data points, my title.
Now we can also turn off this frequency.
How do we do that?
We go back into the function, we say freq.
freq here stands for frequency, and it's a boolean variable.
By default, it is true.
That's why in the histogram it comes with the frequency.
But we can turn that off by saying it is FALSE, or just by using capital letter F.
And if I do that, everything stays same, well almost.
The density has changed, so my histogram scaled down.
Now I have probabilities here.
Now, we can change the colors here, we can, for
example, change the colors of this rectangle.
For example, I can go and say, color=,
again, this is going to take it as a string.
Let's say I'm going to write this as green.
And if I do that, I have my histogram, which is green, all green.
We can impose smooth density
function over this histogram.
So density function is found by using this command, density.
So I have to put my data set here.
Now, density to find its density, but
if I want to impose the graph on it, I should use the lines command.
And my argument for the lines is the density command, and
the argument for the density is basically my data set.
If I do that, it basically imposes some smooth density.
Probably distribution function over my histogram.
I can change the color here, color of my density function.
So let's say this time I'm going to use red, and
now it is red, but I can also play with that line width.
So width is lwd and if I increase it, for example, if I make this five.
Now my line width is a little, the width is bigger than the previous one.
So I have this histogram, I have my distribution.
Density function's imposed on my histogram here.
And one last thing I would like to mention is this bin width.
We can play with this bin width, in other words, the width of this rectangles.
By increasing these break points.
For example, I can go back to my histogram command and say breaks.
So breaks takes a lot of values.
It can take a sequence, array, or it can take a number of break points.
I'm going to use this as just a number.
For example, if I put 10 here.
Let’s see what happens.
Well if I put 10 here then I have a smaller bin width,
more break points and my histogram looks like this.
Might or might not be useful to use these breaks.
And if I want to still impose my density over it,
I would just impose my density over this.
Okay, so what have you learned in this video?
You have learned how to find histogram of a dataset with frequencies or
probabilities on the y-axis.
You learned how to change bin width in histogram using breaks.
You have learned how to impose a smooth density over a histogram.
And you have learned how to change colors and line width in a histogram.