Skip to contents

Set the divider, gap size, and model used to calculate expected frequencies for mosaic layers in a plot. A value set directly in a layer takes priority.

Usage

mosaic_settings(divider, offset, expected)

Arguments

divider

A divider function, a character vector naming divider functions, or a list of divider functions.

offset

A single non-negative number giving the gap at the deepest split. Gaps increase by a factor of 1.5 toward the outermost split.

expected

The log-linear model used to calculate expected frequencies and Pearson residuals. Supply a formula or one of "independence", "saturated", or "conditional". The conditional model requires one or more variables mapped to conds. Supply NULL to turn off model fitting, including a model set by an earlier call to mosaic_settings().

Value

An object of class "ggmosaic_settings" that can be added to a ggplot with +.

Details

A layer inherits a setting only when its corresponding argument is omitted. Settings are resolved in this order: an explicitly supplied layer argument (including expected = NULL), the plot's mosaic_settings() value, and the layer default (mosaic(), 0.01, or NULL).

divider and offset are inherited by geom_mosaic(), stat_mosaic(), geom_mosaic_text(), stat_mosaic_text(), geom_mosaic_jitter(), and stat_mosaic_jitter(). expected is inherited by the same constructors except the mosaic-jitter geom and stat.

The position of mosaic_settings() among the layers in a plot has no effect. If it is added more than once, the last supplied value for each setting is used; omitted arguments do not change earlier settings. Settings are local to the plot and do not change package or session defaults. They share configuration, not computations or fitted model objects, between layers.

Author

Gavin Klorfine

Examples

data(titanic)

ggplot(titanic, aes(x = product(Class, Sex))) +
  mosaic_settings(
    expected = "independence",
    divider = c("vspine", "hspine"),
    offset = 0.005
  ) +
  geom_mosaic() +
  geom_mosaic_text(display_values = "residual") +
  scale_fill_residual() +
  theme_mosaic()


# An explicit layer argument overrides the plot setting. Here NULL turns
# model fitting off for the mosaic layer.
ggplot(titanic, aes(x = product(Class, Sex), fill = Survived)) +
  mosaic_settings(expected = "independence") +
  geom_mosaic(expected = NULL)