---
title: "ggformula/lattice Comparison"
author: "Nicholas Horton (nhorton@amherst.edu)"
date: '`r Sys.Date()`'
vignette: |
  %\VignetteIndexEntry{ggformula/lattice Comparison} 
  %\VignetteEngine{knitr::rmarkdown} 
  \usepackage[utf8]{inputenc}
output: 
  pdf_document:
    fig_height: 3
    fig_width: 5
    pandoc_args: [
      "-V", "classoption=twocolumn"
    ]
  html_document:
    fig_height: 3
    fig_width: 5
  word_document:
    fig_height: 3
    fig_width: 5
---
```{r, setup, include = FALSE}
require(mosaic)   # Load additional packages here 
require(ggformula)

# Some customization.  You can alter or delete as desired (if you know what you are doing).
trellis.par.set(theme = theme.mosaic()) # change default color scheme for lattice
theme_set(theme_bw())                   # change default theme for ggformula
knitr::opts_chunk$set(
  tidy = FALSE,     # display code as typed
  size = "small")   # slightly smaller font for code
```

## Introduction
This document is intended to help users of the `mosaic` package migrate their `lattice` package graphics to `ggformula`.  The mosaic package provides a simplified and systematic introduction to the core functionality related to descriptive statistics, visualization, modeling, and simulation-based inference required in first and second courses in statistics.  Originally, the `mosaic` package used `lattice` graphics but now support is also available for the `ggformula` system. 

\vfill

## Histograms (ggformula)

```{r}
library(mosaic)   # also loads ggformula 
gf_histogram(~ age, data = HELPrct)
```

## Histogram options (ggformula)

```{r}
gf_histogram(~ age, data = HELPrct,
             binwidth = 5) 
```

\newpage

## References 

More information about `ggformula` can be found at https://github.com/ProjectMOSAIC/ggformula.

More information regarding Project MOSAIC (Kaplan, Pruim, and Horton) can be found at http://www.mosaic-web.org.  Further information regarding the `mosaic` package can be found at https://github.com/ProjectMOSAIC/mosaic and https://journal.r-project.org/archive/2017/RJ-2017-02.

Examples of how to bring multidimensional graphics into day one of an introductory statistics course can be found at http://escholarship.org/uc/item/84v3774z. 

\vfill

## Histograms (lattice)

```{r}
library(mosaic)     # also loads lattice
histogram(~ age, data = HELPrct)
```

## Histogram options (lattice)

```{r}
histogram(~ age, width = 5, data = HELPrct)
```




\newpage

## Density plots (ggformula)

```{r, message = FALSE}
gf_dens(~ age, data = HELPrct)
```

## Overlaid density plots (ggformula)

```{r, message = FALSE}
gf_dens(~ age, data = HELPrct,
        color = ~ sex)
```

\newpage

## Density plots (lattice)

```{r, message = FALSE}
densityplot(~ age, data = HELPrct)
```

## Overlaid density plots (lattice)

```{r}
densityplot(~ age, data = HELPrct,
            groups = sex,  auto.key = TRUE)
```

\newpage

## Side by side box plots (ggformula)

```{r, message = FALSE}
gf_boxplot(age ~ sex, data = HELPrct)
```

## Faceted side by side box plots (ggformula)

```{r, message = FALSE}
gf_boxplot(age ~ sex | homeless, 
  data = HELPrct)
```


\newpage

## Side by side plots (lattice)

```{r, message = FALSE}
bwplot(age ~ sex, data = HELPrct)
```

## Faceted side by side plots (lattice)

```{r, message = FALSE}
bwplot(age ~ sex | homeless, 
       data = HELPrct)
```

\newpage


## Scatterplot (ggformula)

```{r, message = FALSE}
gf_point(cesd ~ age, data = HELPrct)
```



## Overlaid scatterplot with linear fit (ggformula)

```{r, message = FALSE}
gf_point(cesd ~ age, data = HELPrct,
         color = ~ sex) %>%
  gf_lm()
``````

\newpage 

## Scatterplot (lattice)

```{r, message = FALSE}
xyplot(cesd ~ age, data = HELPrct)
```

## Overlaid scatterplot with linear fit (lattice)

```{r, message = FALSE}
xyplot(cesd ~ age,  data = HELPrct,
       groups = sex, 
       type = c("p", "r"), 
       auto.key = TRUE) 
```

\newpage 

## Faceted scatterplot with smooth fit (ggformula)

```{r, message = FALSE}
gf_point(cesd ~ age | sex, 
         data = HELPrct) %>%
  gf_smooth()
``````



## More options for scatterplot with linear fit (ggformula)

```{r}
gf_point(cesd ~ age, data = HELPrct,
         color = ~ sex) %>%
  gf_lm() %>% 
  gf_theme(legend.position = "top") %>% 
  gf_labs(title = "This is my plot", 
    x = "age (in years)", 
    y = "CES-D measure of
depressive symptoms")
```


\newpage

## Faceted scatterplot with smooth fit (lattice)

```{r, message = FALSE}
xyplot(cesd ~ age | sex,  data = HELPrct,
       type = c("p", "smooth"), 
       auto.key = TRUE) 
```




## More options for scatterplot with linear fit (lattice)

```{r, message = FALSE}
xyplot(cesd ~ age, groups = sex, 
       type = c("p", "r"), 
       auto.key = TRUE, 
       main = "This is my plot", 
       xlab = "age (in years)", 
       ylab = "CES-D measure of
depressive symptoms",
       data = HELPrct)
```

## Want to explore more?

Within RStudio, after loading the `mosaic` package, ry running the command `mplot(ds)` where `ds` is a dataframe.  This will open up an interactive visualizer that will output the code to generate the figure (using `lattice`, `ggplot2`, or `ggformula`) when you click on `Show Expression`. 






