Make square pie charts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4.7 KiB

---
output: rmarkdown::github_document
---
```{r, include = FALSE}
knitr::opts_chunk$set(echo=TRUE, collapse=TRUE, message=FALSE, warning=FALSE, error=FALSE, fig.retina=2)
```
[![Build Status](https://travis-ci.org/hrbrmstr/waffle.svg)](https://travis-ci.org/hrbrmstr/waffle)
[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/waffle)](https://cran.r-project.org/package=waffle)
![downloads](http://cranlogs.r-pkg.org/badges/grand-total/waffle)

`waffle` is a package to make waffle charts (square pie charts)

It uses ggplot2 and returns a ggplot2 object.

The following functions are implemented:

- `waffle` : make a waffle chart ggplot2 object
- `iron` : vertically stitch together multiple waffle plots, left-aligning edges (best if used with the `waffle` `pad` parameter)

### Installation

```{r s1, eval=FALSE}
install.packages("waffle")
```

```{r s2, include=FALSE}
options(width=120)
```

### Usage

```{r s3}
library(waffle)

# current verison
packageVersion("waffle")

# basic example
parts <- c(80, 30, 20, 10)
```

```{r fig1, fig.width=6, fig.height=2.5}
waffle(parts, rows=8)
```

```{r s4}
# slightly more complex example
parts <- c(`Un-breached\nUS Population`=(318-11-79), `Premera`=11, `Anthem`=79)
```

```{r t1, eval=FALSE}
waffle(parts, rows=8, size=1, colors=c("#969696", "#1879bf", "#009bda"), legend_pos="bottom")
```

**Health records breaches as fraction of US Population**
```{r fig2, echo=FALSE, fig.width=6, fig.height=1.75}
waffle(parts, rows=8, size=1, colors=c("#969696", "#1879bf", "#009bda"), legend_pos="bottom")
```

<span style="font-size:8pt">One square == 1m ppl</span>

```{r t2, eval=FALSE}
waffle(parts/10, rows=3, colors=c("#969696", "#1879bf", "#009bda"))
```

**Health records breaches as fraction of US Population**
```{r fig3, echo=FALSE, fig.width=6, fig.height=2}
waffle(parts/10, rows=3, colors=c("#969696", "#1879bf", "#009bda"))
```

<span style="font-size:8pt">(One square == 10m ppl)</span>

```{r ww1, eval=FALSE}
library(extrafont)
waffle(parts/10, rows=3, colors=c("#969696", "#1879bf", "#009bda"),
use_glyph="medkit", size=8)
```

```{r ww2, echo=FALSE, fig.width=6, fig.height=2}
library(extrafont)
waffle(parts/10, rows=3, colors=c("#969696", "#1879bf", "#009bda"),
use_glyph="medkit", glyph_size=8)
```

```{r s5}
# replicating an old favourite

# http://graphics8.nytimes.com/images/2008/07/20/business/20debtgraphic.jpg
# http://www.nytimes.com/2008/07/20/business/20debt.html
savings <- c(`Mortgage\n($84,911)`=84911, `Auto and\ntuition loans\n($14,414)`=14414,
`Home equity loans\n($10,062)`=10062, `Credit Cards\n($8,565)`=8565)
```

```{r fig4, eval=FALSE}
waffle(savings/392, rows=7, size=0.5, legend_pos="bottom",
colors=c("#c7d4b6", "#a3aabd", "#a0d0de", "#97b5cf"))
```


**Average Household Savings Each Year**
```{r fig4a, echo=FALSE, fig.width=8, fig.height=2.5}
waffle(savings/392, rows=7, size=0.5, legend_pos="bottom",
colors=c("#c7d4b6", "#a3aabd", "#a0d0de", "#97b5cf"))
```

<span style="font-size:8pt">(1 square == $392)</span>

```{r s6}
# similar to but not exact

# https://eagereyes.org/techniques/square-pie-charts
professional <- c(`Male`=44, `Female (56%)`=56)
```

```{r t5, eval=FALSE}
waffle(professional, rows=10, size=0.5, colors=c("#af9139", "#544616"))
```

### Keeps factor by default levels now

With:

```{r fct, fig.height=3, fig.width=6}
iron(
waffle(c(thing1=0, thing2=100), rows=5),
waffle(c(thing1=25, thing2=75), rows=5)
)
```

Without (you can disable this via `keep` parameter now):

```{r no_fct, fig.height=3, fig.width=6}
iron(
waffle(c(thing1=0, thing2=100), rows=5, keep=FALSE),
waffle(c(thing1=25, thing2=75), rows=5, keep=FALSE)
)
```

**Professional Workforce Makeup**

```{r f5, echo=FALSE, fig.height=3, fig.width=4}
waffle(professional, rows=10, size=0.5, colors=c("#af9139", "#544616"))
```

Iron example (left-align & padding for multiple plots)

```{r f8, fig.height=6, fig.width=7}
pain.adult.1997 <- c(`YOY (406)`=406, `Adult (24)`=24)

A <- waffle(pain.adult.1997/2, rows=7, size=0.5,
colors=c("#c7d4b6", "#a3aabd"),
title="Paine Run Brook Trout Abundance (1997)",
xlab="1 square = 2 fish", pad=3)

pine.adult.1997 <- c(`YOY (221)`=221, `Adult (143)`=143)

B <- waffle(pine.adult.1997/2, rows=7, size=0.5,
colors=c("#c7d4b6", "#a3aabd"),
title="Piney River Brook Trout Abundance (1997)",
xlab="1 square = 2 fish", pad=8)

stan.adult.1997 <- c(`YOY (270)`=270, `Adult (197)`=197)

C <- waffle(stan.adult.1997/2, rows=7, size=0.5,
colors=c("#c7d4b6", "#a3aabd"),
title="Staunton River Trout Abundance (1997)",
xlab="1 square = 2 fish")

iron(A, B, C)
```

### Test Results

```{r}
library(waffle)
library(testthat)

date()

test_dir("tests/")
```