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