## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  message = FALSE,
  warning = FALSE,
  comment = "#>"
)

## ----setup--------------------------------------------------------------------
library(ggmosaic2)

## ----topright-code, eval=FALSE------------------------------------------------
# HairEyeColor |>
#   as.data.frame() |>
#   ggplot(aes(x = product(Sex, Eye, Hair), fill = Hair, weight = Freq)) +
#     geom_mosaic()

## ----topright-display, out.width='47%', fig.show='hold', echo=FALSE-----------
knitr::include_graphics(c(
  "fig/topright-old.png",
  "fig/topright-new.png"
))

## ----theme-code, eval=FALSE---------------------------------------------------
# HairEyeColor |>
#   as.data.frame() |>
#   ggplot(aes(x = product(Sex, Eye, Hair), fill = Hair, weight = Freq)) +
#     geom_mosaic() +
#     theme_mosaic(base_size = 12)

## ----theme-display, out.width='47%', fig.show='hold', echo=FALSE--------------
knitr::include_graphics(c(
  "fig/theme-old.png",
  "fig/theme-new.png"
))

## ----rot-labels, fig.width=9, fig.height=7, dpi=300, fig.align='center', out.width='86%'----
HairEyeColor |>
  as.data.frame() |>
  ggplot(aes(x = product(Sex, Eye, Hair), fill = Hair, weight = Freq)) +
    geom_mosaic() +
    theme_mosaic(rot_labels = 30, base_size = 14)

## ----reuse-code, eval=FALSE---------------------------------------------------
# HairEyeColor |>
#   as.data.frame() |>
#   ggplot(aes(x = product(Sex, Eye, Hair), fill = Hair, weight = Freq)) +
#     geom_mosaic() +
#     theme_mosaic()

## ----reuse-display, out.width='47%', fig.show='hold', echo=FALSE--------------
knitr::include_graphics(c(
  "fig/theme-old.png",
  "fig/theme-new.png"
))

## ----facet-old-code, eval=FALSE-----------------------------------------------
# HairEyeColor |>
#   as.data.frame() |>
#   ggplot(aes(x = product(Eye, Hair), fill = Hair, weight = Freq)) +
#   geom_mosaic() +
#   theme_mosaic() +
#   facet_grid(. ~ Sex)

## ----facet-old-display, out.width='80%', fig.align='center', echo=FALSE-------
knitr::include_graphics("fig/facet-old.png")

## ----facet-new-code, eval=FALSE-----------------------------------------------
# HairEyeColor |>
#   as.data.frame() |>
#   ggplot(aes(x = product(Eye, Hair), fill = Hair, weight = Freq)) +
#   geom_mosaic() +
#   theme_mosaic() +
#   facet_mosaic_grid(. ~ Sex)

## ----facet-new-display, out.width='80%', fig.align='center', echo=FALSE-------
knitr::include_graphics("fig/facet-new.png")

## ----residual, fig.width=9, fig.height=7, dpi=300, fig.align='center', out.width='86%'----
HairEyeColor |>
  as.data.frame() |>
  ggplot(aes(x = product(Sex, Eye, Hair), weight = Freq)) +
  geom_mosaic(expected = "independence") +
  scale_fill_residual() +
  theme_mosaic(rot_labels = 30)

## ----residual-limit, fig.width=9, fig.height=7, dpi=300, fig.align='center', out.width='86%'----
HairEyeColor |>
  as.data.frame() |>
  ggplot(aes(x = product(Sex, Eye, Hair), weight = Freq)) +
  geom_mosaic(expected = "independence") +
  scale_fill_residual(limits = c(-4,4)) +
  theme_mosaic(rot_labels = 30)

## ----residual-legend-1, fig.width=9, fig.height=7, dpi=300, fig.align='center', out.width='86%'----
HairEyeColor |>
  as.data.frame() |>
  ggplot(aes(x = product(Sex, Eye, Hair), weight = Freq)) +
  geom_mosaic(expected = "independence") + # `show.legend = FALSE` works as well
  scale_fill_residual(limits = c(-4,4)) +
  theme_mosaic(rot_labels = 30, legend.position = "none")

## ----residual-legend-2, fig.width=9, fig.height=7, dpi=300, fig.align='center', out.width='86%'----
HairEyeColor |>
  as.data.frame() |>
  ggplot(aes(x = product(Sex, Eye, Hair), weight = Freq)) +
  geom_mosaic(expected = "independence") +
  scale_fill_residual(limits = c(-4,4)) +
  theme_mosaic(rot_labels = 30, legend.position = "bottom")

## ----residual-outlines, fig.width=9, fig.height=7, dpi=300, fig.align='center', out.width='86%'----
HairEyeColor |>
  as.data.frame() |>
  ggplot(aes(x = product(Sex, Eye, Hair), weight = Freq)) +
  geom_mosaic(expected = "independence",
              color = NA) +
  scale_fill_residual(limits = c(-4,4)) +
  theme_mosaic(rot_labels = 30, legend.position = "bottom")

## ----mosaic-settings, fig.width=9, fig.height=7, dpi=300, fig.align='center', out.width='86%'----
HairEyeColor |>
  as.data.frame() |>
  ggplot(aes(x = product(Sex, Eye, Hair), weight = Freq)) +
  mosaic_settings(expected = "independence") +
  geom_mosaic() +
  geom_mosaic_text(display_values = "residual",
                   format_digits = 1) +
  scale_fill_residual(limits = c(-4,4)) +
  theme_mosaic(rot_labels = 30)

