##
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 improved `ggformula`

system. Going forward, `ggformula`

will be the preferred graphics package for Project MOSAIC.

##
Histograms

###
Histograms (lattice)

###
Histogram options (lattice)

##
Density Plots

###
Density plots (lattice)

densityplot(~ age, data = HELPrct)

###
Overlaid density plots (lattice)

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

### Density over histograms (lattice)

`mosaic`

makes it easy to add a fitted distribution to a histogram.

histogram(~ age, data = HELPrct,
fit = "normal", dcol = "red")

##
Side by side boxplots

###
Side by side boxplots (lattice)

bwplot(age ~ sex, data = HELPrct)

###
Faceted side by side boxplots (lattice)

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

###
Horizontal boxplots (lattice)

bwplot(sex ~ age, data = HELPrct)

##
Scatterplots

###
Basic Scatterplot (lattice)

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)

##
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 lattice plot",
xlab = "age (in years)",
ylab = "CES-D measure of
depressive symptoms",
data = HELPrct)

##
Refining graphs

###
Log scales (lattice)

xyplot(
cesd ~ age, data = HELPrct,
scales = list(y = list(log = TRUE)))

###
Custom Colors (lattice)

densityplot(
~ cesd, data = HELPrct, groups = sex,
rug = FALSE,
par.settings =
list(
superpose.line =
list(col = c("navy", "red")),
superpose.symbol =
list(col = c("navy", "red"))
))

##
Want to explore more?

Within RStudio, after loading the `mosaic`

package, try 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`

.