How to create a Marimekko/Mosaic plot in ggplot2

后端 未结 7 742
我寻月下人不归
我寻月下人不归 2020-11-27 02:59

The Marimekko/Mosaic plot is a nice default plot when both x and y are categorical variables. What is the best way to create these using ggplot?

相关标签:
7条回答
  • 2020-11-27 03:30

    Following user2030503's suggestion, here's a version that uses ggmosaic. (Note that ggplot 3.0 broke some piece of ggmosaic, so you need the most recent version.)

    library(tidyverse)
    library(ggmosaic)
    
    # Data copied from linked blog post
    df <- data.frame(
      segment = LETTERS[1:4],
      segpct = c(40, 30, 20, 10),
      Alpha = c(60, 40, 30, 25), 
      Beta = c(25, 30, 30, 25),
      Gamma = c(10, 20, 20, 25), 
      Delta = c(5, 10, 20, 25)
      )
    
    # Convert to "long" for plotting
    df_long <- gather(df, key = "greek_letter", value = "pct", 
                      -c("segment", "segpct")) %>% 
      mutate(
        greek_letter = factor(
          greek_letter, 
          levels = c("Alpha", "Beta", "Gamma", "Delta")
          ),
        weight = (segpct * pct) / 10000
        )
    
    # Plot
    ggplot(df_long) +
      geom_mosaic(aes(x = product(greek_letter, segment), fill = greek_letter,
                      weight = weight))
    

    0 讨论(0)
提交回复
热议问题