15 Chart: Mosaic

This chapter originated as a community contribution created by harin

This page is a work in progress. We appreciate any input you may have. If you would like to help improve this page, consider contributing to our repo.

15.1 Overview

This section covers how to make Mosaic plots

15.3 Order of splits

It is best to draw mosaic plots incrementally: start with splitting on one variable and then add additional variables one at a time. The full mosaic plot will have one split per variable.

Important: if your data is in a data frame, as in the example below, the count column must be called Freq. (Tables and matrices also work, see ?vcd::structable for more details.)

Also note that all of these plots are drawn with vcd::mosaic() not the base R function, mosaicplot().

The data:

##     Age   Favorite     Music Freq
## 1   old bubble gum classical    1
## 2   old bubble gum      rock    1
## 3   old     coffee classical    3
## 4   old     coffee      rock    1
## 5 young bubble gum classical    2
## 6 young bubble gum      rock    5
## 7 young     coffee classical    1
## 8 young     coffee      rock    0

Split on Age only:

Split on Age, then Music:

Note that the first split is between “young” and “old”, while the second set of splits divides each age group into “classical” and “rock”.

Split on Age, then Music, then Favorite:

15.4 Direction of splits

Note that in the previous example, the direction of the splits is as follows:

  1. Age – horizontal split

  2. Music – vertical split

  3. Favorite – horizontal split

This is the default direction pattern: alternating directions beginning with horizontal. Therefore we get the same plot with the following:

The directions can be altered as desired. For example, to create a doubledecker plot, make all splits vertical except the last one:

Note that the direction vector is in order of splits (Age, Music, Favorite), not in the order in which the variables appear in the formula, where the last variable to be split is listed first, before the “~”.

15.6 Labels

For official documentation on labeling options, see Labeling in the Strucplot Framework

15.6.1 Rotate labels

The rot_labels = vector sets the rotation in degrees on the four sides of the plot–not on variable split order–in this order: top, right, bottom, left. (Different from the typical base graphics order!) The default is rot_labels = c(0, 90, 0, 90).

15.6.2 Abbreviate labels

Labels are abbreviated in the order of the splits (as for direction =). The abbreviation algorithm appears to return the specified number of characters after vowels are eliminated (if necessary).

For more formatting options, see >?vcd::labeling_border.

15.9 Theory

15.10 When to use

When you want to see the relationships in Multivariate Categorical Data

15.11 Considerations

15.11.1 Labels

Legibility of the labels is problematic in mosaic plot especially when there are a lot of dimensions. This can be alleviated by - Abbreviate names - Rotating the labels

15.11.2 Aspect Ratio

  • lengths are easier to judge than area, so try to use rectangles with same width or height
  • Taller thinner rectangles are better (we are better at distinguishing length than area)

15.11.3 Gaps between rectangles

  • No gap = most efficient

However, a gap can help improve legibility, so try out different combinations

  • Can have a gap at splits
  • Can Vary gap size down the hierarchy

15.11.4 Color

  • good for rates in the subgroup
  • displaying residual
  • emphasizing particular subgroup

15.12 External resources