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

☆樱花仙子☆ 提交于 2019-11-30 05:42:10

问题


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.


回答1:


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)



来源:https://stackoverflow.com/questions/20228326/mosaic-plot-with-labels-in-each-box-showing-a-name-and-percentage-of-all-observa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!