rmosaic-plot

Relabel the bins in a mosaic plot


In a mosaicplot, how do I relabel the bins?

dat <- data.frame(letters = sample(LETTERS[1:3], 15, replace = TRUE),
                  numbers = sample(3, 15, replace = TRUE))
mosaicplot(table(dat))

That is, how can I change the "A", "B", and "C" in the plot below to, say, "dogs", "cows", and "chicken"?

enter image description here


Solution

  • As an addition to @MattTyers solution and a follow-up to the question whether everything can be done in one go: You can use the formula method for mosaicplot() and employ factor() inside the formula to assign new labels to the levels of the factor. Note that you need to set xlab and ylab explicitly in order not to have labels like factor(..., labels = ...).

    mosaicplot(~ factor(letters, labels = c("Aah", "Bee", "Cea")) +
      factor(numbers, labels = c("one", "two", "three")),
      data = dat, xlab = "Letters", ylab = "Numbers")