## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 6,
  fig.height = 4
)
library(ggplot2)
library(marimekko)

titanic <- as.data.frame(Titanic)

## ----one-var------------------------------------------------------------------
# marimekko — one variable
ggplot(titanic) +
  geom_marimekko(aes(fill = Class, weight = Freq), formula = ~Class) +
  theme_marimekko() +
  labs(title = "One variable: f(Class)")

## ----one-var-bar--------------------------------------------------------------
# ggplot — barchart
# todo: needs percentage y scale
ggplot(titanic) +
  geom_bar(aes(Class, weight = Freq, fill = Class)) +
  theme_marimekko() +
  labs(title = "One variable: f(Class)")

## ----two-var------------------------------------------------------------------
# marimekko — two variables via formula
ggplot(titanic) +
  geom_marimekko(aes(fill = Survived, weight = Freq),
    formula = ~ Class | Survived
  ) +
  theme_marimekko() +
  labs(title = "Two variables: f(Survived | Class) f(Class)")

## ----three-var-multi----------------------------------------------------------
# marimekko — three variables via formula
ggplot(titanic) +
  geom_marimekko(aes(fill = Survived, weight = Freq),
    formula = ~ Sex + Class | Survived
  ) +
  theme_marimekko() +
  labs(title = "Three variables: f(Survived | Class, Sex)")

## ----three-var----------------------------------------------------------------
# marimekko — three variables via formula
ggplot(titanic) +
  geom_marimekko(aes(fill = Survived, weight = Freq),
    formula = ~ Class | Survived | Sex
  ) +
  theme_marimekko() +
  labs(title = "Three variables: Class / Sex / Survived")

## ----conds--------------------------------------------------------------------
# marimekko — conditioning via faceting
ggplot(titanic) +
  geom_marimekko(aes(fill = Survived, weight = Freq),
    formula = ~ Class | Survived
  ) +
  facet_wrap(~Sex) +
  theme_marimekko() +
  labs(title = "f(Survived | Class) conditioned on Sex")

## ----facet--------------------------------------------------------------------
# marimekko — faceting works the same way
ggplot(titanic) +
  geom_marimekko(aes(fill = Survived, weight = Freq),
    formula = ~ Class | Survived
  ) +
  facet_wrap(~Sex) +
  theme_marimekko() +
  labs(title = "Mosaic faceted by Sex")

## ----default-width------------------------------------------------------------
# marimekko — proportional-width columns (default)
ggplot(titanic) +
  geom_marimekko(aes(fill = Survived, weight = Freq),
    formula = ~ Class | Survived
  ) +
  theme_marimekko() +
  labs(title = "Mosaic plot (proportional-width columns)")

## ----gaps---------------------------------------------------------------------
# marimekko — no gaps
ggplot(titanic) +
  geom_marimekko(aes(fill = Survived, weight = Freq),
    formula = ~ Class | Survived, gap_x = 0.02, gap_y = 0.02
  ) +
  theme_marimekko() +
  labs(title = "No gaps")

## ----text---------------------------------------------------------------------
# marimekko — automatic tile positions, only label needed
ggplot(titanic) +
  geom_marimekko(aes(fill = Survived, weight = Freq),
    formula = ~ Class | Survived
  ) +
  geom_marimekko_text(aes(
    label = after_stat(paste(Class, Survived, sep = "\n"))
  )) +
  theme_marimekko()

## ----axis---------------------------------------------------------------------
# marimekko — with optional marginal percentages
ggplot(titanic) +
  geom_marimekko(aes(fill = Survived, weight = Freq),
    formula = ~ Class | Survived,
    show_percentages = TRUE
  ) +
  theme_marimekko()

## ----hair-setup, include = FALSE----------------------------------------------
hair <- as.data.frame(HairEyeColor)

## ----hair-mosaic--------------------------------------------------------------
ggplot(hair) +
  geom_marimekko(aes(fill = Eye, weight = Freq),
    formula = ~ Hair | Eye,
    show_percentages = TRUE
  ) +
  theme_marimekko() +
  labs(title = "Hair color vs Eye color")

## ----hair-multi---------------------------------------------------------------
ggplot(hair) +
  geom_marimekko(aes(fill = Eye, weight = Freq),
    formula = ~ Hair | Sex | Eye
  ) +
  theme_marimekko() +
  labs(title = "Hair / Sex / Eye")

## ----plotly, eval=FALSE-------------------------------------------------------
# library(plotly)
# 
# p <- ggplot(titanic) +
#   geom_marimekko(aes(fill = Survived, weight = Freq),
#     formula = ~ Class | Survived
#   )
# 
# ggplotly(p)

