Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Histogram of uniform distribution not plotted correctly in R

Tags:

r

When I run the code

hist(1:5)

or

hist(c(1,2,3,4,5))

The generated histogram shows that the first number "1" has frequency of 2 when there is only one "1" in the array.

enter image description here

I also tried

hist(c(1,2,3,7,7,7,9))

but it still shows that the first bar is twice times higher than the second one

enter image description here

However when I run

 hist(c(1:10))

The frequency height of every bars are equal

I'm pretty new to statistics and R so I don't know what is the reason behind this. I hope somebody can help me clarify why is this happening. Thank you

enter image description here

like image 789
atmosphere506 Avatar asked Mar 15 '14 13:03

atmosphere506


People also ask

How do you make a histogram better in R?

We can add density line to a histogram by adding another geom_() function; geom_density(). This will convert the frequency histogram into density histogram and add the density line to it. In this example we have added density lien with geom_density() function and specify the color of line as an argument to it.

How do you make a uniform histogram?

To create a histogram for uniform data in R, we can simply use hist function but the bars size may vary even if the frequency is same.

How do I change the number of bins in a histogram in R studio?

To change the number of bins in the histogram in Base R Language, we use the breaks argument of the hist() function. The breaks argument of the hist function to increase or decrease the width of our bars by fixing the number of bars, cells, or bins the whole histogram will be divided into.

What is a uniform distribution histogram?

Uniform. A histogram is described as “uniform” if every value in a dataset occurs roughly the same number of times. This type of histogram often looks like a rectangle with no clear peaks.


1 Answers

Taking your first example, hist(1:5), you have five numbers, which get put into four bins. So two of those five get lumped into one.

The histogram has breaks at 2, 3, 4, and 5, so you can reasonably infer that the definition of hist for where a number is plotted, is:

#pseudocode
if (i <= break) { # plot in bin }

You can specify the breaks manually to solve this:

hist(1:5, breaks=0:5)

enter image description here

like image 126
Scott Ritchie Avatar answered Sep 30 '22 21:09

Scott Ritchie