---
title: "Plots with ggplot2"
author: "Evan L. Ray"
date: "September 13, 2017"
output: ioslides_presentation
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```

## Warm Up (with a neighbor)

Here are the first few rows of a data set with information about participants in a randomized controlled trial designed to evaluate a substance abuse treatment program.  What are the observational units and variables?  Are the variables categorical or quantitative?

```{r preprocess_HELPrct, echo=FALSE, warning=FALSE, message=FALSE}
library(mosaicData)
library(dplyr)
library(tidyr)
library(ggplot2)

HELPrct <- HELPrct %>%
  select(
    age,
    homeless,
    substance,
    cesd,
    mcs
  ) %>%
  rename(
  dep_score = cesd,
    mental_score = mcs)
```

```{r head_HELPrct, echo = TRUE}
head(HELPrct)
```


## The observational unit, variables, and variable types matter!!

They dictate

 - What **questions** you can ask
 - What **plots** you can make
 - What **statistical models** are appropriate


## The Grammar of Graphics

A statistical graphic is a `mapping` of `data` variables

to `aes()`thetic attributes of `geom_`etric objects


## The Grammar of Graphics

A statistical graphic is a `mapping` of `data` variables

to `aes()`thetic attributes of `geom_`etric objects

```{r, echo = TRUE, eval = FALSE}
ggplot() +
  geom_<geometry type>(
    mapping = aes(<attribute1> = <variable1>, <attribute2> = <variable2>),
    data = <data frame>)
```


## The Grammar of Graphics

A statistical graphic is a `mapping` of `data` variables

to `aes()`thetic attributes of `geom_`etric objects

```{r, echo = TRUE, eval = FALSE}
ggplot() +
  geom_<geometry type>(
    mapping = aes(<attribute1> = <variable1>, <attribute2> = <variable2>),
    data = <data frame>)
```

```{r, echo = TRUE, eval = FALSE}
ggplot() +
  geom_point(
    mapping = aes(x = dep_score, y = mental_score),
    data = HELPrct)
```

## 1 Categorical Variable: Bar Plot
```{r, echo = TRUE, fig.width=8, fig.height = 4}
ggplot() +
  geom_bar(mapping = aes(x = homeless),
    data = HELPrct)
```

## 2 Categorical Variables: Bar Plot
```{r, echo = TRUE, fig.width=8, fig.height=4}
ggplot() +
  geom_bar(mapping = aes(x = homeless, fill = substance),
    data = HELPrct)
```

## 2 Categorical Variables: Bar Plot
```{r, echo = TRUE, fig.width=8, fig.height=4}
ggplot() +
  geom_bar(mapping = aes(x = homeless, fill = substance),
    position = position_dodge(),
    data = HELPrct)
```

## 1 Quantitative Variable: Histograms
```{r, echo = TRUE, fig.width=8, fig.height=3}
ggplot() +
  geom_histogram(mapping = aes(x = dep_score),
    data = HELPrct)
```

## 1 Quantitative Variable: Histograms
```{r, echo = TRUE, fig.width=8, fig.height=3}
ggplot() +
  geom_histogram(mapping = aes(x = dep_score),
    binwidth = 4,
    data = HELPrct)
```

## 1 Quantitative Variable: Histograms
```{r, echo = TRUE, fig.width=8, fig.height=3}
ggplot() +
  geom_histogram(mapping = aes(x = dep_score, y = ..density..),
    binwidth = 4,
    data = HELPrct)
```

## 1 Quantitative Variable: Density Plots
```{r, echo = TRUE, fig.width=8, fig.height=3}
ggplot() +
  geom_density(mapping = aes(x = dep_score),
    data = HELPrct)
```

## 2 Quantitative Variables: Scatter Plots
```{r, echo = TRUE, fig.width=8, fig.height=3}
ggplot() +
  geom_point(
    mapping = aes(x = dep_score, y = mental_score),
    data = HELPrct)
```

## 2 Quantitative Variables: Scatter Plots
```{r, echo = TRUE, fig.width=8, fig.height=3}
ggplot() +
  geom_point(
    mapping = aes(x = dep_score, y = mental_score,
      color = substance),
    data = HELPrct)
```


## 1 Quantitative, 1 Categorical: Box Plots
```{r, echo = TRUE, fig.width=8, fig.height=3}
ggplot() +
  geom_boxplot(mapping = aes(x = substance, y = dep_score),
    data = HELPrct)
```

## 1 Quantitative, 1 Categorical: Density
```{r, echo = TRUE, fig.width=8, fig.height=3}
ggplot() +
  geom_density(mapping = aes(x = dep_score, color = substance),
    data = HELPrct)
```

## Summary

| Variables      | Plot Type    | Geometry       | Aesthetics |
|----------------|--------------|----------------|-------------------|
| 1 Categorical  | Bar Plot     | geom_bar       | x |
| 1 Quantitative | Histogram    | geom_histogram | x |
| 1 Quantitative | Density      | geom_density   | x |
| 2 Categorical  | Bar Plot     | geom_bar       | x, fill |
| 2 Quantitative | Scatter Plot | geom_point     | x, y |
| 1 Categorical and 1 Quantitative | Box Plot | geom_boxplot | x (categorical), y (quantitative) |
| 1 Categorical and 1 Quantitative | Density Plot | geom_density | x (quantitative), color (categorical) |

## Common Error 1

No `+` on first line:

```{r, echo = TRUE, fig.width=8, fig.height=1.5}
ggplot()
  geom_point(
    mapping = aes(x = dep_score, y = mental_score),
    data = HELPrct)
```

## Common Error 2

Forgot `data = `:

```{r, echo = TRUE, fig.width=8, fig.height=1, eval = FALSE}
ggplot() +
  geom_point(
    mapping = aes(x = dep_score, y = mental_score))
```

**Error in FUN(X[[i]], ...) : object 'dep_score' not found**

