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

StackOverflow https://stackoverflow.com/questions/20228326

  •  05-08-2022
  •  | 
  •  

質問

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.

役に立ちましたか?

解決

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top