forked from hrbrmstr/waffle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
369 lines (292 loc) · 9.44 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
---
output: rmarkdown::github_document
---
```{r, include = FALSE}
knitr::opts_chunk$set(echo = TRUE, collapse = TRUE, message = FALSE, warning = FALSE, error = FALSE, cache = TRUE, fig.retina = 2)
```
[![Build Status](https://travis-ci.org/hrbrmstr/waffle.svg)](https://travis-ci.org/hrbrmstr/waffle)
[![CRAN_Status_Badge](https://www.r-pkg.org/badges/version/waffle)](https://cran.r-project.org/package=waffle)
![downloads](https://cranlogs.r-pkg.org/badges/grand-total/waffle)
# waffle
Create Waffle Chart Visualizations
## Description
Square pie charts (a.k.a. waffle charts) can be used to communicate parts of a whole for categorical quantities. To emulate the percentage view of a pie chart, a 10x10 grid should be used with each square representing 1% of the total. Modern uses of waffle charts do not necessarily adhere to this rule and can be created with a grid of any rectangular shape. Best practices suggest keeping the number of categories small, just as should be done when creating pie charts. Tools are provided to create waffle charts as well as stitch them together, and to use glyphs for making isotype pictograms.
It uses ggplot2 and returns a ggplot2 object.
## What's Inside the Tin
The following functions are implemented:
- `waffle`: Make waffle (square pie) charts
```{r ingredients, results='asis', echo=FALSE, cache=FALSE}
hrbrpkghelpr::describe_ingredients()
```
## Installation
```{r nstall-ex, results='asis', echo = FALSE, cache=FALSE}
hrbrpkghelpr::install_block()
```
```{r s2, include=FALSE}
options(width = 120)
```
## Usage
```{r s3, cache=FALSE}
library(waffle)
library(magrittr)
library(hrbrthemes)
library(ggplot2)
library(dplyr)
library(waffle)
# current verison
packageVersion("waffle")
```
### Some new bits up first
```{r fig.width=8, fig.height=6}
data.frame(
parts = factor(rep(month.abb[1:3], 3), levels=month.abb[1:3]),
vals = c(10, 20, 30, 6, 14, 40, 30, 20, 10),
col = rep(c("blue", "black", "red"), 3),
fct = c(rep("Thing 1", 3),
rep("Thing 2", 3),
rep("Thing 3", 3))
) -> xdf
xdf %>%
count(parts, wt = vals) %>%
ggplot(aes(fill = parts, values = n)) +
geom_waffle(n_rows = 20, size = 0.33, colour = "white", flip = TRUE) +
scale_fill_manual(
name = NULL,
values = c("#a40000", "#c68958", "#ae6056"),
labels = c("Fruit", "Sammich", "Pizza")
) +
coord_equal() +
theme_ipsum_rc(grid="") +
theme_enhance_waffle()
xdf %>%
count(parts, wt = vals) %>%
ggplot(aes(label = parts, values = n)) +
geom_pictogram(n_rows = 10, aes(colour = parts), flip = TRUE, make_proportional = TRUE) +
scale_color_manual(
name = NULL,
values = c("#a40000", "#c68958", "#ae6056"),
labels = c("Fruit", "Sammich", "Pizza")
) +
scale_label_pictogram(
name = NULL,
values = c("apple-alt", "bread-slice", "pizza-slice"),
labels = c("Fruit", "Sammich", "Pizza")
) +
coord_equal() +
theme_ipsum_rc(grid="") +
theme_enhance_waffle() +
theme(legend.key.height = unit(2.25, "line")) +
theme(legend.text = element_text(size = 10, hjust = 0, vjust = 0.75))
xdf %>%
count(parts, wt = vals) %>%
ggplot(aes(label = parts, values = n)) +
geom_pictogram(
n_rows = 20, size = 6, aes(colour = parts), flip = TRUE,
family = "FontAwesome5Brands-Regular"
) +
scale_color_manual(
name = NULL,
values = c("#073f9c", "black", "#f34323"),
labels = c("BitBucket", "GitHub", "Other")
) +
scale_label_pictogram(
name = NULL,
values = c("bitbucket", "github", "git-alt"),
labels = c("BitBucket", "GitHub", "Other")
) +
coord_equal() +
theme_ipsum_rc(grid="") +
theme_enhance_waffle() +
theme(legend.text = element_text(hjust = 0, vjust = 1))
```
### Geoms!
```{r geoms, fig.width=6, fig.height=8, fig.retina=2}
library(hrbrthemes)
library(waffle)
library(tidyverse)
tibble(
parts = factor(rep(month.abb[1:3], 3), levels=month.abb[1:3]),
values = c(10, 20, 30, 6, 14, 40, 30, 20, 10),
fct = c(rep("Thing 1", 3), rep("Thing 2", 3), rep("Thing 3", 3))
) -> xdf
ggplot(xdf, aes(fill=parts, values=values)) +
geom_waffle(color = "white", size=1.125, n_rows = 6) +
facet_wrap(~fct, ncol=1) +
scale_x_discrete(expand=c(0,0)) +
scale_y_discrete(expand=c(0,0)) +
ggthemes::scale_fill_tableau(name=NULL) +
coord_equal() +
labs(
title = "Faceted Waffle Geoms"
) +
theme_ipsum_rc(grid="") +
theme_enhance_waffle()
```
### Waffle Bar Charts with scales!
```{r waffle-bars, fig.retina=2}
library(dplyr)
library(waffle)
storms %>%
filter(year >= 2010) %>%
count(year, status) -> storms_df
ggplot(storms_df, aes(fill = status, values = n)) +
geom_waffle(color = "white", size = .25, n_rows = 10, flip = TRUE) +
facet_wrap(~year, nrow = 1, strip.position = "bottom") +
scale_x_discrete() +
scale_y_continuous(labels = function(x) x * 10, # make this multiplyer the same as n_rows
expand = c(0,0)) +
ggthemes::scale_fill_tableau(name=NULL) +
coord_equal() +
labs(
title = "Faceted Waffle Bar Chart",
subtitle = "{dplyr} storms data",
x = "Year",
y = "Count"
) +
theme_minimal(base_family = "Roboto Condensed") +
theme(panel.grid = element_blank(), axis.ticks.y = element_line()) +
guides(fill = guide_legend(reverse = TRUE))
```
### Basic example
```{r fig0, fig.width=6, fig.height=2.5}
parts <- c(80, 30, 20, 10)
waffle(parts, rows = 8)
```
### Use a data frame
```{r fig1, fig.width=6, fig.height=2.5}
parts <- data.frame(
names = LETTERS[1:4],
vals = c(80, 30, 20, 10)
)
waffle(parts, rows = 8)
```
### Slightly more complex example
```{r s4}
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, legend_pos = "bottom",
colors = c("#969696", "#1879bf", "#009bda")
)
```
<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 medkit0, eval=FALSE}
library(extrafont)
waffle(
parts / 10, rows = 3, colors = c("#969696", "#1879bf", "#009bda"),
use_glyph = "medkit", size = 8
) + expand_limits(y = c(0, 4))
```
```{r medkit, 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
) + expand_limits(y = c(0, 4))
```
### Replicating an old favourite
![](https://graphics8.nytimes.com/images/2008/07/20/business/20debtgraphic.jpg)
Via: <https://www.nytimes.com/2008/07/20/business/20debt.html>
```{r s5}
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>
### More replication
Similar to <https://eagereyes.org/techniques/square-pie-charts>
```{r s6}
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)
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
) -> A
pine.adult.1997 <- c(`YOY (221)` = 221, `Adult (143)` = 143)
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
) -> B
stan.adult.1997 <- c(`YOY (270)` = 270, `Adult (197)` = 197)
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"
) -> C
iron(A, B, C)
```
## Package Code Metrics
```{r}
cloc::cloc_pkg_md()
```
## Code of Conduct
Please note that this project is released with a [Contributor Code of Conduct](CONDUCT.md). By participating in this project you agree to abide by its terms.