我尝试了很多东西,但无法使马赛克情节发挥作用。我从一个数据框开始:
df = data.frame(effect = c("no","no", "yes", "yes"),
sex = c("f","m","f","m"),
n = c(8,3,8,12))
df$effect <- factor((df$effect), levels=c("yes", "no"))
df$sex <- factor(df$sex)
我试过ggplot:
windows(width=3.5, height=3.5 )
ggplot(df) +
geom_bar(aes(effect, fill = sex))
我尝试了另一个ggplot:
library(ggmosaic)
windows(width=3.5, height=3.5 )
ggplot(df) +
geom_mosaic(aes(x = product(effect), fill = sex)) +
labs(x = "effect", y = "number")
我尝试了另一种方法:
library("graphics")
windows(width=3.5, height=3.5 )
with(df,
mosaicplot(table(effect, sex), color=TRUE))
无论我尝试了什么,单元格中的数字都没有在图上正确表示。我无法弄清楚我做错了什么......