rggplot2mosaic-plot

Creating mosaic plot in R


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


Solution

  • 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))