## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 7,
  fig.height = 4.5
)

## ----eval = FALSE-------------------------------------------------------------
# # From CRAN
# install.packages("marimekko")
# 
# # From GitHub (when published)
# devtools::install_github("gogonzo/marimekko")

## ----basic--------------------------------------------------------------------
library(ggplot2)
library(marimekko)

titanic <- as.data.frame(Titanic)

ggplot(titanic) +
  geom_marimekko(aes(fill = Survived, weight = Freq), formula = ~ Class | Survived) +
  labs(title = "Titanic survival by class")

## ----unweighted---------------------------------------------------------------
ggplot(mtcars) +
  geom_marimekko(aes(fill = factor(gear)),
    formula = ~ cyl | gear
  )

## ----gap----------------------------------------------------------------------
ggplot(titanic) +
  geom_marimekko(aes(fill = Survived, weight = Freq),
    formula = ~ Class | Survived, gap = 0.03
  ) +
  labs(title = "Wider gaps (gap = 0.03)")

## ----no-gap-------------------------------------------------------------------
ggplot(titanic) +
  geom_marimekko(aes(fill = Survived, weight = Freq),
    formula = ~ Class | Survived, gap = 0
  ) +
  labs(title = "No gaps")

## ----pct----------------------------------------------------------------------
ggplot(titanic) +
  geom_marimekko(aes(fill = Survived, weight = Freq),
    formula = ~ Class | Survived,
    show_percentages = TRUE
  )

## ----text-labels--------------------------------------------------------------
ggplot(titanic) +
  geom_marimekko(aes(fill = Survived, weight = Freq), formula = ~ Class | Survived) +
  geom_marimekko_text(aes(label = after_stat(weight)), colour = "white") +
  labs(title = "Counts inside tiles")

## ----pct-labels---------------------------------------------------------------
ggplot(titanic) +
  geom_marimekko(aes(fill = Survived, weight = Freq), formula = ~ Class | Survived) +
  geom_marimekko_text(aes(
    label = after_stat(paste0(round(cond_prop * 100), "%"))
  ), colour = "white", size = 3)

## ----theme--------------------------------------------------------------------
ggplot(titanic) +
  geom_marimekko(aes(fill = Survived, weight = Freq), formula = ~ Class | Survived) +
  theme_marimekko() +
  labs(title = "With theme_marimekko()")

## ----theme-custom-------------------------------------------------------------
ggplot(titanic) +
  geom_marimekko(aes(fill = Survived, weight = Freq), formula = ~ Class | Survived) +
  theme_marimekko() +
  theme(legend.position = "bottom")

## ----facet--------------------------------------------------------------------
ggplot(as.data.frame(Titanic)) +
  geom_marimekko(aes(fill = Survived, weight = Freq), formula = ~ Class | Survived) +
  facet_wrap(~Sex) +
  labs(title = "Survival by class, faceted by sex")

