ggformula/lattice Comparison

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)

library(mosaic)   # also loads ggformula 
gf_histogram(~ age, data = HELPrct)

Histogram options (ggformula)

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)

library(mosaic)     # also loads lattice
histogram(~ age, data = HELPrct)

Histogram options (lattice)

histogram(~ age, width = 5, data = HELPrct)

\newpage

Density plots (ggformula)

gf_dens(~ age, data = HELPrct)

Overlaid density plots (ggformula)

gf_dens(~ age, data = HELPrct,
        color = ~ sex)

\newpage

Density plots (lattice)

densityplot(~ age, data = HELPrct)

Overlaid density plots (lattice)

densityplot(~ age, data = HELPrct,
            groups = sex,  auto.key = TRUE)

\newpage

Side by side box plots (ggformula)

gf_boxplot(age ~ sex, data = HELPrct)

Faceted side by side box plots (ggformula)

gf_boxplot(age ~ sex | homeless, 
  data = HELPrct)

\newpage

Side by side plots (lattice)

bwplot(age ~ sex, data = HELPrct)

Faceted side by side plots (lattice)

bwplot(age ~ sex | homeless, 
       data = HELPrct)

\newpage

Scatterplot (ggformula)

gf_point(cesd ~ age, data = HELPrct)

Overlaid scatterplot with linear fit (ggformula)

gf_point(cesd ~ age, data = HELPrct,
         color = ~ sex) %>%
  gf_lm()
``````

\newpage 

## Scatterplot (lattice)

```r
xyplot(cesd ~ age, data = HELPrct)

Overlaid scatterplot with linear fit (lattice)

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

\newpage

Faceted scatterplot with smooth fit (ggformula)

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)

xyplot(cesd ~ age | sex,  data = HELPrct,
       type = c("p", "smooth"), 
       auto.key = TRUE) 

More options for scatterplot with linear fit (lattice)

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.



Try the mosaic package in your browser

Any scripts or data that you put into this service are public.

mosaic documentation built on Aug. 27, 2017, 1:03 a.m.