Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mosaic plot with labels in each box showing a name and percentage of all observations

I would like to create a mosaic plot (R package vcd, see e.g. http://cran.r-project.org/web/packages/vcd/vignettes/residual-shadings.pdf ) with labels inside the plot. The labels should show either a combination of the various factors or some custom label and the percentage of total observations in this combination of categories (see e.g. http://i.usatoday.net/communitymanager/_photos/technology-live/2011/07/28/nielsen0728x-large.jpg , despite this not quite being a mosaic plot). I suspect something like the labeling_values function might play a role here, but I cannot quite get it to work.

library(vcd)
library(MASS)
data("Titanic")
mosaic(Titanic, labeling = labeling_values)

Alternative ways to represent two variables with categorical data in a friendly way for non-statisticians are also welcome and are acceptable solutions.

like image 553
Rob Hall Avatar asked Nov 26 '13 21:11

Rob Hall


1 Answers

Here is an example of adding proportions as labels. As usual, the degree of customization of a plot is a matter of taste, but this shows at least the principles. See ?labeling_cells for further possibilities.

labs <- round(prop.table(Titanic), 2)
mosaic(Titanic, pop = FALSE)
labeling_cells(text = labs, margin = 0)(Titanic)

enter image description here

like image 75
Henrik Avatar answered Nov 02 '22 12:11

Henrik