---
title: "Using lattice's xyplot()"
output:
  rmarkdown::html_vignette:
    toc: true
vignette: >
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteIndexEntry{Using lattice's xyplot()}
  %\usepackage[utf8]{inputenc}
---

```{r include=FALSE}
require(knitr)
opts_chunk$set(tidy=FALSE,fig.width=5.5,fig.height=3.5)
```

# Preliminaries

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`:

```{r message=FALSE}
require(tigerstats)
```

then `lattice` will be loaded as well.

# Basic Scatterplot

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:

```{r xytutfastestgpa}
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:

* the `xlab` argument to label the horizontal axis;
* the `ylab` argument to label the vertical axis, complete with units (miles per hour);
* the `main` argument to provide a brief but descriptive title for the graph;
* the "\n" to make two lines in the title (useful if you have a long title).

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$$

# Including a Regression Line

If you want desire a regression line along with your scatterplot, use the argument `type`, as follows:

```{r xytutfastestgpareg}
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").

# Playing With Points

You can vary the type of point using the `pch` argument, and the color of the points with the `col` argument.  For example:

```{r xytutfastestgpapoints}
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:

```{r eval=FALSE}
colors()
```