Creating mosaic plot in R

2.6k Views Asked by At

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...

3

There are 3 best solutions below

0
On BEST ANSWER

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 than geom_barr(). In order to have the bars fill the either region, add position="fill" to the geometry definition.

df = structure(list(effect = structure(c(2L, 2L, 1L, 1L), .Label = c("yes", 
     "no"), class = "factor"), sex = structure(c(1L, 2L, 1L, 2L), 
      .Label = c("f",  "m"), class = "factor"), n = c(8, 3, 8, 12)), 
      row.names = c(NA, -4L), class = "data.frame")

ggplot(df, aes(effect, y=n, fill = sex)) +
  geom_col(position="fill")

enter image description here

To change the bar's widths you can try something like:

library(dplyr)
widths<-df %>% group_by(effect) %>% summarize(value=sum(n)) %>% mutate(value=value/sum(value))
ggplot(df, aes(effect, y=n, fill = sex)) +
  geom_col(position="fill", width=1.8*rep(widths$value, each=2))
0
On

You can use the mosaicplot function from graphics. However, the data needs to be in a "table" or raw data format, not aggregated. Your data is aggregated, so we need to "deaggregate" it using xtabs:

xtab <- xtabs(n~sex+effect, data=df)
   effect
sex yes no
  f   8  8
  m  12  3

Then either of the following will work.

mosaicplot(xtab, main="Sex v Effect", col=TRUE)
mosaicplot(~sex+effect, data=xtab, main="Sex v Effect", col=TRUE)

enter image description here

0
On

You could also use the ggmosaic package to create a mosaic plot with a ggplot2 look like this:

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)
library(ggmosaic)
ggplot(data = df) +
  geom_mosaic(aes(x = product(effect), fill=sex, weight = n)) +
  theme_mosaic()

Created on 2022-08-15 by the reprex package (v2.0.1)