I have tried many things but cannot make the mosaic plot work. I start with a data frame:
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)
I tried ggplot:
windows(width=3.5, height=3.5 )
ggplot(df) +
geom_bar(aes(effect, fill = sex))
I tried another 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")
I tried another approach:
library("graphics")
windows(width=3.5, height=3.5 )
with(df,
mosaicplot(table(effect, sex), color=TRUE))
Whatever I tried the numbers in the cells are not represented correctly on the plots. I cannot figure out what I am doing wrong...
You need to include the value for n in the definition of the plot. Also since you are summing the values a
geom_col()
is more appropriate thangeom_barr()
. In order to have the bars fill the either region, add position="fill" to the geometry definition.To change the bar's widths you can try something like: