Skip to Main Content
Graphing Data with R
book

Graphing Data with R

by John Jay Hilfiger
October 2015
Beginner to intermediate content levelBeginner to intermediate
250 pages
6h 26m
English
O'Reilly Media, Inc.
Content preview from Graphing Data with R

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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

A Course in Statistics with R

A Course in Statistics with R

Prabhanjan N. Tattar, Suresh Ramaiah, B. G. Manjunath

Publisher Resources

ISBN: 9781491922606Errata Page