How to create a histogram in R

How to create a histogram in R


  R Interview Questions

A histogram is a wonderful tool to check the spread of data – specifically frequency distribution.

1. Basic histogram

> hist(iris$Sepal.Length)

2. Specify ‘breaks’

Since histogram uses binning to ‘break’ the linear distribution of numeric data across bins, specifying breaks gives you more control over the construction of the histogram for small data sets.

> hist(iris$Sepal.Length,breaks=20)

3. Density plot

Histograms by default are based on frequency distribution. You could also base it on density.

> h = hist(iris$Sepal.Length,breaks=20,freq=FALSE)

You can get the actual values from

> h$density
 [1] 0.13333333 0.16666667 0.23333333 0.53333333 0.43333333 0.23333333
 [7] 0.43333333 0.50000000 0.30000000 0.33333333 0.53333333 0.23333333
[13] 0.36666667 0.16666667 0.13333333 0.06666667 0.03333333 0.13333333
[19] 0.03333333

asdf

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: