Compare categorical relationships in a Mosaic matrix

ggmosaic_pairs.RdEach off-diagonal panel is a separate call to the shared Mosaic layout and model engine. The first dimension of a marginal plot comes from its column. Choose an independence hypothesis separately for the upper and lower triangles. All panels share one residual colour scale.
Arguments
- x
A frequency table, multidimensional array, xtabs, or data frame.
- vars, weight
For data frames, dimension columns (in matrix order) and an optional frequency column. Use bare names, quoted names or
all_of(names). Arrays supply dimension names and counts.- upper, lower
Hypothesis to display in each triangle.
"pairwise"sums over other dimensions, then fits independence to the two-way table."total"fits mutual independence to the full table."conditional"splits on the remaining variables first and fits independence of the displayed pair within their joint strata."joint"fits independence between the pair taken together and its complement taken together. Use"none"to leave one triangle blank.- diagonal
Draw observed univariate marginal proportions as bars, or print dimension names. Each bar height uses the same zero-to-one scale.
- type
Use
"observed"or"expected"frequencies for the area. Expected area requires a fitted model or supplied expected counts. The observed frequencies, observed shares and fitted residuals are available in both displays.- residual
Residual returned in the
residualcolumn. Choices are"pearson","standardized"(Poisson Pearson divided by the square root of one minus leverage), signed Poisson"deviance", and"FT"(Freeman-Tukey, square root of observed plus square root of observed plus one, minus the square root of four times expected plus one). An expected array alone cannot supply leverage. Unit leverage or zero expectation gives an undefined standardized residual.- gap
Parent-relative numeric spacing or a named spacing preset shared by the panels. Absolute device units are unavailable in a matrix because its facet size is determined when the whole plot is drawn.
- lang, dictionary
Display language and optional local dictionary. Matrix strips use the variable context. Diagonal level labels use the corresponding original variable name as their dictionary context.
- max_cells
Maximum cross-product size of the input and maximum combined count of all off-diagonal rectangles before drawing.
Value
A standard ggplot. Its data contains the off-diagonal counts,
expectations, selected residuals, bounds and source IDs in .mosaic_keys.
Collapsed dimensions have missing IDs in marginal cells. Diagonal bars
summarize the original observed table for both area types. Shared
residual shading is descriptive across the matrix. In
plotly::ggplotly(), off-diagonal tooltips show the panel's model,
original category IDs, observed and expected counts and the selected
residual. Dictionary context "mosaic.model" translates the model
IDs pairwise, total, conditional and joint.
References
Meyer, Zeileis and Hornik (2006), The Strucplot Framework: Visualizing Multi-way Contingency Tables with vcd. doi:10.18637/jss.v017.i03 .
