forked from r4bds/r4bds.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab03.qmd
336 lines (249 loc) · 10.1 KB
/
lab03.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
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
# Lab 3: Data Visualisation II {.unnumbered}
## Package(s)
- [ggplot2](https://ggplot2.tidyverse.org)
- [patchwork](https://patchwork.data-imaginist.com)
- [scales](https://scales.r-lib.org)
- [ggridges](https://wilkelab.org/ggridges/)
## Schedule
- 08.00 - 08.45: [Recap of Lab 2 and Lecture](https://raw.githack.com/r4bds/r4bds.github.io/main/lecture_lab03.html)
- 08.45 - 09.00: Break
- 09.00 - 12.00: [Exercises](#sec-exercises)
## Learning Materials
_Please prepare the following materials_
- Book: [R4DS2e: "Visualize", chapters 9, 10 and 11](https://r4ds.hadley.nz/visualize)
- Video: [William Chase | The Glamour of Graphics | RStudio (2020)](https://www.youtube.com/watch?v=h5cTacaWE6I)
- Web: [Patchwork - Getting started](https://patchwork.data-imaginist.com/articles/patchwork.html)
- Web: [Scales - Getting started](https://www.tidyverse.org/blog/2022/04/scales-1-2-0/)
_Unless explicitly stated, do not do the per-chapter exercises in the R4DS2e book_
## Learning Objectives
_A student who has met the objectives of the session will be able to:_
- Use more advanced ggplot features
- Customise the data visualisation
- Combine multiple plots into one pane
- Look at a more advanced ggplot and decipher the components used
## Exercises {#sec-exercises}
```{r}
#| message: false
#| warning: false
#| echo: false
#| eval: true
library("tidyverse")
library("ggridges")
```
_Read the steps of this exercises carefully, while completing them_
### Introduction
Some authors are kind enough to supply the data they used for their paper, e.g.,
- "Assessment of the influence of intrinsic environmental and geographical factors on the bacterial ecology of pit latrines" [@Torondel2016]
Where the supporting data can be found here:
- [http://userweb.eng.gla.ac.uk/umer.ijaz/bioinformatics/ecological.html](http://userweb.eng.gla.ac.uk/umer.ijaz/bioinformatics/ecological.html)
### Getting Started
Again, go to the [R for Bio Data Science RStudio Cloud Server](https://teaching.healthtech.dtu.dk/22100/rstudio.php) session from last time and login and choose the project you created
- **Create a new Quarto Document for today's exercises, e.g. lab03_exercises.qmd**
- **NB! The Quarto document MUST be placed together with your `.Rproj` file (defining, the project root - look in your `Files` tab) and also there, the `data`-folder should be placed!**
- **REMEMBER paths are important! Also, R is case-sensitive, i.e. "data" is not the same as "Data"**
See [Paths and Projects](paths_and_projects.qmd)
### Getting the data
Add a new code chunk and add the following code (Never mind the details, we will get back to this), remember you can use headers to nicely section your quarto Document.
```{r}
#| echo: true
#| eval: false
base_url <- "http://userweb.eng.gla.ac.uk/umer.ijaz/bioinformatics/ecological/"
SPE <- read_csv(file = str_c(base_url, "SPE_pitlatrine.csv"))
write_csv(x = SPE,
file = "data/SPE_pitlatrine.csv")
ENV <- read_csv(file = str_c(base_url, "ENV_pitlatrine.csv"))
write_csv(x = ENV,
file = "data/ENV_pitlatrine.csv")
```
Add the chunk settings `#| echo: true` and `#| eval: true`, then run the block and change the latter to `#| eval: false`.
- Discuss in your group, what this means and why we do it
<details>
<p><summary>Click here for hint</summary></p>
Where do we retrieve the data from, where do we write it to, and what happens if we run the chunk more than one time?
</details>
### Wrangling the data
- What is data wrangling?
Before we continue with plotting, we want to unify the data, so here again you will run some code, where the details are not important right now.
But... Make sure, that you have run `library("tidyverse")` somewhere in your Quarto document - Perhaps under an initial header saying "Load Libraries" or similar?
```{r}
#| echo: true
#| eval: false
SPE |>
pivot_longer(cols = -Taxa,
names_to = "Samples",
values_to = "OTU_Count") |>
full_join(ENV, by = "Samples") |>
mutate(site = case_when(str_detect(Samples, "^T") ~ "Tanzania",
str_detect(Samples, "^V") ~ "Vietnam")) |>
write_tsv(file = "data/SPE_ENV.tsv")
```
Change the chunk settings as before
## Data Visualisation II
### Read the data
```{r}
#| message: false
SPE_ENV <- read_tsv(file = "data/SPE_ENV.tsv")
SPE_ENV
```
### IMPORTANT INSTRUCTIONS - READ!
_For these exercises, you will have to identify what you see in the plot!_
For each plot, complete the following steps
1. Look at this overview of the components of a ggplot (see below)
1. Look at the plot you are to recreate and discuss in the group:
1. What is the data? Take a look at it and understand what is _in_ the data
1. What are the mappings? I.e. what variables are on the x-/y-axis?
1. Are there any colour-/fill-mappings?
1. What are the geoms used?
1. Are there any modifications to theme?
<details>
<p><summary>Click here for hint</summary></p>
- Consult the [Data visualization with ggplot2 cheatsheet](https://rstudio.github.io/cheatsheets/data-visualization.pdf)
- Check which options you have available
- Consult the chapters in the book you read, see preparation materials for labs 2 and 3
![](images/vis_ggplot_components.png)
</details>
### TASKS
#### Task 1 - Recreate the following plot
_Discuss in your group, which ggplot elements can you identify?_
```{r}
#| echo: false
ggplot(data = SPE_ENV,
mapping = aes(x = OTU_Count,
y = Taxa,
fill = site)) +
geom_boxplot()
```
#### Task 2 - Recreate the following plot
_Discuss in your group, which ggplot elements can you identify?_
```{r}
#| echo: false
ggplot(data = SPE_ENV,
mapping = aes(x = site,
y = Temp)) +
geom_boxplot()
```
#### Task 3 - Recreate the following plot
_Discuss in your group, which ggplot elements can you identify?_
```{r}
#| echo: false
ggplot(data = SPE_ENV,
mapping = aes(x = Temp,
y = pH,
colour = site)) +
geom_point()
```
#### Task 4 - Recreate the following plot
_Discuss in your group, which ggplot elements can you identify?_
```{r}
#| echo: false
ggplot(data = SPE_ENV,
mapping = aes(x = Temp,
y = pH,
colour = site,
label = Samples)) +
geom_label(size = 3)
```
#### Task 5 - Recreate the following plots
_Discuss in your group, which ggplot elements can you identify?_
```{r}
#| echo: false
ggplot(data = SPE_ENV,
mapping = aes(x = NH4,
fill = site)) +
geom_density()
```
```{r}
#| echo: false
#| warning: false
#| message: false
ggplot(data = SPE_ENV,
mapping = aes(x = NH4,
fill = site)) +
geom_density(alpha = 0.5) +
geom_hline(yintercept = 0) +
scale_x_log10() +
theme(panel.grid.minor.x = element_blank())
```
<details>
<p><summary>Click here for hint</summary></p>
Same data, but a transformation happened, changing the representation of the data. Look carefully at the axes.
</details>
#### Task 6 - Recreate the following plot
_Discuss in your group, which ggplot elements can you identify?_
```{r}
#| echo: false
#| message: false
ggplot(data = SPE_ENV,
mapping = aes(x = CODt,
y = CODs,
colour = site)) +
geom_point() +
geom_smooth(method = "lm")
```
<details>
<p><summary>Click here for hint</summary></p>
_See if you can find something online on `geom_smooth()`_
</details>
#### Task 7 - Recreate the following plot
_Discuss in your group, which ggplot elements can you identify?_
```{r}
#| echo: false
#| message: false
#| fig-align: center
#| fig-width: 7
#| fig-height: 7
ggplot(data = SPE_ENV,
mapping = aes(x = Taxa,
y = Samples,
fill = OTU_Count)) +
geom_tile() +
scale_fill_gradient2(midpoint = 10000,
low = "blue",
mid = "white",
high = "red") +
theme_classic(base_size = 8) +
theme(legend.position = "bottom",
axis.text.x = element_text(angle = 45,
hjust = 1))
```
<details>
<p><summary>Click here for hint</summary></p>
_Think about `fill` and then see if you can find something online on `geom_tile()`, `scale_fill_gradient2` and how to `ggplot rotate axis labels`_
</details>
#### Task 8 - Recreate the following plot
Start by running this code in a new chunk (ignore details for now)
```{r}
targets <- c("Methanobacteria", "Clostridia", "Actinobacteria",
"Sphingobacteria", "Anaerolineae")
SPE_ENV_targets <- SPE_ENV |>
filter(Taxa %in% targets)
```
and then use the created dataset `SPE_ENV_targets` to recreate this plot:
_Discuss in your group, which ggplot elements can you identify?_
```{r}
#| echo: false
#| message: false
ggplot(data = SPE_ENV_targets,
mapping = aes(x = OTU_Count,
y = Taxa,
fill = Taxa)) +
geom_density_ridges(alpha = 0.5) +
scale_fill_viridis_d() +
labs(x = "OTU Count",
y = "Taxa Identified",
title = "OTU Count Distribution for 5 Taxa Stratified by Site",
caption = "Data from doi.org/10.1111/1751-7915.12334") +
theme_minimal(base_family = "Avenir",
base_size = 12) +
theme(legend.position = "bottom") +
facet_wrap(facets = vars(site),
ncol = 2)
```
<details>
<p><summary>Click here for hint</summary></p>
_Here we need to use `geom_density_ridges()`, but which package contains this? Also, we are using a colour scale called `viridis`, but how do we add this? Also, perhaps there are more themes we can use than just `theme_classic()?`_
</details>
#### Task 9 - <span style="color: red;">GROUP ASSIGNMENT</span>
_For this assignment you and your group are to apply what you have learned in the two data visualisation labs. The task is to create a really nice plot using one of two datasets, the `cancer_data` or the `SPE_ENV`_
Try to play around with some custom colouring. There is a nice tool to aid in choosing colours for visualisations [here](https://www.sessions.edu/color-calculator/)
Be sure to read the [assignment instructions](assignments.qmd) before submitting your solution.