An R Introduction to Statistics

Histogram

A histogram consists of parallel vertical bars that graphically shows the frequency distribution of a quantitative variable. The area of each bar is equal to the frequency of items found in each class.

Example

In the data set faithful, the histogram of the eruptions variable is a collection of parallel vertical bars showing the number of eruptions classified according to their durations.

Problem

Find the histogram of the eruption durations in faithful.

Solution

We apply the hist function to produce the histogram of the eruptions variable.

> duration = faithful$eruptions 
> hist(duration,    # apply the hist function 
+   right=FALSE)    # intervals closed on the left

Answer

The histogram of the eruption durations is:

PIC

Enhanced Solution

To colorize the histogram, we select a color palette and set it in the col argument of hist. In addition, we update the titles for readability.

> colors = c("red", "yellow", "green", "violet", "orange", 
+   "blue", "pink", "cyan") 
> hist(duration,    # apply the hist function 
+   right=FALSE,    # intervals closed on the left 
+   col=colors,     # set the color palette 
+   main="Old Faithful Eruptions", # the main title 
+   xlab="Duration minutes")       # x-axis label

PIC

Exercise

Find the histogram of the eruption waiting period in faithful.