Use widths and conditional heights to expose how two categorical variables share one total.
Mosaic plots: make cell area match the underlying count
Choose a denominator before drawing tiles
A two-way mosaic plot partitions one rectangle by a contingency table. Each first-category column gets width proportional to its count in the full data set. Within that column, second-category tile heights represent conditional shares. Multiplying width by height yields the cell's share of the grand total. That area contract is the point of the chart. If the column widths are all equal instead, the display answers a different question: it emphasizes within-group composition and conceals unequal group sizes. Stacked bars are often easier to read when exact conditional rates are the primary comparison, while treemaps use area for hierarchical totals rather than a two-way cross-classification.
Declare the table grain
Count one unit only once in the table unless the chart explicitly represents events rather than entities. If a shipment can appear in several outcome categories, a standard contingency table is not valid until a mutually exclusive rule is chosen. A missing outcome is its own category or an explicit exclusion; quietly dropping it changes every tile's area. Aggregation grain and denominator should be fixed upstream. Record the category order even if one group has a zero count, and distinguish a true zero cell from a category never collected. Reconcile each column subtotal with source records before calculating widths, then reconcile all columns to the grand total. A plot that looks rectangular can still be numerically wrong.
Read association without mistaking size for rate
A wider group contributes more observations, while a taller tile within that group represents a larger conditional share. Readers often compare tile heights across groups and forget the width, or compare areas when the question is only about rates. Label both count and within-group percentage in a companion table. If a tiny tile cannot hold text, put the label outside or leave it to an accessible table rather than enlarging the tile. Heatmaps can encode a rate in color, but their equal-size cells do not show count automatically. Do not imply statistical significance from a striking mosaic pattern; sampling design, uncertainty and multiple subgroup inspections remain separate questions.
Keep order and coverage stable
A mosaic is more legible with a limited number of well-defined categories. Many thin columns or slivers make the visual fragile on phones and at high zoom. Sort categories by a declared business order, not by whichever tile creates the strongest pattern in each filter state. If a dashboard filter removes units, recompute column totals and display the new grand total; a stale denominator can make area and labels disagree. Linked filters should expose which cohort is selected. Export the exact cross-tabulation with count, row total, grand total, conditional percentage and excluded count so the visible geometry can be independently reconstructed.
Worked contract
def mosaic_tiles(table: dict[str, dict[str, int]],
outcomes: list[str]):
if not table or not outcomes or len(outcomes) != len(set(outcomes)):
raise ValueError("groups and ordered outcomes are required")
if any(set(cells) != set(outcomes) for cells in table.values()):
raise ValueError("every group needs every outcome, including zeros")
if any(type(count) is not int or count < 0
for cells in table.values() for count in cells.values()):
raise ValueError("counts must be nonnegative integers")
group_totals = {group: sum(cells.values()) for group, cells in table.items()}
if any(total == 0 for total in group_totals.values()):
raise ValueError("empty groups need a separate missing-data state")
grand_total = sum(group_totals.values())
tiles, left = [], 0.0
for group, cells in table.items():
right = left + group_totals[group] / grand_total
bottom = 0.0
for outcome in outcomes:
top = bottom + cells[outcome] / group_totals[group]
tiles.append((group, outcome, left, right, bottom, top,
cells[outcome]))
bottom = top
left = right
return tiles
review_counts = {
"early shift": {"accepted": 47, "returned": 13},
"late shift": {"accepted": 20, "returned": 20},
}
review_tiles = mosaic_tiles(review_counts, ["accepted", "returned"])
early_accepted = review_tiles[0]
assert abs((early_accepted[3] - early_accepted[2]) *
(early_accepted[5] - early_accepted[4]) - 0.47) < 1e-12
assert review_tiles[-1][-1] == 20Performance and operating cost
For G groups and K outcome categories, validation and tile construction take O(GK) time and O(GK) output space. The positions are normalized to a unit square; the first tile covers 47 percent of the area because 47 of 100 reviewed shipments fall in that cell. Floating-point boundaries are drawing coordinates, not replacement counts. Keep the integer table available for labels and audit exports.
Common Mistakes
- Making every group equally wide while claiming tile area represents total count.
- Dropping unknown outcomes without updating the denominator and exclusion note.
- Enlarging tiny tiles to fit labels, which breaks the area contract.
- Reading an attractive pattern as evidence of association without examining uncertainty or sampling.
Validation drill
Cross-tabulate 83 inspected shipments by shift and disposition, including an unknown disposition. Reconcile the grand total and verify that each tile area equals its cell count divided by 83.
Read next
- Aggregation grain and denominator: make every plotted mark auditable
- Stacked bars: separate composition from volume and preserve denominators
- Treemaps: validate hierarchy and leaf totals before assigning area
- Heatmaps: make missing cells, rates and color domains explicit
