-
Notifications
You must be signed in to change notification settings - Fork 1
/
README.Rmd
254 lines (190 loc) · 6.2 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
---
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 = "90%",
fig.align = "center"
)
```
# figpatch
<!-- badges: start -->
[](https://lifecycle.r-lib.org/articles/stages.html#experimental)
[](https://CRAN.R-project.org/package=figpatch)
[](https://github.com/BradyAJohnston/figpatch/actions/workflows/R-CMD-check.yaml)
<!-- badges: end -->
The goal of figpatch is to create an easy way to incorporate external figures
and images into figures assembled with
[{patchwork}](https://patchwork.data-imaginist.com/).
## Installation
You can install the released version of figpatch from [CRAN](https://CRAN.R-project.org) with:
``` r
install.packages("figpatch")
```
Install the development version from [GitHub](https://github.com/) with:
``` r
# install.packages("devtools")
devtools::install_github("BradyAJohnston/figpatch")
```
## Example
```{r example}
library(figpatch)
library(ggplot2)
library(patchwork)
```
To use images inside of a patchwork object, they need to be converted to a
`{ggplot}` object via `fig()`. Once converted, you can assemble the
`{patchwork}` as you would otherwise with `+ / - * &` or `wrap_plots()`.
```{r figpatch}
image_path <- system.file(
"extdata",
"fig.png",
package = "figpatch",
mustWork = TRUE)
img <- fig(image_path)
plt <- ggplot(mtcars) +
aes(mpg, cyl) +
geom_point()
wrap_plots(img, plt, plt, img)
```
The `aspect.ratio` of the figs is set to the dimensions of the image, but the
plots can still resize as you would expect. For each plot that aligns with a
fig, it's dimensions will match that of the fig (as above). If however it only
aligns on one axis, then the other is free to resize to fill up the total image
space (as below).
```{r}
wrap_plots(plt, img, plt, img, ncol = 2)
```
If for some reason you want your fig to also resize (and thus distort your
image) then you can specify a particular `aspect.ratio` or let it _be free!_
```{r}
free_fig <- fig(image_path, aspect.ratio = "free")
wrap_plots(free_fig, plt, ncol = 1)
```
_Elegant._
### Tagging
Patchwork already provides support for easy tagging of sub-plots and sub-figures
using `plot_annotation()`.
```{r}
wrap_plots(img, plt, plt, img) +
plot_annotation(tag_levels = "A")
```
For a lot of figures that include images, tags should be placed on top of the images themselves.
Tagging in {patchwork} currently utilises the ggplot `tag` option
from `ggplot2::labs(tag = ...)` but which currently [doesn't support tagging inside plot borders.](https://github.com/tidyverse/ggplot2/issues/4297)
Lets see how it plays out.
#### The assembled figs
```{r fig.height=2, fig.width=7}
knitr::opts_chunk$set(fig.height = 2, fig.width = 7)
```
```{r fig.height=2, fig.width=7}
patchwork::wrap_plots(img, img, img, nrow = 1)
```
### Scaling the Figs
If multiple figs have differing dimensions, but but should be scaled the same, you can use `fig_scale()` to scale them all to the maximum width and height of all of the included figs.
#### Without scaling
```{r}
fl <- image_path <- system.file("extdata",
package = "figpatch",
mustWork = TRUE) %>%
list.files(pattern = "png",
full.names = TRUE)
fl %>%
lapply(fig) %>%
fig_wrap(ncol = 3)
```
#### With Scaling
```{r}
fl %>%
lapply(fig) %>%
fig_scale() %>%
fig_wrap(ncol = 3)
```
The scaling is based on the number of pixels, so while the two chemical structures are now properly scaled, the fig has been reduced significantly.
We can scale them independently to keep the third fig bigger.
```{r}
scaled_strctures <- lapply(fl[1:2], fig) %>%
fig_scale()
fig_wrap(c(scaled_strctures, list(fig(fl[3]))))
```
### {patchwork} tagging the figs
```{r}
patchwork::wrap_plots(img, img, img, nrow = 1) +
plot_annotation(tag_levels = "A")
```
### {figpatch} tagging the figs
To add internal tags to the figs, use the `fig_tag()` function. Assembling with
{patchwork} can continue as normal.
```{r}
img1 <- fig_tag(img, "A")
img2 <- fig_tag(img, "(B)")
img3 <- fig_tag(img, "misc")
patchwork::wrap_plots(img1, img2, img3, nrow = 1)
```
A number of default positions can be supplied to `fig_tag(pos = ...)` or a custom
vector which will place the text in `npc` coordinates (0 to 1 for both `x` and `y`) and
automatically adjust for the aspect ratio of the fig.
```{r}
img1 <- fig_tag(img, "A", pos = "topright")
img2 <- fig_tag(img, "(B)", pos = "bottomleft")
img3 <- fig_tag(img, "misc", pos = c(0.4, 0.9))
wrap_plots(img1, img2, img3, nrow = 1)
```
## `fig_wrap()`
To quickly label and wrap multiple figures, use `fig_wrap()`
To add borders around individual figures, use `b_*` options inside of `fig_wrap()`
or specify them individually with `fig()`.
```{r}
fig_wrap(
list(img, img, img),
"A",
prefix = "(",
suffix = ")",
b_col = "black"
)
```
Assembling lots of figures.
```{r}
knitr::opts_chunk$set(fig.height = 5, fig.width = 7)
```
```{r}
figs <- lapply(1:9, function(x) img)
fig_wrap(
figs,
nrow = 3,
tag = 1,
suffix = ")",
b_col = "gray20",
b_size = 2
)
```
Adjust the padding around plots with `b_margins` and change the unit used with
`b_unit`.
```{r}
fig_wrap(
figs,
nrow = 3,
tag = 1,
suffix = ")",
b_col = "gray20",
b_size = 2,
b_margin = ggplot2::margin(8, 8, 8, 8)
)
```
## Adding specific sub-plot text
You can add labels to the text using the `fig_lab()` function. Some
customisations are available. At the end of the day, a `fig()` is just a
`ggplot` object, and the labels are just the axis titles (x or y). You can add
your own `theme()` elements to customise further.
```{r warning=FALSE}
img1 <- fig_lab(img1, "Above is a fig.")
img2 <- fig_lab(img2, "This is an italic label.", fontface = "italic")
img3 <- fig_lab(img3, "Below is a fig.", pos = "top")
design <- "AB
CC"
wrap_plots(img1, img2, img3, design = design)
```