---
title: "Scatter Plots"
author: "David Gerbing"
output: 
  rmarkdown::html_vignette:
    toc: true
vignette: >
  %\VignetteIndexEntry{2. Visualize: Scatter Plots}
  %\VignetteEngine{knitr::rmarkdown}
  \usepackage[utf8]{inputenc}
---


```{r, include=FALSE}
knitr::opts_chunk$set(fig.width=4, fig.height=3.5)
```

```{r}
library("lessR")
style()
```


To illustrate, first read the Employee data included as part of __lessR__.

```{r read}
d <- Read("Employee")
```

__lessR__ provides many versions of a scatter plot with its `Plot()` function.


## Two Variables

The regular scatterplot of two continuous variables.

```{r sp, fig.width=4.5}
Plot(Years, Salary)
```


The enhanced scatterplot with parameter `enhance`. 

```{r spEnhance, fig.width=4.5}
Plot(Years, Salary, enhance=TRUE)
```

Plot the scatterplot with the non-linear best fit `"loess"` line. The three available values for the `fit` line are `"loess"` for non-linear, `"lm"` for linear, and `"null"` for the null model line, the flat line at the mean of $y$. Also, setting `fit` to `TRUE` sets `fit` to`"loess"`.

For emphasis set `plot_errors` to `TRUE` to plot the residuals from the line.

```{r sploess, fig.width=4.5}
Plot(Years, Salary, fit="loess", plot_errors=TRUE)
```


Map variable _Pre_ to the points with the `size` parameter, a bubble plot.

```{r size, fig.width=4.5}
Plot(Years, Salary, size=Pre)
```

Plot against levels of categorical variable _Gender_ with the `by` parameter.

```{r by, fig.width=5}
Plot(Years, Salary, by=Gender)
```


The categorical variable can also generate Trellis plots with the `by` parameter.

```{r, , fig.width=4.5, fig.height=4}
Plot(Years, Salary, by1=Gender)
```


Two categorical variables result in a bubble plot of their joint frequencies.

```{r}
Plot(Dept, Gender)
```


## Distribution of a Single Variable

The default plot for a single continuous variable includes not only the scatterplot, but also the violin plot and box plot, with outliers identified. Call this plot the VBS plot.

```{r}
Plot(Salary)
```


For a single categorical variable, get the corresponding bubble plot of frequencies.

```{r}
Plot(Dept)
```


## Cleveland Dot Plot

The Cleveland dot plot, here for a single variable, has row names on the y-axis. The default plots sorts by the value plotted.

```{r, fig.height=5.5, fig.width=4.5}
Plot(Salary, row_names)
```

The standard scatterplot version of a Cleveland dot plot.

```{r, fig.height=5.5, fig.width=4.5}
Plot(Salary, row_names, sort_yx="0", segments_y=FALSE)
```

This Cleveland dot plot has two x-variables, indicated as a standard R vector with the `c()` function. In this situation the two points on each row are connected with a line segment. By default the rows are sorted by distance between the successive points.

```{r, fig.width=5, fig.height=5.5}
Plot(c(Pre, Post), row_names)
```


## Time Series

Read time series data of stock _Price_ for three companies: Apple, IBM, and Intel. The data table is in long form, part of __lessR__.


```{r}
d <- Read("StockPrice")
d[1:5,]
```


Activate a time series plot by setting the $x$-variable to a variable of R type `Date`, which is true of the variable _date_ in this data set. Here plot just for _Apple_.

```{r}
Plot(date, Price, rows=(Company=="Apple"))
```

With the `by` parameter, plot all three companies on the same panel.

```{r, fig.width=5}
Plot(date, Price, by=Company)
```


With the `by1` parameter, plot all three companies on the different panels, a Trellis plot.

```{r, fig.width=4.5}
Plot(date, Price, by1=Company)
```

Now do the Trellis plot with some color.

```{r, fig.width=4.5}
style(sub_theme="black", trans=.55,
      window_fill="gray10", grid_color="gray25")
Plot(date, Price, by1=Company, n_col=1,  fill="darkred", color="red")
```


With `style()` many themes can be selected, such as `"lightbronze"`, `"dodgerblue"`, `"darkred"`, such as `"gray"`. When no `theme` is specified, return to the default theme, `colors`. 

```{r}
style()
```

## Annotation

```{r}
d <- Read("Employee")
```

Add three different text blocks at three different specified locations.

```{r fig.width=4.5}
Plot(Years, Salary, add=c("Hi", "Bye", "Wow"), x1=c(12, 16, 18),
  y1=c(80000, 100000, 60000))
```


A rectangle requires two points, four coordinates, <x1,y1> and <x2,y2>.

```{r fig.width=4.5}
style(add_trans=.8, add_fill="gold", add_color="gold4", add_lwd=0.5)
Plot(Years, Salary, add="rect", x1=12, y1=80000, x2=16, y2=115000)
```


## Full Manual

Use the base R `help()` function to view the full manual for `Plot()`. Simply enter a question mark followed by the name of the function.

```
?Plot
```


## More

More on Scatterplots and other visualizations from __lessR__ and other packages such as __ggplot2__ at:

Gerbing, D., _R Visualizations: Derive Meaning from Data_, CRC Press, May, 2020, ISBN 978-1138599635.