Adding Graph Features to Base R Graphics - dummies

Adding Graph Features to Base R Graphics

By Joseph Schmuller

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.

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.

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.