-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
190 lines (144 loc) · 6.07 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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
library(dplyr)
library(ggplot2)
library(patchwork)
library(facetious)
```
# facetious
<!-- badges: start -->

<!-- badges: end -->
`facetious` is home to some alternate facetting options for `ggplot2`.
## What's in the box
* `facet_wrap_strict()`
* respects nrow, ncol and inserts empty grobs to keep the resultant
plot at the given size
* Otherwise works like `ggplot2::facet_wrap()`
* `facet_grid_blank()`
* When a factor level doesn't exist, this facetting approach will insert
a totally empty object, instead of just an empty plot
* Otherwise works like `ggplot2::facet_grid()`
## Installation
You can install from [GitHub](https://github.com/coolbutuseless/facetious) with:
``` r
# install.package('remotes')
remotes::install_github('coolbutuseless/facetious')
```
## `facet_wrap_strict`
In `ggplot2::facet_wrap()` (regardless of the `nrow` and `ncol` specified by the user),
rows and columns of empty facets are removed.
This behaviour of `ggplot2` can make it difficult to size and align plots in multiple plots.
In contrast, `facetious::facet_wrap_strict()` will strictly adhere to the `nrow`, `ncol`
specified by the user, and retain blank rows/cols of facets.
```{r}
library(ggplot2)
library(patchwork)
library(facetious)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Standard ggplot2 facet_wrap()
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ggplot_facet_wrap <- ggplot(mtcars) +
geom_point(aes(mpg, wt)) +
facet_wrap(~cyl, nrow = 3, ncol = 2) +
labs(title = "facet_wrap(... nrow=3, ncol=2)")
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# facetious::facet_wrap_strict
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
facetious_facet_wrap_strict <- ggplot(mtcars) +
geom_point(aes(mpg, wt)) +
facet_wrap_strict(~cyl, nrow = 3, ncol = 2) +
labs(title = "facet_wrap_strict(... nrow=3, ncol=2)")
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Use 'patchwork' to stitch plots side-by-side
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ggplot_facet_wrap + facetious_facet_wrap_strict
```
## `facet_grid_blank`
By default when a particular factor level is empty, `ggplot2` includes an empty
plot.
`facetious::facet_grid_blank()` goes a step further and (by default) makes the
empty facet entirely *blank*.
That is, empty factor levels are represented as a `grid::nullGrob()`, but it is
possible to specify another grob, or list of grobs to fill the empty facets.
v0.1.1 of `facetious` borrows an idea from the really great hack provided in
[ggbillboard](https://github.com/nacnudus/ggbillboard) by [Duncan Garmonsway](https://twitter.com/nacnudus) - i.e.
don't just put empty grobs into empty facets, put some advertising in there!
```{r}
library(dplyr)
library(ggplot2)
library(patchwork)
library(facetious)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Make some data with some empty factors
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
plot_df <- mtcars %>%
mutate(
cyl = as.factor(cyl),
am = as.factor(am)
) %>%
filter(!(cyl == 4 & am == 1)) %>%
filter(!(cyl == 8 & am == 0))
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Standard ggplot facet_grid()
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ggplot_facet_grid <- ggplot(plot_df) +
geom_point(aes(mpg, wt)) +
facet_grid(rows = vars(cyl), cols = vars(am), drop = FALSE,
labeller = label_both) +
labs(title = "ggplot2::facet_grid()")
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# facetious::facet_grid_blank()
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
facetious_facet_grid_blank <- ggplot(plot_df) +
geom_point(aes(mpg, wt)) +
facet_grid_blank(rows = vars(cyl), cols = vars(am), drop = FALSE,
labeller = label_both) +
labs(title = "facetious::facet_grid_blank()")
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Use 'patchwork' to stitch plots side-by-side
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ggplot_facet_grid + facetious_facet_grid_blank
```
# Using alternate globs for blank facets. Example 1 - single grob
```{r}
grob_for_blank <- grid::textGrob("No data available")
ggplot(plot_df) +
geom_point(aes(mpg, wt)) +
facet_grid_blank(rows = vars(cyl), cols = vars(am), drop = FALSE,
labeller = label_both, blank = grob_for_blank) +
labs(title = "facetious::facet_grid_blank()")
```
# Using alternate globs for blank facets. Example 2 - multiple grobs
```{r fig.height = 8}
grobs_for_blank <- list(
grid::rasterGrob(jpeg::readJPEG("man/figures/distracted.jpg")),
grid::rasterGrob(jpeg::readJPEG("man/figures/run.jpg"))
)
ggplot(plot_df) +
geom_point(aes(mpg, wt)) +
facet_grid_blank(rows = vars(cyl), cols = vars(am), drop = FALSE,
labeller = label_both, blank = grobs_for_blank) +
labs(title = "facetious::facet_grid_blank()")
```
## Related Software
* [ggplot2](https://cran.r-project.org/package=ggplot2)
* [ggforce](https://cran.r-project.org/package=ggforce) a great package with alternate facets and
tools for ggplot2
* [ggbillboard](https://github.com/nacnudus/ggbillboard) a package for replacing
blank facets with other grobs. This was so cool that I've incorporated the
idea into `facetious` as part of the facet process, rather than as a post-processing
step.
## Acknowledgements
* R Core for developing and maintaining the language.
* CRAN maintainers, for patiently shepherding packages onto CRAN and maintaining
the repository