【发布时间】:2023-01-20 01:26:47
【问题描述】:
我有下表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 函数添加标签的提示吗?非常感谢你提前。
【问题讨论】:
标签: r plot mosaic-plot