Home

Histograms in Base R

|
Updated:  
2017-07-04 0:57:11
|
Statistical Analysis with R Essentials For Dummies
Explore Book
Buy On Amazon
You can create a histogram from the Cars93 data frame, which resides in the MASS package. This data frame holds data on 27 variables for 93 car models that were available in 1993. The figure shows part of the data frame in the Data Editor window that opens after you type

> edit(Cars93) To create a histogram of the distribution of prices in that data frame, you'd enter:

hist(Cars93$Price) which produces the following figure.
stats-r-histogram2 Initial histogram of the distribution of prices in Cars93.

How do you spruce it up? By adding arguments.

One often-used argument in base R graphics changes the label of the x-axis from R's default into something more meaningful. It's called xlab. For the x-axis, add

xlab= "Price (x $1,000)"

to the arguments. You can use ylab to change the y-axis label as well.

You want to extend the x-axis from a lower limit of 0 to an upper limit of 70, and that's the province of the argument xlim. Because this argument works with a vector, add

xlim = c(0,70)

For a different title, use main: main = "Prices of 93 Models of 1993 Cars" To produce the final histogram shown, the whole megillah is hist(Cars93$Price, xlab="Price (x $1,000)", xlim = c(0,70), main = "Prices of 93 Models of 1993 Cars")

stats-r-histogram Histogram of prices of cars in the Cars93 data frame.

When creating a histogram, R figures out the best number of columns for a nice-looking appearance. Here, R decided that 12 is a pretty good number. You can vary the number of columns by adding an argument called breaks and setting its value. R doesn't always give you the value you set. Instead, it provides something close to that value and tries to maintain a nice-looking appearance. Add this argument, set its value (breaks =4, for example).

About This Article

This article is from the book: 

About the book author:

Joseph Schmuller, PhD, is a cognitive scientist and statistical analyst. He creates online learning tools and writes books on the technology of data science. His books include R All-in-One For Dummies and R Projects For Dummies.