Search code examples
rplotmosaic

Mosaic plot with variation by color and size in R


Here is my dataset

library(dplyr)
df <- data.frame(var_1 = sample(c('A', 'B', 'C'), 1000, replace =TRUE), 
                 var_2 = sample(c(0,1), 1000, replace = TRUE))
df <- df %>% group_by(var_1) %>% summarize(count = n(),
                                             avg = mean(var_2))

I would like to create a plot similar to mosaic where size of square would correspond to count variable and color of a square would correspond to avg variable. Is this possible?


Solution

  • What you're looking for is called a "treemap". There is a library to create treemaps in ggplot2, called treemapify:

    library(tidyverse)
    library(magrittr)
    library(treemapify)
    
    df %>% ggplot(aes(area = count, fill = avg)) + geom_treemap()
    

    enter image description here