Statistical Analysis with R For Dummies
Book image
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, has taught undergraduate and graduate statistics, and has 25 years of IT experience. The author of four editions of Statistical Analysis with Excel For Dummies and three editions of Teach Yourself UML in 24 Hours (SAMS), he has created online coursework for Lynda.com and is a former Editor in Chief of PC AI magazine. He is a Research Scholar at the University of North Florida.

This article can be found in the category: