Chapter 12. Scatter Plots and Line Charts
Basic Scatter Plots
The scatter plot may be one of the most useful graphic tools that we have. We can easily study the associations between two variables—or lack thereof—on this familiar type of graph. Further, many other graph types are simply variants of the basic scatter plot.
Again, let’s examine the trees
dataset. Remember, the head()
function prints out the first six rows. You can see the entire dataset by typing trees
:
> head(trees) Girth Height Volume 1 8.3 70 10.3 2 8.6 65 10.3 3 8.8 63 10.2 4 10.5 72 16.4 5 10.7 81 18.8 6 10.8 83 19.7
There are probably strong relationships among the three variables, which we should be able to see on a scatter plot. We will use the plot()
function to produce scatter plots. Its basic form is as follows:
plot(x-variable, y-variable, arguments...)
The following scripts produce several scatter plots of the trees
data:
# 4 short scripts to produce the 4 graphs in Fig. 12-1 attach(trees) par(mfrow = c(2,2), cex = .7) # Fig. 12-1a: show just 2 points on the graph trees2 = trees[1:2,] # trees2 a subset, only 1st 2 trees # see sidebar plot(trees2$Height, trees2$Girth, xlim = c(63,80), ylim = c(7.8,10), xlab = "Height", ylab = "Girth", main = "a. First two trees") # text() allows annotation on the graph text(72,8.1,labels = "(Height = 70, Girth = 8.3)", xlim = c(61,80), ylim = c(8,22)) text(65,8.8, labels = "(65, 8.6)", xlim = c(62,89), ylim = c(8,22)) # Fig. 12-1b: note that a basic plot requires ...
Get Graphing Data with R now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.