-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03-Describe-visualize.Rmd
1460 lines (1382 loc) · 35 KB
/
03-Describe-visualize.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
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Descriptives and visualizations
## Overview
In this section, I describe and visualize the variables.
We got variables on the person-level and on the wave level.
**Person-level**
* Age and gender (`age`, `gender`)
* What region of the UK participants are from (`region`)
* Whether they've used a medium in the three months before the first wave (variables starting with `filter`)
* To what extent they identify with a medium (variables ending with `identity`)
**Wave-level**
* How much they used a medium per wave (variables ending with `time`)
* Their well-being per wave (`well_being`)
## Person-level
Let's have a look at the final sample.
Overall, our sample size is N = `r working_file %>% group_by(id) %>% slice(1) %>% ungroup() %>% nrow()`.
Participants, on average, were *M* = `r round(working_file %>% group_by(id) %>% pull(age) %>% mean, digits = 1)` years old, *SD* = `r round(working_file %>% group_by(id) %>% pull(age) %>% sd, digits = 1)`, see Figure \@ref(fig:visualize-age).
The gender distribution is pretty equal (`r working_file %>% group_by(id) %>% slice(1) %>% ungroup() %>% filter(gender == "Female") %>% nrow()` women (`r round(working_file %>% group_by(id) %>% slice(1) %>% filter(gender == "Female") %>% nrow() / length(unique(working_file$id)), digits = 4) * 100`%), `r working_file %>% group_by(id) %>% slice(1) %>% ungroup() %>% filter(gender == "Male") %>% nrow()` men, and `r working_file %>% group_by(id) %>% slice(1) %>% ungroup() %>% filter(gender == "Other") %>% nrow()` non-binary participants).
```{r visualize-age, echo=FALSE, fig.cap="Distribution of age"}
age <-
describe(
working_file,
"age",
trait = TRUE
)
single_cloud(
working_file,
age,
"age",
"#009E73",
title = NULL,
trait = TRUE
)
```
Alright, next let's have a look again (now that we have the final sample) at how many people had used a medium in the three months before wave 1 (Table \@ref(tab:table-media-filters)).
```{r table-media-filters, echo=FALSE}
working_file %>%
filter(wave == 1) %>%
select(id, wave, starts_with("filter")) %>%
pivot_longer(
cols = c(-id, -wave),
names_to = "Medium",
values_to = "Used"
) %>%
mutate(
Medium = str_remove(Medium, "filter_")
) %>%
group_by(Medium) %>%
summarise(
Used = sum(Used),
"Not Used" = n() - Used,
Proportion = round(Used / n(), digits = 3) * 100
) %>%
knitr::kable(
.,
caption = "Frequency of how much a medium was used at wave 1"
)
```
Let's inspect the sample characteristics (age and gender) by how many surveys a person filled out (Table \@ref(tab:describe-per-wave)).
So we're just looking at whether there are pronounced differences in sample charateristics as people dropped out of the study.
Looks pretty stable to me, especially given the samller sample sizes for those who dropped out early.
```{r describe-per-wave, echo=FALSE}
temp %>%
group_by(id) %>%
mutate(Waves = n()) %>%
slice(1) %>%
ungroup() %>%
group_by(Waves) %>%
summarise(
N = n(),
`Mean age` = mean(age, na.rm = TRUE),
`SD age` = sd(age, na.rm = TRUE),
`Women` = sum(gender=="Female"),
Men = sum(gender=="Male"),
Other = sum(gender=="Other"),
`Proportion women (%)` = round(Women / n(), digits = 2) * 100
) %>%
knitr::kable(
.,
caption = "Demographics by number of filled out surveys"
)
# remove tmp file
rm(tmp)
```
Now let's have a look at how much people identified with each medium.
Note that people weren't asked those identity questions if they indicated that they hadn't used a medium at wave 1.
```{r describe-identities, echo=FALSE}
# custom function, see setting up page
identity_descriptives <-
bind_rows(
describe(
working_file,
"music_identity",
trait = TRUE
),
describe(
working_file,
"tv_identity",
trait = TRUE
),
describe(
working_file,
"films_identity",
trait = TRUE
),
describe(
working_file,
"games_identity",
trait = TRUE
),
describe(
working_file,
"e_publishing_identity",
trait = TRUE
)
) %>%
mutate(
across(
mean:cihigh,
~ round(.x, digits = 2)
)
)
knitr::kable(
identity_descriptives,
caption = "Descriptive information for identity scales"
)
```
Figure \@ref(fig:identity-distributions) shows the distributions of those identity variables.
Apparently gamers identified more with games than for example music listeners identified with music.
```{r identity-distributions, echo=FALSE, fig.cap="Distribution of identity variables"}
# plot_grid is from cowplot
plot_grid(
single_cloud(
working_file,
identity_descriptives,
"music_identity",
"#999999",
title = "Music",
trait = TRUE
),
single_cloud(
working_file,
identity_descriptives,
"tv_identity",
"#E69F00",
title = "TV",
trait = TRUE
),
single_cloud(
working_file,
identity_descriptives,
"films_identity",
"#56B4E9",
title = "Films",
trait = TRUE
),
single_cloud(
working_file,
identity_descriptives,
"games_identity",
"#009E73",
title = "Games",
trait = TRUE
),
single_cloud(
working_file,
identity_descriptives,
"e_publishing_identity",
"#F0E442",
title = "E-publishing/books",
trait = TRUE
),
ncol = 2
)
```
## Wave-level
Let's inspect the descriptive information for the different media.
Table \@ref(tab:describe-time-with-zeros) shows that people used TV, music, and films the most, but spent little time on magazines and audio books.
```{r describe-time-with-zeros, echo=FALSE}
times_descriptives_with_zeros <-
bind_rows(
describe(
working_file,
"music_time",
trait = FALSE
),
describe(
working_file,
"tv_time",
trait = FALSE
),
describe(
working_file,
"films_time",
trait = FALSE
),
describe(
working_file,
"games_time",
trait = FALSE
),
describe(
working_file,
"books_time",
trait = FALSE
),
describe(
working_file,
"magazines_time",
trait = FALSE
),
describe(
working_file,
"audiobooks_time",
trait = FALSE
)
) %>%
mutate(
across(
mean:cihigh,
~ round(.x, digits = 2)
)
)
knitr::kable(
times_descriptives_with_zeros,
caption = "Descriptive information for use time (including zero estimates)"
)
```
Figure \@ref(fig:time-distributions-with-zeros) shows the distributions of those time variables.
Note that most distributions are zero-inflated with a heavy skew.
```{r time-distributions-with-zeros, echo=FALSE, fig.cap="Distribution of time variables (with zeros)"}
plot_grid(
single_cloud(
working_file,
times_descriptives_with_zeros,
"music_time",
"#999999",
title = "Music",
trait = FALSE
),
single_cloud(
working_file,
times_descriptives_with_zeros,
"tv_time",
"#E69F00",
title = "TV",
trait = FALSE
),
single_cloud(
working_file,
times_descriptives_with_zeros,
"films_time",
"#56B4E9",
title = "Films",
trait = FALSE
),
single_cloud(
working_file,
times_descriptives_with_zeros,
"games_time",
"#009E73",
title = "Games",
trait = FALSE
),
single_cloud(
working_file,
times_descriptives_with_zeros,
"books_time",
"#F0E442",
title = "E-publishing/books",
trait = FALSE
),
single_cloud(
working_file,
times_descriptives_with_zeros,
"magazines_time",
"#0072B2",
title = "Magazines",
trait = FALSE
),
single_cloud(
working_file,
times_descriptives_with_zeros,
"audiobooks_time",
"#D55E00",
title = "Audiobooks",
trait = FALSE
),
ncol = 2
)
```
Let's also inspect those summary statistics and distributions without zeros.
Table \@ref(tab:describe-time-without-zeros) shows the same pattern was with zeros, but overall higher means.
Interestingly, those who listen to audiobooks report to do so for a long time per day.
```{r describe-time-without-zeros, echo=FALSE}
# temp file without zeros
temp <-
working_file %>%
mutate(
across(
ends_with("time"),
~ na_if(.x, 0)
)
)
times_descriptives_without_zeros <-
bind_rows(
describe(
temp,
"music_time",
trait = FALSE
),
describe(
temp,
"tv_time",
trait = FALSE
),
describe(
temp,
"films_time",
trait = FALSE
),
describe(
temp,
"games_time",
trait = FALSE
),
describe(
temp,
"books_time",
trait = FALSE
),
describe(
temp,
"magazines_time",
trait = FALSE
),
describe(
temp,
"audiobooks_time",
trait = FALSE
)
) %>%
mutate(
across(
mean:cihigh,
~ round(.x, digits = 2)
)
)
knitr::kable(
times_descriptives_without_zeros,
caption = "Descriptive information for use time (excluding zero estimates)"
)
```
Figure \@ref(fig:time-distributions-without-zeros) shows a less skewed distribution once we remove zeros.
```{r time-distributions-without-zeros, echo=FALSE, fig.cap="Distribution of time variables (without zeros)"}
plot_grid(
single_cloud(
temp,
times_descriptives_without_zeros,
"music_time",
"#999999",
title = "Music",
trait = FALSE
),
single_cloud(
temp,
times_descriptives_without_zeros,
"tv_time",
"#E69F00",
title = "TV",
trait = FALSE
),
single_cloud(
temp,
times_descriptives_without_zeros,
"films_time",
"#56B4E9",
title = "Films",
trait = FALSE
),
single_cloud(
temp,
times_descriptives_without_zeros,
"games_time",
"#009E73",
title = "Games",
trait = FALSE
),
single_cloud(
temp,
times_descriptives_without_zeros,
"books_time",
"#F0E442",
title = "E-publishing/books",
trait = FALSE
),
single_cloud(
temp,
times_descriptives_without_zeros,
"magazines_time",
"#0072B2",
title = "Magazines",
trait = FALSE
),
single_cloud(
temp,
times_descriptives_without_zeros,
"audiobooks_time",
"#D55E00",
title = "Audiobooks",
trait = FALSE
),
ncol = 2
)
# remove temp file
rm(temp)
```
As a next step, let's see how use developed over time.
Figure \@ref(fig:use-over-time) shows that most media stay stable, except maybe an overall spike in wave 2.
```{r use-over-time, echo=FALSE, fig.cap="Average use over time per medium"}
# get summary stats with CI per wave in the long format
time_per_wave <-
working_file %>%
group_by(wave) %>%
summarise(
across(
c(ends_with("time")),
list(
mean = ~ mean(.x, na.rm = TRUE),
cilow = ~Rmisc::CI(na.omit(.x))[[3]], # lower CI
cihigh = ~Rmisc::CI(na.omit(.x))[[1]] # upper CI
)
)
) %>%
pivot_longer(
-wave,
names_to = "Medium",
values_to = "value"
) %>%
extract(Medium, into = c("Medium", "metric"), "(.*)_([^_]+$)") %>% # separate by last occurrence of underscore
pivot_wider(
names_from = "metric",
values_from = "value"
) %>%
mutate(
Medium = str_remove(Medium, "_time"),
Medium = tools::toTitleCase(Medium) # capitalize first letter
)
ggplot(
time_per_wave,
aes(
x = as.factor(wave),
y = mean,
color = Medium,
group = Medium
)
) +
geom_line(size = 1) +
geom_errorbar(
aes(
ymin = cilow,
ymax = cihigh,
width = 0
)
) +
scale_color_brewer(palette="Dark2") +
xlab("Wave") +
ylab("Average use per day (in h)")
```
Last, let's see the overall descriptives of the well-being variables.
Table \@ref(tab:describe-well-being) shows that both life satisfaction and affect were above the mid-point of the scale - even though for affect, it's a close call.
```{r describe-well-being, echo = FALSE}
well_being <-
bind_rows(
describe(
working_file,
"affect",
trait = FALSE
),
describe(
working_file,
"life_satisfaction",
trait = FALSE
)
)
knitr::kable(
well_being %>%
mutate(
across(
mean:cihigh,
~ round(.x, digits = 2)
)
),
caption = "Well-being descriptives"
)
```
Let's plot the distribution of both affect and life satisfaction in Figure \@ref(fig:well-being-distribution).
```{r well-being-distribution, echo=FALSE, fig.cap="Distribution of well-being across waves and participants"}
plot_grid(
single_cloud(
working_file,
well_being,
"affect",
"#009E73",
title = "Affect",
trait = FALSE
),
single_cloud(
working_file,
well_being,
"life_satisfaction",
"#E69F00",
title = "Life satisfaction",
trait = FALSE
)
)
```
Last, let's look at how well-being develops over time.
Figure \@ref(fig:well-being-over-time) shows that life satisfaction appears stable: note the y-axis is on decimal points (original range: 0 to 10).
In contrast, affect has significantly decreased, mostly because anxiety has decreased (not shown on graph).
```{r well-being-over-time, echo=FALSE, fig.cap="Average estimates over time per medium"}
# get summary stats with CI per wave in the long format
well_being_per_wave <-
working_file %>%
group_by(wave) %>%
summarise(
across(
c(affect, life_satisfaction),
list(
mean = ~ mean(.x, na.rm = TRUE),
cilow = ~Rmisc::CI(na.omit(.x))[[3]], # lower CI
cihigh = ~Rmisc::CI(na.omit(.x))[[1]] # upper CI
)
)
) %>%
pivot_longer(
-wave,
names_to = c("Variable", "Metric"),
values_to = "Value",
names_pattern = "(.*)_([^_]+$)" # life_satisfaction_mean has two underscores
) %>%
pivot_wider(
names_from = "Metric",
values_from = "Value"
)
ggplot(
well_being_per_wave,
aes(
x = as.factor(wave),
y = mean,
color = Variable,
group = Variable
)
) +
geom_line(
size = 1
) +
geom_errorbar(
aes(
ymin = cilow,
ymax = cihigh,
width = 0
)
) +
scale_color_brewer(palette="Dark2") +
xlab("Wave") +
ylab("Average well-being")
```
## Correlation matrices
Let's have a look at the row correlations between the media use variables and well-being.
Figure \@ref(fig:correlation-matrix-use-well-being) shows that a) most media use is correlated, and b) there are negative, but very small correlations between well-being and media use.
The significance is negligible here just because the sample is so large.
That said, we haven't done any grouping yet, so the correlation matrix treats all observations as independent, which is clearly not the case.
```{r correlation-matrix-use-well-being, echo=FALSE, cache=TRUE, message=F, warning=F, fig.cap="Correlation matrix of hours spent with a medium and well-being",fig.dim=c(14,14)}
ggpairs(
data = working_file %>%
select(
contains("time"),
affect,
life_satisfaction
) %>%
# remove _time appendix to make variable names shorter
rename_with(
~ str_remove(.x, "_time"),
ends_with("time"),
),
lower = list(
continuous = lm_function # custom helper function
),
diag = list(
continuous = dens_function # custom helper function
)
) +
theme(
axis.line=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank(),
axis.title.y=element_blank(),
axis.title.x=element_blank(),
legend.position="none",
panel.background=element_blank(),
panel.border=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
plot.background=element_blank(),
strip.background = element_blank()
)
```
At the request of a reviewer, we can also look at correlations per age group.
We leave a formal analysis up to readers, who can estimate models with an interaction term.
Here, I merely plot the slope between total media time and well-being by age groups.
In Figure \@ref(fig:relation-by-age), we see that there are differences in age on well-being, but not in slopes.
```{r relation-by-age, echo=FALSE, warning=FALSE, message=FALSE, fig.cap="Relation between total time and well-being by age"}
working_file %>%
mutate(
age_group = case_when(
age < 30 ~ "16-29",
age >= 30 ~ "30+"
)
) %>%
select(id, age_group, total_time, affect, life_satisfaction) %>%
pivot_longer(
.,
cols = c("affect", "life_satisfaction"),
names_to = "Measure",
values_to = "score"
) %>%
ggplot(
.,
aes(
x = total_time,
y = score,
color = age_group
)
) +
ylim(0, 11) +
geom_smooth(method = "lm") +
scale_colour_manual(values=cb_palette) +
scale_fill_manual(values = cb_palette) +
facet_wrap(~ Measure) +
theme_cowplot()
```
The reviewer was also interested in different lags.
However, to see each correlation at each wave with each lag for each medium would be 6 (lags medium) x 6 (lags well-being) x 2 (well-being measures) x 7 (media types) = `r 6*6*2*7` correlations.
Therefore, Figure \@ref(fig:correlations-per-lag) shows the correlations of total time with all media with affect at the last wave for total time at each wave.
We see little differences in the slopes, meaning that a different lag for the analysis would probably not change the results much.
However, we invite readers to run the analysis themselves with a different lag.
```{r correlations-per-lag, echo=FALSE, warning=FALSE, message=FALSE, fig.cap="Correlation between affect at the last wave and total time at each wave"}
working_file %>%
select(id, wave, total_time, affect) %>%
pivot_wider(
id_cols = id,
names_from = wave,
values_from = total_time,
names_glue = "total_time_{wave}"
) %>%
# add affect at wave 6
left_join(
.,
working_file %>%
filter(wave==6) %>%
select(id, affect)
) %>%
pivot_longer(
cols = c(-id, -affect),
names_to = "lag",
values_to = 'total_time'
) %>%
mutate(lag = parse_number(lag)) %>%
ggplot(
.,
aes(
x = total_time,
y = affect
)
) +
geom_smooth(method = "lm", color = "black") +
facet_wrap(~ lag) +
scale_colour_manual(values=cb_palette) +
scale_fill_manual(values = cb_palette) +
theme_cowplot() +
xlab("Total time at each wave") +
ylab("Affect at the last wave")
```
## Plots for Paper
For the paper, I want to avoid confronting readers with blocks of numbers.
Therefore, I'll try to put as much info as possible into figures rather than tables or in-text reports of numbers.
I think the following plots are necessary for the paper:
1. A plot of the response rate per wave, simply as a descriptive measure.
2. A plot showcasing the exclusions. I'll describe the exclusion criteria in the paper, but don't want to overwhelm readers with numbers for each.
3. A plot showing the distributions, M, and SD of media use for different categories.
4. Same plot, but for well-being.
5. A plot of the results.
First, a plot for the response rate for each wave.
We already showed response rates as a table in the processing section, so we can re-use the `completion_table` object for a plot.
Not sure about the colors yet, might go all black for this one.
```{r response-rate-plot}
ggplot(
data = completion_table %>%
rename(
Wave = Waves,
`Participants per wave (%)` = `Participants per wave`
) %>%
mutate(
Wave = as.factor(Wave)
),
aes(
x = Wave,
y = `Participants per wave (%)`#,
# color = Wave,
# fill = Wave
)
) +
geom_segment(
aes(
xend = Wave,
y = 0,
yend = `Participants per wave (%)`
),
size = 1
) +
geom_point(
size = 2
) +
geom_text(
aes(
label = paste0(`Participants per wave (%)`, "(", `Frequency per wave`, "%)"),
y = `Participants per wave (%)` + 160
)
) +
scale_colour_manual(values=cb_palette) +
scale_fill_manual(values = cb_palette) +
theme(
legend.position = "none"
) -> figure1
figure1
# create figure folder
dir.create("figures/", FALSE, TRUE)
ggsave(
filename = here("figures", "figure1.png"),
plot = figure1,
width = 21 * 0.8,
height = 29.7 * 0.4,
units = "cm",
dpi = 300
)
```
Next, I plot the exclusions.
There's two ways to go about this.
Either I show the absolute number of participants and observations that we exclude with each criterion in relation to the total sample size.
I do that below.
Note that getting those exclusions in a nice format requires some wrangling.
I already created the `exclusion_plot_data` object in the processing section.
Here, I turn it into the long format first.
```{r exclusions-plot1}
# to long format for plotting
exclusion_plot_data <-
exclusion_plot_data %>%
filter(!str_detect(Exclusion, "Total")) %>% # don't include the total numbers for now
rename(Participants = PPs) %>% # nicer name
pivot_longer(
Participants:Observations,
names_to = "Measure",
values_to = "Value"
) %>%
mutate( # create new variable that's stable across exclusions
Total = case_when(
Measure == "Participants" ~ exclusion_plot_data %>% filter(Type == "Total") %>% pull(PPs),
TRUE ~ exclusion_plot_data %>% filter(Type == "Total") %>% pull(Observations),
),
`Total Before` = case_when(
Measure == "Participants" ~ exclusion_plot_data %>% filter(Type == "Total Before") %>% pull(PPs),
TRUE ~ exclusion_plot_data %>% filter(Type == "Total Before") %>% pull(Observations),
)
) %>%
mutate(
across(
Type:Measure,
as.factor
)
)
# https://stackoverflow.com/questions/11889625/annotating-text-on-individual-facet-in-ggplot2
# I want to show the total numbers once, not for each facet/criterion combination, which is why I create a data frame that only has a matching value for the positions in the plot that I want
label_positions <-
tibble(
Type = rep("Wave-level", 2),
Exclusion = c(3, 3), # on the right, where wave-level exclusions are
Measure = c("Participants", "Observations")
) %>%
mutate(
across(
everything(),
as.factor
)
)
# add the total values
label_positions <-
left_join(
label_positions,
exclusion_plot_data %>% select(-Value)
)
# then plot
ggplot(
exclusion_plot_data,
aes(
x = Exclusion,
y = Value,
color = Type
)
) +
geom_segment(
aes(
x = Exclusion,
xend = Exclusion,
y = 0,
yend = Value
),
alpha = 1,
size = 1
) +
facet_grid(Measure ~ Type, scales = "free") +
geom_point(alpha = 1, size = 2) +
geom_text(
aes(
label = paste0("-", `Total Before` - Value),
x = as.numeric(Exclusion) + 0.35,
y = Total * 0.1
)
) +
geom_hline(
aes(
yintercept = Total
),
linetype = "dashed"
) +
geom_hline(
aes(
yintercept = `Total Before`
),
linetype = "solid"
) +
geom_text(
data = label_positions,
aes(
label = `Total`,
x = 3.3,
y = Total * 1.05
),
color = "black"
) +
geom_text(
data = label_positions,
aes(
label = `Total Before`,
x = 3.3,
y = `Total Before` * 1.05
),
color = "black"
) +
xlab("Exclusion Criterion") +
scale_colour_manual(values=cb_palette) +
scale_fill_manual(values = cb_palette) +
theme(
axis.title.y = element_blank(),
legend.position = "none",
panel.background=element_blank(),
panel.border=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
plot.background=element_blank(),
strip.background = element_blank()
) -> exclusion_figure1
exclusion_figure1
```
Alternatively, we could emphasize the contributions of each criterion in relation to the other criteria.
Not sure I like this because it doesn't show the total sample size and the reduction in total sample size.
Then again, could simply add those to the figure captions.
```{r exclusions-plot2}
ggplot(
exclusion_plot_data %>%
mutate(
Value = `Total Before` - Value
),
aes(
x = Exclusion,
y = Value,
color = Type
)
) +
geom_segment(
aes(
x = Exclusion,
xend = Exclusion,
y = 0,
yend = Value
),
alpha = 1,
size = 1
) +
facet_grid(Measure ~ Type, scales = "free") +
geom_point(alpha = 1, size = 2) +
geom_text(
aes(
label = paste0(round(Value/`Total Before`, digits = 4) * 100, "%"),
y = Value
),
vjust = 1,
hjust = -0.2,
size = 3
) +
xlab("Exclusion Criterion") +
scale_colour_manual(values=cb_palette) +
scale_fill_manual(values = cb_palette) +
theme(
axis.title.y = element_blank(),
legend.position = "none",
panel.background=element_blank(),
panel.border=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
plot.background=element_blank(),
strip.background = element_blank()
) -> exclusion_figure2
exclusion_figure2
```
Next, I plot the distributions of the use variables.
I want to show distributions, but there are a couple of single large values, so violin or density plots will look strange with a long tail.
Raincloud plots could solve that to a degree, but they duplicate information.
I'll go with a doptplot, namely beeswarm plots.
Like before, because I facet, I'll turn the data into the long format first.
I'm also not sure whether I like vertical or horizontal beeswarms, so I'll do both.
```{r distributions-time-plot1, warning=FALSE, message=FALSE}
# temporary data file in long format
dat <-
working_file %>%
pivot_longer(
contains("_time"), # only the time variables
names_to = "measure",
values_to = "Hours per day"
) %>%
mutate(
measure = as.factor(str_to_title(str_remove(measure, "_time"))), # prettier factor levels
measure = fct_recode(
measure,