The function xyplot()
makes scatterplots to indicate the relationship between two numerical variables. It comes from the lattice
package for statistical graphics, which is pre-installed with every distribution of R. Also, package tigerstats
depends on lattice, so if you load tigerstats
:
require(tigerstats)
then lattice
will be loaded as well.
Suppose you want to know:
Do students with higher GPA’s tend to drive more slowly than students with lower GPA’s?
If so, then you might check to see if numerical variable fastest (in the m111survey
data frame from the tigerstats
package) is related to the numerical variable GPA
. Then you can make a scatterplot as follows:
xyplot(fastest~GPA,data=m111survey,
xlab="grade point average",
ylab="speed (mph)",
main="Fastest Speed Ever Driven,\nby Grade Point Average")
Note the use of:
xlab
argument to label the horizontal axis;ylab
argument to label the vertical axis, complete with units (miles per hour);main
argument to provide a brief but descriptive title for the graph;When we think of one variable as explanatory and the other as the response, it is common to put the explanatory on the horizontal axis and the response on the vertical axis. This is accomplished by the formula
\[response \sim explanatory\]
If you want desire a regression line along with your scatterplot, use the argument type
, as follows:
xyplot(fastest~GPA,data=m111survey,
xlab="grade point average",
ylab="speed (mph)",
main="Fastest Speed Ever Driven,\nby Grade Point Average",
type=c("p","r"))
The list given by c("p","r")
tells xyplot()
that we want both the points (“p”) and a regression line (“r”).
You can vary the type of point using the pch
argument, and the color of the points with the col
argument. For example:
xyplot(fastest~GPA,data=m111survey,
xlab="grade point average",
ylab="speed (mph)",
main="Fastest Speed Ever Driven,\nby Grade Point Average",
pch=19,col="blue")
There are 25 different values for pch
: the integers 1 through 25.
There are many, many values for col
. You can explore 657 of them with the command:
colors()