-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5.3-Complex_operations.qmd
executable file
·225 lines (172 loc) · 5.19 KB
/
5.3-Complex_operations.qmd
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
---
title: "Complex plot operations"
subtitle: "Here are some strange plot things we have done before"
author:
- Elizabeth King
- Kevin Middleton
format:
revealjs:
theme: [default, custom.scss]
standalone: true
self-contained: true
logo: QMLS_Logo.png
slide-number: true
show-slide-number: all
link-external-newwindow: true
---
## Complex plot operations
```{r}
#| label: setup
#| echo: false
#| warning: false
#| message: false
library(tidyverse)
library(cowplot)
ggplot2::theme_set(theme_cowplot())
library(patchwork)
library(lemon)
```
When at all possible:
- Generate figures with code
- Even photos/microscopy/gel panels (`annotation_raster()`)
- Use version tracking (git)
- Avoid post-export editing with Illustrator, Canvas, Inkscape, etc.
Reproducible and ultimately time-saving.
## Images in a grid
```{r}
#| echo: true
#| output-location: slide
library(jpeg)
img <- readJPEG(system.file("img", "Rlogo.jpg", package="jpeg"))
P <- map(.x = 1:4,
.f = function(ii) {
ggplot() +
annotation_raster(img,
xmin = 0, xmax = dim(img)[2],
ymin = 0, ymax = dim(img)[1]) +
xlim(c(0, dim(img)[2])) +
ylim(c(0, dim(img)[1])) +
coord_equal() +
ggpubr::theme_transparent()
})
wrap_plots(P) +
plot_annotation(tag_levels = 'a', tag_suffix = ".")
```
## Resizing plots in a grid
- `patchwork` automatically handles axis alignment
- `cowplot::plot_grid()` gives fine control over relative plot size
## Data to work with
```{r}
#| echo: true
#| output-location: slide
set.seed(87534579)
M <- tibble(x1 = runif(20),
x2 = runif(20),
y1 = rnorm(20),
y2 = rnorm(20))
p1 <- ggplot(M, aes(x1, y1)) + geom_point()
p2 <- ggplot(M, aes(x2, y1)) + geom_point()
p3 <- ggplot(M, aes(x1, y2)) + geom_point()
p4 <- ggplot(M, aes(x2, y2)) + geom_point()
plot_grid(p1, p2, p3, p4)
```
## Removing axes
```{r}
#| echo: true
#| output-location: slide
p1 <- ggplot(M, aes(x1, y1)) + geom_point() +
theme(axis.title.x = element_blank(), axis.text.x = element_blank())
p2 <- ggplot(M, aes(x2, y1)) + geom_point() +
theme(axis.title = element_blank(), axis.text = element_blank())
p3 <- ggplot(M, aes(x1, y2)) + geom_point()
p4 <- ggplot(M, aes(x2, y2)) + geom_point() +
theme(axis.title.y = element_blank(), axis.text.y = element_blank())
plot_grid(p1, p2, p3, p4)
```
## Aligning axes
```{r}
#| echo: true
#| output-location: slide
plot_grid(p1, p2, p3, p4,
align = "hv")
```
## Resizing sublots
```{r}
#| echo: true
#| output-location: slide
plot_grid(p1, p2, p3, p4,
rel_widths = c(1.05, 1),
rel_heights = c(1, 1.10))
```
## Moving the legend around
- `theme(legend.position = ...)` for basic positioning
- `lemon` package for [more options](https://cran.r-project.org/web/packages/lemon/vignettes/legends.html)
## Legend positioning 1
```{r}
#| echo: true
#| output-location: slide
p <- ggplot(palmerpenguins::penguins |>
drop_na(bill_length_mm, bill_depth_mm),
aes(bill_length_mm, bill_depth_mm, color = species)) +
geom_point() +
facet_wrap("species", nrow = 2)
p +
theme(legend.position = "inside",
legend.position.inside = c(0.7, 0.1))
```
## Legend positioning with `reposition_legend`
- Can span multiple grid positions
```{r}
#| echo: true
#| output-location: slide
reposition_legend(p, "center", panel = "panel-2-2")
```
## Legend positioning 2
- Extracting the legend and adding manually with `grid.arrange()`
```{r}
#| echo: true
#| output-location: slide
p1 <- ggplot(M, aes(x1, y1, color = y1)) + geom_point()
p2 <- ggplot(M, aes(x2, y1, color = y1)) + geom_point()
p3 <- ggplot(M, aes(x1, y2, color = y1)) + geom_point()
library(gridExtra)
legend <- g_legend(p1 + theme(legend.position = "bottom"))
grid.arrange(p1 + theme(legend.position = "hidden"),
p2 + theme(legend.position = "hidden"),
p3 + theme(legend.position = "hidden"),
legend)
```
## Customizing the legend elements
- `guides()` and `guide_legend()`
- Control fonts, position, layout of legend elements
```{r}
bp <- Data4Ecologists::beepollen |>
mutate(Bee_type = if_else(queen == 0, "Worker", "Queen")) |>
rename(Prop_removed = removed,
Duration = duration) |>
select(-queen) |>
as_tibble()
```
```{r}
#| echo: true
#| output-location: slide
ggplot(bp, aes(x = Duration, y = Prop_removed, color = Bee_type)) +
geom_path(linewidth = 3, alpha = 0.5, show.legend = FALSE) +
geom_point(size = 10, alpha = 0.5) +
guides(color = guide_legend(override.aes = list(size = 3,
alpha = 1)))
```
## Customizing facets
```{r}
#| echo: true
#| output-location: slide
PP <- palmerpenguins::penguins |>
mutate(Penguin_label = paste0(species, "\npenguin"))
ggplot(PP |> drop_na(bill_length_mm, bill_depth_mm),
aes(bill_length_mm, bill_depth_mm, color = species)) +
geom_point(show.legend = FALSE) +
facet_grid(. ~ Penguin_label) +
cowplot::theme_cowplot(font_size = 10) +
theme(strip.background = element_blank(),
strip.text = element_text(face = "bold", size = rel(1.5)))
```