Skip to contents

Split the unit square recursively using observed or expected mass in each parent. The product of the defined conditional area shares equals a cell's global area share. With zero gaps this is also the visible area. At each branching split a gap fraction is reserved from the parent span before allocating the remainder. Cells with zero selected mass have zero area. A zero-mass parent has undefined conditional proportions. The default model is mutual independence of all dimensions.

Usage

mosaic_layout(
  counts,
  direction = NULL,
  gap = 0,
  sort = "level",
  model = "independence",
  expected = NULL,
  structural = NULL,
  weight_type = NULL,
  max_cells = 100000L,
  given = NULL,
  residual = c("pearson", "standardized", "deviance", "FT"),
  type = c("observed", "expected"),
  zero_split = FALSE,
  residuals = NULL,
  layout_size = NULL
)

Arguments

counts

Numeric array, table or xtabs object of dimension two or greater.

direction

One value or one per split. "x", "v", and "right" place levels from left to right. "y" and "top" place them bottom to top. "left" reverses the horizontal order. "h" and "bottom" place levels top to bottom like a printed table. The default alternates x and y.

gap

Fraction of each parent span reserved for space between positive siblings, in [0, 1). Supply one number or one per depth. The "hierarchical" preset reserves 0.055 times 0.60 to the power of depth minus one. "conditional" separates the conditioning dimensions in given more strongly and uses 0.016 for other dimensions. Absolute grid lengths specify the distance between adjacent positive siblings at the target layout_size. A function receives one named list with depth, variable, axis, parent, level order and mass. It may return one total fraction, individual gap fractions, or absolute gap units.

sort

Preserve level order ("level") or order each parent's children by decreasing mass ("decreasing"). Can vary by depth. Ties preserve level order.

model

"independence" (mutual independence), "conditional", "none", "custom", a one-sided hierarchical log-linear formula made from dimension names, or a fitted Poisson log-link GLM. A GLM must store a model frame with one matching row per possible cell, all original dimension keys and the same counts, with unit prior weights. A fitted MASS loglm object must retain both its fitted and frequency arrays. Its generating margins are checked to relative tolerance 1e-6 using max(1, observed) as denominator. Its reported df is stored separately.

expected

For a custom model, a nonnegative expected array with the same dimensions, keys, ordering and grand total as the counts.

structural

Optional aligned logical array of impossible cells. Their observed and fitted counts must be zero. Supply an explicit formula for a fit on restricted support, a matching fitted GLM or custom array, or use the descriptive model.

weight_type

Frequency counts must be finite nonnegative integers. Use "survey" with model = "none" for descriptive weighted areas.

max_cells

Limit on the dense cross-product. The combined number of hierarchy nodes is also limited to eight times this value.

given

Character vector of conditioning dimensions for model = "conditional". At least two dimensions must remain. Each combination of the conditioning levels defines a separate stratum.

residual

Residual returned in the residual column. 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.

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.

zero_split

If TRUE, subdivide the remaining span of a zero-mass parent equally among its levels. Their areas remain zero. The default places their anchors together at the starting edge of the parent split.

residuals

Optional numeric residual array with named dimensions matching the original counts, or a function of the observed and expected arrays returning such an array. Use NA for undefined values. This replaces the displayed residual and preserves built-in model diagnostics. For independently fitted panels supply a function. With a table-wide fit the function runs once on the complete table.

layout_size

Optional target tile field width and height in millimetres. Absolute grid unit gaps use this field, defaulting to 100 by 100 mm. A high-level plot fixes its panel to this size plus border label space. Supply enough device space for all panels, titles and legends.

Value

A list with a complete cells data frame, a nodes hierarchy, and the named key_names mapping from original dimension names to cell columns. It also records the variables, model, weight type and layout settings. The diagnostics list records method, support, effective rank and residual degrees of freedom when available, Pearson sum of squares and Poisson deviance. These are descriptive fit summaries. Each cell has observed and expected counts, the selected residual, Pearson and deviance residuals, and (when requested) leverage and standardized residual, global and immediate-parent observed shares, area_mass, area_proportion, conditional_area_proportion, rectangle bounds, a label centre, depth, node ID, parent ID and a structural-zero flag. For zero expectation the residual is missing. IDs are local to this layout.

Examples

layout <- mosaic_layout(Titanic)
head(layout$cells[c("Class", "observed", "expected", "residual")])
#>   Class observed  expected  residual
#> 1   1st        0  8.569058 -2.927295
#> 2   2nd        0  7.514404 -2.741241
#> 3   3rd       35 18.614630  3.797773
#> 4  Crew        0 23.334203 -4.830549
#> 5   1st        0  2.326665 -1.525341
#> 6   2nd        0  2.040306 -1.428393
mosaic_layout(UCBAdmissions, model = "conditional", given = "Dept")$diagnostics
#> $method
#> [1] "product of within-stratum margins"
#> 
#> $converged
#> [1] TRUE
#> 
#> $iterations
#> [1] 0
#> 
#> $possible_cells
#> [1] 24
#> 
#> $structural_cells
#> [1] 0
#> 
#> $positive_expected_cells
#> [1] 24
#> 
#> $rank
#> [1] 18
#> 
#> $df_residual
#> [1] 6
#> 
#> $pearson_x2
#> [1] 19.93841
#> 
#> $deviance
#> [1] 21.73551
#> 
mosaic_layout(HairEyeColor, model = ~ Hair * Sex + Eye * Sex,
              residual = "standardized")$diagnostics
#> $method
#> [1] "Poisson log-linear GLM"
#> 
#> $converged
#> [1] TRUE
#> 
#> $iterations
#> [1] 6
#> 
#> $possible_cells
#> [1] 32
#> 
#> $structural_cells
#> [1] 0
#> 
#> $positive_expected_cells
#> [1] 32
#> 
#> $rank
#> [1] 14
#> 
#> $df_residual
#> [1] 18
#> 
#> $pearson_x2
#> [1] 147.944
#> 
#> $deviance
#> [1] 156.6779
#>