-
Notifications
You must be signed in to change notification settings - Fork 5
/
lecture_lab02.qmd
306 lines (228 loc) · 10.2 KB
/
lecture_lab02.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
---
title: "Lecture Lab 2"
author: "Leon Eyrich Jessen"
format:
revealjs:
embed-resources: true
theme: moon
slide-number: c/t
width: 1600
height: 900
mainfont: avenir
logo: images/r4bds_logo_small.png
footer: "R for Bio Data Science"
---
# Data Visualisation I
```{r}
library("ggplot2")
library("tibble")
library("dplyr")
```
<!--# ---------------------------------------------------------------------- -->
<!--# SLIDE ---------------------------------------------------------------- -->
<!--# ---------------------------------------------------------------------- -->
## First: Make sure you're on track!
### Lab I Learning Objectives
- Navigate the RStudio IDE and master the very basics of R
- Create, edit and run a basic Quarto document
- Explain why reproducible data analysis is important
- Describe the components of a reproducible data analysis
### Where should you be by now?
- In a group
- On Slack
- On the RStudio Cloud Server
- On GitHub
*If any of this is not the case, see the `Getting Started` section on the course site*
<!--# ---------------------------------------------------------------------- -->
<!--# SLIDE ---------------------------------------------------------------- -->
<!--# ---------------------------------------------------------------------- -->
## What is Data Visualisation?
:::: {.columns}
::: {.column width="50%"}
- Think about Hadley's and Hans' lectures, the book chapter and paper you read for today
- Use the next 5 minutes to think and talk to the person next to you
- Then, write your thoughts on Slack in the channel for Teaching lab 2
:::
::: {.column width="50%"}
![](images/what_is_data_viz.png)
:::
::::
<!--# ---------------------------------------------------------------------- -->
<!--# SLIDE ---------------------------------------------------------------- -->
<!--# ---------------------------------------------------------------------- -->
## On Data Visualisation
- Depending on where your read, something along the lines of:
- *The graphical representation of information and data*
- *Uses visual elements like charts, graphs and maps*
- *Provide an accessible way to see and understand trends, outliers and patterns*
- Data visualisation is your means to summarise and communicate key messages
- Within research
- Within industry
- It is not easy!
- Anyone can make a plot, but an impactful data visualisation requires true skills
<!--# ---------------------------------------------------------------------- -->
<!--# SLIDE ---------------------------------------------------------------- -->
<!--# ---------------------------------------------------------------------- -->
## What is wrong here?
```{r}
#| echo: false
#| fig-align: center
tibble(performance = c(0.82, 0.84),
method = c("Someone else", "me")) |>
ggplot(aes(x = method, y = performance)) +
geom_col() +
coord_cartesian(ylim = c(0.80, 0.85))
```
<!--# ---------------------------------------------------------------------- -->
<!--# SLIDE ---------------------------------------------------------------- -->
<!--# ---------------------------------------------------------------------- -->
## Should have been
```{r}
#| echo: false
#| fig-align: center
tibble(performance = c(0.82, 0.84),
method = c("Someone else", "me")) |>
ggplot(aes(x = method, y = performance)) +
geom_col() +
geom_hline(yintercept = 0) +
coord_cartesian(ylim = c(0, 1))
```
<!--# ---------------------------------------------------------------------- -->
<!--# SLIDE ---------------------------------------------------------------- -->
<!--# ---------------------------------------------------------------------- -->
## What's wrong here?
```{r}
#| echo: false
#| fig-align: center
set.seed(541880)
d = tibble(value = c(rnorm(20, 10, 2),
rnorm(20, 8, 2)),
time = c(rep("t1", 20), rep("t2", 20)))
d |>
group_by(time) |>
summarise(mu = mean(value), sigma = sd(value)) |>
ggplot(aes(x = time, y = mu)) +
geom_col() +
geom_errorbar(aes(ymin = mu - sigma,
ymax = mu + sigma), width = 0.1)
```
<!--# ---------------------------------------------------------------------- -->
<!--# SLIDE ---------------------------------------------------------------- -->
<!--# ---------------------------------------------------------------------- -->
## Should have been
```{r}
#| echo: false
#| fig-align: center
d |>
ggplot(aes(x = time, y = value)) +
geom_boxplot()
```
<!--# ---------------------------------------------------------------------- -->
<!--# SLIDE ---------------------------------------------------------------- -->
<!--# ---------------------------------------------------------------------- -->
## Or maybe even
```{r}
#| echo: false
#| fig-align: center
d |>
ggplot(aes(x = time, y = value)) +
geom_violin(scale = "width") +
geom_boxplot(width = 0.2)
```
<!--# ---------------------------------------------------------------------- -->
<!--# SLIDE ---------------------------------------------------------------- -->
<!--# ---------------------------------------------------------------------- -->
## What is this?
![](images/bad_viz_01.png){fig-align="center" width="80%"}
<!--# ---------------------------------------------------------------------- -->
<!--# SLIDE ---------------------------------------------------------------- -->
<!--# ---------------------------------------------------------------------- -->
## Or this?
![](images/bad_viz_02.png){fig-align="center" width="80%"}
<!--# ---------------------------------------------------------------------- -->
<!--# SLIDE ---------------------------------------------------------------- -->
<!--# ---------------------------------------------------------------------- -->
## Or this?
![](images/bad_viz_03.png){fig-align="center" width="80%"}
<!--# ---------------------------------------------------------------------- -->
<!--# SLIDE ---------------------------------------------------------------- -->
<!--# ---------------------------------------------------------------------- -->
## Or this?
![](images/bad_viz_04.png){fig-align="center" width="80%"}
<!--# ---------------------------------------------------------------------- -->
<!--# SLIDE ---------------------------------------------------------------- -->
<!--# ---------------------------------------------------------------------- -->
## Or this?
![](images/bad_viz_05.png){fig-align="center" width="80%"}
<!--# ---------------------------------------------------------------------- -->
<!--# SLIDE ---------------------------------------------------------------- -->
<!--# ---------------------------------------------------------------------- -->
## Or this?
![](images/bad_viz_06.png){fig-align="center" width="80%"}
<!--# ---------------------------------------------------------------------- -->
<!--# SLIDE ---------------------------------------------------------------- -->
<!--# ---------------------------------------------------------------------- -->
## Or this?
![](images/bad_viz_07.png){fig-align="center" width="80%"}
<!--# ---------------------------------------------------------------------- -->
<!--# SLIDE ---------------------------------------------------------------- -->
<!--# ---------------------------------------------------------------------- -->
## ...and then there is this one?!?
![](images/bad_viz_17.jpeg){fig-align="center" width="80%"}
<!--# ---------------------------------------------------------------------- -->
<!--# SLIDE ---------------------------------------------------------------- -->
<!--# ---------------------------------------------------------------------- -->
## ...and then there is this one?!?
![](images/bad_viz_17_mod.jpeg){fig-align="center" width="80%"}
<!--# ---------------------------------------------------------------------- -->
<!--# SLIDE ---------------------------------------------------------------- -->
<!--# ---------------------------------------------------------------------- -->
## Or this?
![](images/bad_viz_08.png){fig-align="center" width="80%"}
- Don't even know where to begin with this one...
<!--# ---------------------------------------------------------------------- -->
<!--# SLIDE ---------------------------------------------------------------- -->
<!--# ---------------------------------------------------------------------- -->
## In Summary on Data Visualisation
- Think carefully about *exactly* what it is, you want to communicate with your visualisation
- Remove redundant information
- Be honest and show your data
- Less is more, do not cram 3 plots into 1
- Do not make a fancy, but information deprived plot
- Think about colour choice - Separation, but also quite a few people are colour blind
- Today is meant as in intro, data visualisation will be an integrated part of the rest of the course
<!--# ---------------------------------------------------------------------- -->
<!--# SLIDE ---------------------------------------------------------------- -->
<!--# ---------------------------------------------------------------------- -->
## ggplot, the Grammar of Graphics
### The basic syntax of ggplot
- In a new code chunk in your Quarto document, input and run:
```{{r}}
ggplot(data = my_data,
mapping = aes(x = v1, y = v2)) +
geom_point()
```
- Define your data
- Map variables in your plot to your visualisation
- Choose a graphical representation
- Let us look at that in another way
<!--# ---------------------------------------------------------------------- -->
<!--# SLIDE ---------------------------------------------------------------- -->
<!--# ---------------------------------------------------------------------- -->
## ggplot, the Grammar of Graphics
![](images/vis_ggplot_components.png){fig-align="center" width="80%"}
<!--# ---------------------------------------------------------------------- -->
<!--# SLIDE ---------------------------------------------------------------- -->
<!--# ---------------------------------------------------------------------- -->
## Example - Scatter plot
```{r}
#| echo: true
#| fig-align: center
#| fig-height: 3
library("tidyverse")
ggplot(data = datasets::Puromycin,
mapping = aes(x = conc,
y = rate)) +
geom_point()
```
# Break and the it is time for exercises...