-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1.3_Data_Visualization.qmd
616 lines (459 loc) · 17.7 KB
/
1.3_Data_Visualization.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
---
title: "1.3 Data Visualization"
subtitle: |
| Workshop: "Handling Uncertainty in your Data"
|
| Dr. Mario Reutter & Juli Nagel
| (slides adapted from Dr. Lea Hildebrandt)
format:
revealjs:
smaller: true
scrollable: true
slide-number: true
theme: serif
chalkboard: true
width: 1280
height: 720
from: markdown+emoji
---
# Data Viz
```{css}
code.sourceCode {
font-size: 1.4em;
}
div.cell-output-stdout {
font-size: 1.4em;
}
```
Why should we visualize our data?
::: incremental
- check whether data make sense (unusual observations?)
- honestly and comprehensively present the data
- (visually) check whether data fits the assumptions of statistical tests
:::
. . .
It's fun! (And plots are probably the most important information in papers!)
## ggplot
We will use a package called `ggplot2` (part of the `tidyverse`). `ggplot2` is a very versatile package that allows us to make beautiful, publication-ready(-ish) figures.
`ggplot2` follows the ["grammar of graphics" by Leland Wilkinson](https://ggplot2-book.org/mastery), a formal guide to visualization principles. A core feature are the *layers* each plot consists of. The main function to "start" plotting is `ggplot()` - we will then add layers of data and layers to tweak the appearance.
![Layers of a ggplot](images/ggplotlayers.PNG){fig-alt="Depiction of how a plot is built up from different layers in ggplot2" width="502"}
## Activity 1: Set Up
1. Within your precision workshop project in RStudio, create a new script called `DataVisualisation1.R`.
2. Make sure you have the following two files downloaded into your project folder (we already used them in Intro to `R` presentation): [`ahi-cesd.csv`](https://psyteachr.github.io/quant-fun-v2/ahi-cesd.csv) and [`participant-info.csv`](https://psyteachr.github.io/quant-fun-v2/participant-info.csv).
3. Copy and run the code below to load the `tidyverse` package and the data files:
```{r}
#| echo: true
#| eval: false
#|
library(tidyverse)
dat <- read_csv("ahi-cesd.csv")
pinfo <- read_csv("participant-info.csv")
```
```{r}
#| echo: false
#| eval: true
#| message: false
library(tidyverse)
dat <- read_csv("Data/ahi-cesd.csv")
pinfo <- read_csv("Data/participant-info.csv")
```
## Activity 1: Set Up
4. Run the following code to combine both files and select our variables of interest:
```{r}
#| echo: true
all_dat <-
dat %>%
inner_join(
pinfo, # combine dat with pinfo
by = c("id", "intervention") # common variables that tell R which data belongs together
) %>%
arrange(id, occasion) #joining messes up the order of the data frame => arrange again
# we throw out several variables even though they would be important for a comprehensive data analysis
summarydata <-
all_dat %>%
select(
id, ahiTotal, cesdTotal, # ID & questionnaire scores
sex, age, educ, income # demographic variables
)
```
::: notes
what happens in the code chunk?
:::
## Look at the Data
Have a look at the types of data:
```{r}
#| echo: true
glimpse(summarydata)
```
What do you see?
. . .
All variables are loaded as numeric. However, are all of those numeric?
. . .
`sex`, `educ` and `income` don't seem to really be numbers but **factors** with individual categories (factor **levels**)! \
We should convert these data to factor. Checking and adjusting data types (as part of data wrangling) will be important for plotting and analyzing the data, you might otherwise get strange/wrong results!
## Activity 2: Transform to factor
Copy and run the below code to change the categories to factors.
```{r}
#| echo: true
summarydata1 <-
summarydata %>%
mutate(
sex = as_factor(sex),
educ = as_factor(educ),
income = as_factor(income)
)
```
. . .
- If you mutate a new column with the same name as the old one, it will overwrite the column.
. . .
```{r}
#| echo: true
glimpse(summarydata1)
```
## Activity 2: Transform to factor
```{r}
#| echo: true
glimpse(summarydata1)
```
. . .
```{r}
#| echo: true
summarydata1 %>% pull(educ) %>% unique()
```
. . .
- At first glance, the data look the same. But the 1s and 2s in `sex` are now e.g. not treated as numbers anymore, but as factor levels (categories). This e.g. changes how the data behave in different analyses, or plotting (continuous vs. categorical data).
## Set labels
A simple change to a factor is not always helpful. We still don't know what a `1` in `sex` or a `5` in `educ` stands for:
- `sex`: 1 = *female*, 2 = *male*
- `educ`: 1 = *no graduation*, 2 = *school graduation*, 3 = *vocational training*, 4 = *bachelor's degree*, 5 = *post graduate*
- `income`: 1 = *low*, 2 = *middle*, 3 = *high*
:::aside
There is very sparse information on the variables at <https://doi.org/10.5334/jopd.35>, so I guesstimated some of the factor levels.
:::
## Set labels
`match_case()` allows us to label our numeric data with more human-readable descriptions. `if_else()` is a useful shorthand for cases where we only have two categories.
```{r}
#| echo: true
summarydata2 <-
summarydata %>%
mutate(
sex = if_else(sex == 1, "female", "male") %>% as_factor(),
educ = educ %>%
case_match(
1 ~ "no graduation",
2 ~ "school graduation",
3 ~ "vocational training",
4 ~ "bachelor's degree",
5 ~ "post grad"
) %>%
as_factor(), # need to transform to factor in the end
income = income %>%
case_match(1 ~ "low", 2 ~ "middle", 3 ~ "high") %>%
as_factor()
)
```
## Set labels
```{r}
#| echo: true
summarydata2 %>% pull(educ) %>% unique()
```
Factor is now ordered by occurrence in data! :( (E.g., the order on the axis of a plot would be wrong.)
## Set factor order (levels)
Using `factor()`, we can explicitly order the categories using the argument `levels`.
```{r}
#| echo: true
summarydata %>%
mutate(
income = income %>%
case_match(1 ~ "low", 2 ~ "middle", 3 ~ "high") %>%
as_factor()
) %>%
pull(income) %>% unique()
```
. . .
```{r}
#| echo: true
summarydata %>%
mutate(
income = income %>%
case_match(1 ~ "low", 2 ~ "middle", 3 ~ "high") %>%
factor(levels = c("low", "middle", "high")) # factor(), not as_factor()!
) %>%
pull(income) %>% unique()
```
# Activity 3: Barplot
A bar plot is a plot that shows counts of categorical data (factors), where the height of each bar represents the count of that variable.
We will plot male and female participants.
## The First Layer
- The first line (or layer) sets up the base of the graph: the data to use and the aesthetics (`aes()`) (what will go on the x and y axis, how the plot will be grouped).
- `aes()` includes anything that is *directly* related to your data: e.g., what goes on the x and y axis, or whether the plot should be *grouped* by a variable in your data.
- We can provide `x` and `y` as arguments, however, in a bar plot, the count per category is calculated automatically, so we don't need to put anything on the y-axis ourselves.
```{r}
#| output-location: column-fragment
#| echo: true
ggplot(summarydata1, aes(x = sex))
```
## The Second Layer
The next layer adds a **geom** or a shape. In this case we use `geom_bar()` as we want to draw a bar plot.
- Note that we are adding layers, using a [`+`](https://rdrr.io/r/base/Arithmetic.html) between layers. This is a very important difference between pipes and visualization.
```{r}
#| echo: true
#| output-location: column-fragment
#| code-line-numbers: "2"
ggplot(summarydata1, aes(x = sex)) +
geom_bar()
```
## The Second Layer with color
- Adding `fill` to the first layer will separate the data into each level of the grouping variable and fill it with a different color. <span style="font-size: 16px;">Note that `fill` colors the inside of the bar, while `colour` colors the bar's outlines.</span>
- We can get rid of the (in this case redundant legend) with `show.legend = FALSE`.
```{r}
#| echo: true
#| output-location: column-fragment
#| code-line-numbers: "1"
ggplot(summarydata2, aes(x = sex, fill = sex)) +
geom_bar() #geom_bar(show.legend = FALSE)
```
## The Next Layers - Improving the Plot
We might want to make the plot a bit prettier and easier to read. What would you improve?
. . .
We could add better axis labels, and custom colors. We can do so with the functions `scale_x_discrete()` and `scale_y_continuous()`, which adjust the x and y axes.
Both functions can change several aspects of our axes; here, we use the argument `name` to set a new axis name.
```{r}
#| echo: true
#| output-location: column-fragment
#| code-line-numbers: "3,4|5"
ggplot(summarydata2, aes(x = sex, fill = sex)) +
geom_bar(show.legend = FALSE) +
scale_x_discrete(name = "Participant Sex") +
scale_y_continuous(name = "Number of participants")
```
::: notes
There's also the counterparts `scale_x_continuous()` and `scale_y_discrete()`. What do you think, why do we use the ones mentioned above and when would we use the other ones?
:::
## Themes: Changing the Appearance
There are a number of built-in themes that you can use to change the appearance (background, whether axes are shown etc.), but you can also tweak the themes further manually.
We will now change the default theme to `theme_minimal()`, but you can also try other themes (just type "theme\_" and see what the autocomplete brings up).
```{r}
#| echo: true
#| output-location: column-fragment
#| code-line-numbers: "5"
ggplot(summarydata2, aes(x = sex, fill = sex)) +
geom_bar(show.legend = FALSE) +
scale_x_discrete(name = "Participant Sex") +
scale_y_continuous(name = "Number of participants") +
theme_minimal()
```
## Colors
There are various ways to change the colors of the bars. You can manually indicate the colors you want to use but you can also easily use pre-determined color palettes that are already checked for color-blind friendliness.
A popular palette is **viridis**. We can simply add a function/layer to your ggplot named `scale_fill_viridis_d()` (d for discrete). The function has an *option* parameter that takes 5 different values (A - E).
- Run the code below. Try changing the option to either A, B, C or D and see which one you like!
```{r}
#| echo: true
#| output-location: column-fragment
#| code-line-numbers: "6"
ggplot(summarydata2, aes(x = sex, fill = sex)) +
geom_bar(show.legend = FALSE) +
scale_x_discrete(name = "Participant Sex") +
scale_y_continuous(name = "Number of participants") +
theme_minimal() +
scale_fill_viridis_d(option = "E")
```
## Transparency
You can also add transparency to your plot, which can be helpful if you plot several layers of data that overlap.
To do so, you can simply add `alpha` to the `geom_bar()` - try changing the value of `alpha` (between 0 and 1):
```{r}
#| echo: true
#| output-location: column-fragment
#| code-line-numbers: "3"
ggplot(summarydata2, aes(x = sex, fill = sex)) +
geom_bar(show.legend = FALSE,
alpha = .8) +
scale_x_discrete(name = "Participant Sex") +
scale_y_continuous(name = "Number of participants") +
theme_minimal() +
scale_fill_viridis_d(option = "E")
```
## Grouped Plots
Imagine that you have several *factors* that you want to use to group your data, such as gender and income. In this case, you could use a **grouped bar plot**:
```{r}
#| echo: true
#| output-location: column-fragment
#| code-line-numbers: "1|4"
ggplot(summarydata2, aes(x = sex, fill = income)) +
geom_bar(
# the default are stacked bars; we use "dodge" to put them side by side
position = "dodge",
alpha = .8
) +
scale_x_discrete(name = "Participant Sex") +
scale_y_continuous(name = "Number of participants") +
theme_minimal() +
scale_fill_viridis_d(option = "E")
```
:::notes
Without `position = dodge`, you would get a stacked barplot
:::
## Facetting
You could also use **facets** to divide your data visualizations into several subplots: `facet_wrap` for one variable.
```{r}
#| echo: true
#| code-line-numbers: "7"
ggplot(summarydata2, aes(x = sex, fill = sex)) +
geom_bar(show.legend = FALSE, alpha = .8) +
scale_x_discrete(name = "Participant Sex") +
scale_y_continuous(name = "Number of participants") +
theme_minimal() +
scale_fill_viridis_d(option = "E") +
facet_wrap(vars(income)) # here, you need to use vars() around variable names
```
:::notes
What is problematic here? We needed percentages for females and males for a better comparison
:::
## Facetting 2
You could also use **facets** to divide your data visualizations into several subplots: `facet_grid` for a matrix of (combinations of) two variables.
```{r}
#| echo: true
#| code-line-numbers: "7-11"
ggplot(summarydata2, aes(x = sex, fill = sex)) +
geom_bar(show.legend = FALSE, alpha = .8) +
scale_x_discrete(name = "Participant Sex") +
scale_y_continuous(name = "Number of participants") +
theme_minimal() +
scale_fill_viridis_d(option = "E") +
facet_grid(
rows = vars(income),
cols = vars(educ),
labeller = "label_both" # this adds the variable name into the facet legends
)
```
## A closer look
```{r}
ggplot(summarydata2, aes(x = sex, fill = sex)) +
geom_bar(show.legend = FALSE, alpha = .8) +
scale_x_discrete(name = "Participant Sex") +
scale_y_continuous(name = "Number of participants") +
theme_minimal() +
scale_fill_viridis_d(option = "E") +
facet_grid(
rows = vars(income),
cols = vars(educ),
labeller = "label_both" # this adds the variable name into the facet legends
)
```
# Activity 4: The Violin-Boxplot
There are a number of different figure types you can plot with the different geoms, e.g. `geom_point()`, `geom_histogram()`, `geom_line()`...
We now want to plot a form of a boxplot that becomes more popular: The violin-boxplot (which combines, i.e. overlays, the violin plot with a boxplot).
## Violin-Boxplot
Let's look at the code. How does the code differ from the one for the barplot above?
```{r}
#| echo: true
#| output-location: column-fragment
#| code-line-numbers: "|2|4|6"
ggplot(summarydata1, aes(x = income,
y = ahiTotal, # new variable!
fill = income)) +
geom_violin(trim = FALSE, # smooth on edges
alpha = .4) +
geom_boxplot(width = .2, # small boxplot contained in violin
alpha = .7) +
scale_x_discrete(
name = "Income",
# set new labels
labels = c("Below Average", "Average", "Above Average")) +
scale_y_continuous(name = "Authentic Happiness Inventory Score")+
theme_minimal() +
# no need to switch of axis for every geom individually
theme(legend.position = "none") +
scale_fill_viridis_d()
```
::: notes
In this case, not the **count** on the y-axis, but another cont. variable!
:::
## Layer Order
The order of layers is crucial, as the plot will be built up in that order (later layers on **top**):
::: columns
::: column
```{r}
#| echo: true
ggplot(summarydata1, aes(x = income, y = ahiTotal)) +
geom_violin() +
geom_boxplot()
```
:::
::: column
```{r}
#| echo: true
ggplot(summarydata1, aes(x = income, y = ahiTotal)) +
geom_boxplot() +
geom_violin()
```
:::
:::
## Scatterplot
If we have continuous data of two variables, we often want to make a **scatter plot**:
```{r}
#| echo: true
#| output-location: column-fragment
#| code-line-numbers: "1|2|5"
ggplot(summarydata1, aes(x = age, y = cesdTotal)) +
geom_point() +
# if you don't want the shaded CI, add se = FALSE to this
geom_smooth(method = lm)
```
## Saving your Figures
You can use `ggsave()` to save your plots. If you don't tell `ggsave()` which plot you want to save, by default it will save **the last plot you created**.
You just have to enter the name of the file to be saved (in your working directory) like this:
```{r}
#| echo: true
#| eval: false
ggsave("violin-boxplot.png")
```
Check whether indeed the last plot was saved!
. . .
You can also specify the dimensions of your plot to be saved:
```{r}
#| echo: true
#| eval: false
ggsave("violin-boxplot.png",
width = 6.5, #width of a typical page in inches minus border (according to APA format)
height = 6.5 / sqrt(2), #golden ratio :)
units = "in")
```
or
```{r}
#| echo: true
#| eval: false
ggsave("violin-boxplot.png",
width = 1920,
height = 1080,
units = "px") #full HD picture in pixels: 1920 x 1080
```
## Saving your Figures 2
You can also assign the plot to a variable in your environment and then tell `ggsave()` which object to save. This is a bit safer.
Run the code for the violin-boxplot again and save the plot in an object called `viobox`. You'd then have to explicitly tell `ggsave()` to save the object `viobox`:
```{r}
#| echo: true
#| eval: false
#| code-line-numbers: "1,20"
viobox <-
ggplot(summarydata1, aes(x = income, y = ahiTotal, fill = income)) +
geom_violin(trim = FALSE, alpha = .4) +
geom_boxplot(width = .2, alpha = .7) +
scale_x_discrete(
name = "Income",
labels = c("Below Average", "Average", "Above Average")) +
scale_y_continuous(name = "Authentic Happiness Inventory Score")+
theme_minimal() +
theme(legend.position = "none") +
scale_fill_viridis_d()
ggsave("violin-boxplot-stored.png", plot = viobox)
```
::: notes
Do not add `ggsave()` to the plot with a `+`. Instead run it on a separate line!
If plot is assigned to object, it won't be displayed unless you type `viobox` in the console!
:::
# Thanks!
Check out [Chapter 13 of QuantFun](https://psyteachr.github.io/quant-fun-v2/visualisation.html) for further exercises and tips for data visualization!
The free book [R for data science](https://r4ds.had.co.nz/) was written by (some of) the creators of the `tidyverse`, and includes lots of plotting and data science exercises. (For more advanced plotting, check the [ggplot2 book](https://ggplot2-book.org/).)
Also keep in mind: Coding is a lot of googling things!
You can also check out the [R Graph Gallery](https://r-graph-gallery.com/) for code for different types of plots.