---
title: "Introduction to the mekko package"
output:
  rmarkdown::html_vignette:
    keep_md: true
vignette: >
  %\VignetteIndexEntry{Introduction to the mekko package}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

### Bar mekko graphics in R

This is a quick introduction to the bar mekko function* in
the mekko package.

Its main value is to add quantitative context to a bar graph, via bar width.

```{r set-opts, echo = FALSE}
knitr::opts_chunk$set(
  fig.width = 7,
  fig.height = 4
)
```
<br>
Install with:
```{r install-mekko-package, eval=FALSE}
install.packages("mekko")
```

You can also install the development version with 
`devtools::install_github('ryninho/mekko', build_vignettes = TRUE)`.

View examples with:
```{r open-vignette, eval=FALSE}
vignette("mekko-vignette")
```

First we'll load ggplot and mekko and create some example data.
```{r load-packages, warning = FALSE, message = FALSE}
library(ggplot2)
library(mekko)
```

```{r create-example-data}
profits <- data.frame(
  product = c('widgets', 'sprockets', 'cogs', 'whosits', 'whatsits'),
  profit_margin = c(3.2, -1.4, 0.1, -5.5, 11.9),
  revenue = c(850, 1600, 900, 675, 250)
  )
```

### Bar mekko

Now let's take a look at profit margin by product using ggplot2.

```{r current-state-bad-example-bar-mekko}
ggplot(profits, aes(x = product, y = profit_margin)) +
  geom_bar(stat = "identity")
```

Well that's insightful, but I don't know how worried I should be about the
margin on whosits or cogs, nor do I know how happy I should be about whatsits
knocking it out of the park. Maybe I can add revenue as the bar width so I know
what's important here?

```{r ggplot-bar-width-fail}
ggplot(profits, aes(x = product, y = profit_margin, width = revenue)) +
  geom_bar(stat = "identity") +
  labs(title = "Variable bar width fail :(")
```

Well shucks, that looks like some kind of Atari graphics airplane. Let's use the
mekko package to put our margins in context.

```{r bar-mekko-example}
bmx <- barmekko(profits, product, profit_margin, revenue)
bmx
```

Alright, so actually the weak profit margins on sprockets may be worth nearly as 
much focus as the problem with whosits. Also, no high-fives for margins on the 
whatsits until we triple sales of them.

Those labels are a little close together- this is a ggplot object so let's use
the usual method of rotating the axes.

```{r bar-mekko-extension}
bmx + theme(axis.text.x = element_text(angle = 90, hjust = 1))
```

### Appendix

* This package originally included a marimekko function as well, but the latter 
was discovered to be well covered in [other](https://CRAN.R-project.org/package=ggmosaic/vignettes/ggmosaic.html) [packages](https://www.semanticscholar.org/paper/Product-Plots-Wickham-Hofmann/0598a59354cb96161d68dab91fb0de21fb8671fd/figure/6), so it was removed to simplify maintenance
* While the barmekko is a ggplot object, your mileage may vary when attempting operations that normally would work on a ggplot object, as the data frame is deconstructed in the process of creating the graph