Statistical Analysis with R For Dummies
Book image
Explore Book Buy On Amazon
An important aspect of base R graphics is the ability to add features to a graph after you create it. One way of showing histogram information is to think of the data as probabilities rather than frequencies. So instead of the frequency of a particular price range, you graph the probability that a car selected from the data is in that price range. To do this, you add

probability = True

to the arguments. Now the R code looks like this:

> hist(Cars93$Price, xlab="Price (x $1,000)", xlim = c(0,70), main = "Prices of 93 Models of 1993 Cars",probability = TRUE)

The result appears here. The y-axis measures Density — a concept related to probability. The graph is called a density plot.

stats-r-density
Density plot of the distribution of prices in Cars93.

The point of all this is what you do next. After you create the graph, you can use an additional function called lines() to add a line to the density plot:

> lines(density(Cars93$Price)) The graph now looks like the following image.

stats-r-density-plot
Density plot with an added line.

So in base R graphics, you can create a graph and then start adding to it after you see what the initial graph looks like. It’s something like painting a picture of a lake and then adding mountains and trees as you see fit.

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: