我有下面的表x
,看起来像这样:
> x <- table(married_working_women$fem_ed, married_working_women$hom_ed)
> rownames(x) <- c("< HS", "HS", "> HS")
> colnames(x) <- c("< HS", "HS", "> HS")
> x
< HS HS > HS
< HS 2410 112 283
HS 63 83 55
> HS 44 49 172
我使用以下代码创建镶嵌图:
library(RColorBrewer)
mosaicplot(x,
main = "Education Levels of Working Spouses",
xlab = "Wife's Education",
ylab = "Husband's Education",
las = 1, color = brewer.pal(3, name = "Pastel2"))
这给出了这个结果:
现在我想把表x
中的数字,可能还有百分比,添加到马赛克图中。
我尝试使用mosaic
函数,但它给出了一个错误:
> library(vcd)
> mosaic(x, labeling = labeling_cells(text = round(x, 2)), legend = TRUE)
Error in `[.default`(c(2410, 63, 44, 112, 83, 49, 283, 55, 172), `NA` = NA_character_, :
subscript out of bounds
有谁能给予我一个提示,告诉我如何给mosaicplot
函数添加标签吗?提前非常感谢。
1条答案
按热度按时间sqserrrh1#
我不知道为什么,但关键是永远不要使用
colnames(x)
和rownames(x)
。在创建表时应该使用dimnames(x)
。代码基于RStudio post和StackOverflow post。