Information Technology Reference
In-Depth Information
1.19 Creating a Histogram
Problem
You want to create a histogram of your data.
Solution
Use
hist(
x
)
, where
x
is a vector of numeric values.
Discussion
The lefthand panel of
Figure 1-6
shows a histogram of the
MPG.city
column taken from
the
Cars93
dataset, created like this:
>
data(Cars93, package="MASS")
>
hist(Cars93$MPG.city)
Histogram of Cars93$MPG.city
City MPG (1993)
15
25
35
45
15
20
25
30
35
40
45
Cars93$MPG.city
MPG
Figure 1-6. Histograms
The
hist
function must decide how many cells (bins) to create for binning the data. In
this example, the default algorithm chose seven bins. That creates too few bars for my
taste, because the shape of the distribution remains hidden. So, I would include a sec-
ond argument for
hist
—namely, the suggested number of bins:
>
hist(Cars93$MPG.city, 20)
The number is only a suggestion, but
hist
will expand the number of bins as possible
to accommodate that suggestion.