-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
145 lines (110 loc) · 3.82 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
---
output:
github_document
---
```{r, echo=FALSE}
suppressPackageStartupMessages(library(dplyr))
knitr::opts_chunk$set(fig.width=14)
```
<img src='https://cranlogs.r-pkg.org/badges/ggbrick'/><img src='https://cranlogs.r-pkg.org/badges/grand-total/ggbrick'/><img src='https://www.r-pkg.org/badges/version/ggbrick'/>
# ggbrick <img src='dev/images/ggbrick1.png' align="right" height="240" />
If you’re looking for something a little different, `ggbrick` creates a ‘waffle’ style chart with the aesthetic of a brick wall. The usage is similar to `geom_col` where you supply counts as the height of the bar and a fill for a stacked bar. Each whole brick represents 1 unit. Two half bricks equal one whole brick.
# Installation
Install from CRAN
```{r, eval=FALSE}
install.packages("ggbrick")
```
or from Git
```{r, eval=FALSE}
devtools::install_github("doehm/ggbrick")
```
# Geoms
There are two main geoms included:
1. `geom_brick()`: To make the brick wall-style waffle chart.
2. `geom_waffle()`: To make a regular-style waffle chart.
## geom_brick()
Use `geom_brick()` the same way you would use `geom_col()`.
```{r}
library(dplyr)
library(ggplot2)
library(ggbrick)
# basic usage
mpg |>
count(class, drv) |>
ggplot() +
geom_brick(aes(class, n, fill = drv)) +
coord_brick()
```
`coord_brick()` is included to maintain the aspect ratio of the bricks. It is similar to `coord_fixed()`, in fact, it is just a wrapper for `coord_fixed()` with a parameterised aspect ratio based on the number of bricks. The default number of bricks is 4. To change the width of the line outlining the brick use the linewidth parameter as normal.
To change specify the `bricks_per_layer` parameter in the geom and coord functions.
```{r}
mpg |>
count(class, drv) |>
ggplot() +
geom_brick(aes(class, n, fill = drv), bricks_per_layer = 6) +
coord_brick(6)
```
You can change the width of the columns similar to `geom_col()` to add more space between the bars. To maintain the aspect ratio you also need to set the width in `coord_brick()`.
```{r}
mpg |>
count(class, drv) |>
ggplot() +
geom_brick(aes(class, n, fill = drv), width = 0.5) +
coord_brick(width = 0.5)
```
To get more space between each brick use the `gap` parameter.
```{r}
mpg |>
count(class, drv) |>
ggplot() +
geom_brick(aes(class, n, fill = drv), gap = 0.04) +
coord_brick()
```
For no gap set `gap = 0` or use the shorthand `geom_brick0()`.
```{r}
mpg |>
count(class, drv) |>
ggplot() +
geom_brick0(aes(class, n, fill = drv)) +
coord_brick()
```
For fun, I’ve included a parameter to randomise the fill of the bricks or add a small amount of variation at the join between two groups. The proportions are maintained and designed to just give a different visual.
```{r}
mpg |>
count(class, drv) |>
ggplot() +
geom_brick(aes(class, n, fill = drv), type = "soft_random") +
coord_brick()
```
```{r}
mpg |>
count(class, drv) |>
ggplot() +
geom_brick(aes(class, n, fill = drv), type = "random") +
coord_brick()
```
## geom_waffle()
`geom_waffle()` has the same functionality as `geom_brick()` but the bricks are square giving a standard waffle chart. I added this so you can make a normal waffle chart in the same way you would use `geom_col()`. It requires `coord_waffle()`. To maintain the aspect ratio.
```{r}
mpg |>
count(class, drv) |>
ggplot() +
geom_waffle(aes(class, n, fill = drv)) +
coord_waffle()
```
```{r}
mpg |>
count(class, drv) |>
ggplot() +
geom_waffle0(aes(class, n, fill = drv), bricks_per_layer = 6) +
coord_waffle(6)
```
You may want to flip the coords when using `geom_waffle()`. To do so you’ll need to use `coord_flip()` and `theme(aspect.ratio = <number>)`.
```{r, fig.width=5, fig.height=7.5}
mpg |>
count(class, drv) |>
ggplot() +
geom_waffle0(aes(class, n, fill = drv)) +
coord_flip() +
theme(aspect.ratio = 1.8)
```