-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplots_code-git.R
423 lines (341 loc) · 17.5 KB
/
plots_code-git.R
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
# load packages #
library(tidyverse)
library(ggplot2)
library(skimr)
library(patchwork)
library(reshape2)
###################plots######################
##### screentime, social media, and PROMIS measures #####
# add jitter to each wave for plotting
set.seed(1021)
combined_dat$wj <- jitter(combined_dat$wave, amount=.09)
# total screen time
st <- ggplot(combined_dat, aes(wj, st_total)) +
geom_line(aes(group= prolific_pid), color = 'lightgray', alpha = 0.3) +
geom_point(color = "purple", alpha = 0.25) +
geom_boxplot(lwd = .1,data = combined_dat %>%
filter(wave==1), aes(x=wave, y = st_total), width = .3,
color = 'black', size=1, alpha = 0.15, na.rm = TRUE) +
geom_boxplot(lwd = .1,data = combined_dat %>%
filter(wave==2), aes(x=wave, y = st_total), width = .3,
color = 'black', size=1, alpha = 0.15, na.rm = TRUE) +
geom_boxplot(lwd = .1,data = combined_dat %>%
filter(wave==3), aes(x=wave, y = st_total), width = .3,
color = 'black', size=1, alpha = 0.15, na.rm = TRUE) +
geom_boxplot(lwd = .1,data = combined_dat %>%
filter(wave==4), aes(x=wave, y = st_total), width = .3,
color = 'black', size=1, alpha = 0.15, na.rm = TRUE) +
scale_x_continuous(breaks=c(1,2,3,4), labels=c("1", "2", "3", "4"), limits=c(0.5, 4.5) ) +
xlab("Wave") + ylab("Screentime (hours)") +
theme_classic()
st
# social media
sm <- ggplot(combined_dat, aes(wj, sm_total)) +
geom_line(aes(group= prolific_pid), color = 'lightgray', alpha = 0.3) +
geom_point(color = "cyan", alpha = 0.25) +
geom_boxplot(lwd = .1,data = combined_dat %>%
filter(wave==1), aes(x=wave, y = sm_total), width = .3,
color = 'black', size=1, alpha = 0.15, na.rm = TRUE) +
geom_boxplot(lwd = .1,data = combined_dat %>%
filter(wave==2), aes(x=wave, y = sm_total), width = .3,
color = 'black', size=1, alpha = 0.15, na.rm = TRUE) +
geom_boxplot(lwd = .1,data = combined_dat %>%
filter(wave==3), aes(x=wave, y = sm_total), width = .3,
color = 'black', size=1, alpha = 0.15, na.rm = TRUE) +
geom_boxplot(lwd = .1,data = combined_dat %>%
filter(wave==4), aes(x=wave, y = sm_total), width = .3,
color = 'black', size=1, alpha = 0.15, na.rm = TRUE) +
scale_x_continuous(breaks=c(1,2,3,4), labels=c("1", "2", "3", "4"), limits=c(0.5, 4.5) ) +
scale_y_continuous(limits = c(0, 65)) +
xlab("Wave") + ylab("Social Media (hours)") +
theme_classic()
sm
# pickups
pups <- ggplot(combined_dat, aes(wj, st_pickups_total)) +
geom_line(aes(group= prolific_pid), color = 'lightgray', alpha = 0.3) +
geom_point(color = "deeppink", alpha = 0.25) +
geom_boxplot(lwd = .1,data = combined_dat %>%
filter(wave==1), aes(x=wave, y = st_pickups_total), width = .3,
color = 'black', size=1, alpha = 0.15, na.rm = TRUE) +
geom_boxplot(lwd = .1,data = combined_dat %>%
filter(wave==2), aes(x=wave, y = st_pickups_total), width = .3,
color = 'black', size=1, alpha = 0.15, na.rm = TRUE) +
geom_boxplot(lwd = .1,data = combined_dat %>%
filter(wave==3), aes(x=wave, y = st_pickups_total), width = .3,
color = 'black', size=1, alpha = 0.15, na.rm = TRUE) +
geom_boxplot(lwd = .1,data = combined_dat %>%
filter(wave==4), aes(x=wave, y = st_pickups_total), width = .3,
color = 'black', size=1, alpha = 0.15, na.rm = TRUE) +
scale_x_continuous(breaks=c(1,2,3,4), labels=c("1", "2", "3", "4"), limits=c(0.5, 4.5) ) +
xlab("Wave") + ylab("Pickups") +
theme_classic()
pups
# depression
dep <- ggplot(combined_dat, aes(wj, dep_tscore)) +
geom_line(aes(x=wj, group= prolific_pid), color = 'lightgray', alpha = 0.3) +
geom_point(color = "dodgerblue", alpha = 0.15) +
geom_boxplot(lwd = .1, data = combined_dat %>%
filter(wave==1), aes(x=wave, y = dep_tscore), width = .2,
color = 'black', size=1, alpha = 0.15, na.rm = TRUE) +
geom_boxplot(lwd = .1, data = combined_dat %>%
filter(wave==2), aes(x=wave, y = dep_tscore), width = .2,
color = 'black', size=1, alpha = 0.15, na.rm = TRUE) +
geom_boxplot(lwd = .1, data = combined_dat %>%
filter(wave==3), aes(x=wave, y = dep_tscore), width = .2,
color = 'black', size=1, alpha = 0.15, na.rm = TRUE) +
geom_boxplot(lwd = .1, data = combined_dat %>%
filter(wave==4), aes(x=wave, y = dep_tscore), width = .2,
color = 'black', size=1, alpha = 0.15, na.rm = TRUE) +
scale_x_continuous(breaks=c(1,2,3,4), labels=c("1", "2", "3", "4"), limits=c(0.5, 4.5) ) +
xlab("Wave") + ylab("Depression") +
theme_classic()
dep
# anxiety
anx <- ggplot(combined_dat, aes(wj, anx_tscore)) +
geom_line(aes(x=wj, group= prolific_pid), color = 'lightgray', alpha = 0.3) +
geom_point(color = "red", alpha = 0.15) +
geom_boxplot(lwd = .1, data = combined_dat %>%
filter(wave==1), aes(x=wave, y = anx_tscore), width = .2,
color = 'black', size=1, alpha = 0.15, na.rm = TRUE) +
geom_boxplot(lwd = .1, data = combined_dat %>%
filter(wave==2), aes(x=wave, y = anx_tscore), width = .2,
color = 'black', size=1, alpha = 0.15, na.rm = TRUE) +
geom_boxplot(lwd = .1, data = combined_dat %>%
filter(wave==3), aes(x=wave, y = anx_tscore), width = .2,
color = 'black', size=1, alpha = 0.15, na.rm = TRUE) +
geom_boxplot(lwd = .1, data = combined_dat %>%
filter(wave==4), aes(x=wave, y = anx_tscore), width = .2,
color = 'black', size=1, alpha = 0.15, na.rm = TRUE) +
scale_x_continuous(breaks=c(1,2,3,4), labels=c("1", "2", "3", "4"), limits=c(0.5, 4.5) ) +
xlab("Wave") + ylab("Anxiety") +
theme_classic()
anx
# sleep dist.
sleep <- ggplot(combined_dat, aes(wj, sleep_tscore)) +
geom_line(aes(x=wj, group= prolific_pid), color = 'lightgray', alpha = 0.3) +
geom_point(color = "green", alpha = 0.15) +
geom_boxplot(lwd = .1, data = combined_dat %>%
filter(wave==1), aes(x=wave, y = sleep_tscore), width = .2,
color = 'black', size=1, alpha = 0, na.rm = TRUE) +
geom_boxplot(lwd = .1, data = combined_dat %>%
filter(wave==2), aes(x=wave, y = sleep_tscore), width = .2,
color = 'black', size=1, alpha = 0, na.rm = TRUE) +
geom_boxplot(lwd = .1, data = combined_dat %>%
filter(wave==3), aes(x=wave, y = sleep_tscore), width = .2,
color = 'black', size=1, alpha = 0, na.rm = TRUE) +
geom_boxplot(lwd = .1, data = combined_dat %>%
filter(wave==4), aes(x=wave, y = sleep_tscore), width = .2,
color = 'black', size=1, alpha = 0, na.rm = TRUE) +
scale_x_continuous(breaks=c(1,2,3,4), labels=c("1", "2", "3", "4"), limits=c(0.5, 4.5) ) +
xlab("Wave") + ylab("Sleep Disturbance") +
theme_classic()
sleep
# social isolation
iso <- ggplot(combined_dat, aes(wj, soc_iso_tscore)) +
geom_line(aes(x=wj, group= prolific_pid), color = 'lightgray', alpha = 0.3) +
geom_point(color = "orange", alpha = 0.25) +
geom_boxplot(lwd = .1, data = combined_dat %>%
filter(wave==1), aes(x=wave, y = soc_iso_tscore), width = .2,
color = 'black', size=1, alpha = 0.15, na.rm = TRUE) +
geom_boxplot(lwd = .1, data = combined_dat %>%
filter(wave==2), aes(x=wave, y = soc_iso_tscore), width = .2,
color = 'black', size=1, alpha = 0.15, na.rm = TRUE) +
geom_boxplot(lwd = .1, data = combined_dat %>%
filter(wave==3), aes(x=wave, y = soc_iso_tscore), width = .2,
color = 'black', size=1, alpha = 0.15, na.rm = TRUE) +
geom_boxplot(lwd = .1, data = combined_dat %>%
filter(wave==4), aes(x=wave, y = soc_iso_tscore), width = .2,
color = 'black', size=1, alpha = 0.15, na.rm = TRUE) +
scale_x_continuous(breaks=c(1,2,3,4), labels=c("1", "2", "3", "4"), limits=c(0.5, 4.5) ) +
xlab("Wave") + ylab("Social Isolation") +
theme_classic()
iso
# plot side-by-side
all <- st + sm + pups + dep + anx + sleep + iso
st_sm_pups <- st + sm + pups
promis <- dep + anx + sleep + iso
# save high-quality versions
tiff("all_dotandboxplots.tiff", units="in", width=7.75, height=6, res=300)
all
dev.off()
tiff("stsmpups_dotandboxplots.tiff", units="in", width=7.75, height=6, res=300)
st_sm_pups
dev.off()
tiff("promis_dotandboxplots.tiff", units="in", width=7.75, height=6, res=300)
promis
dev.off()
############# individual plots ##############
# standardize variables
z_dat <- combined_dat %>%
select(id, wave, st_total, sm_total, dep_tscore,
anx_tscore, sleep_tscore, soc_iso_tscore)
z_dat <- z_dat %>%
mutate_at(vars(c(-id, -wave)),scale)
# create random subsample of those with >2 waves
set.seed(1512)
z_dat <- z_dat %>%
count(id) %>%
left_join(z_dat, by = 'id') %>%
filter(n>2)
pids <- z_dat %>%
group_by(id) %>%
summarize() %>%
sample_n(50)
slice <- right_join(z_dat, pids, by= 'id')
i <- ggplot(data=slice, aes(x=wave)) +
geom_line(aes(y=st_total, color= 'screen time') ) +
geom_line(aes(y=dep_tscore, color = 'dep') ) +
geom_line(aes(y=anx_tscore, color = 'anx') ) +
geom_line(aes(y=sleep_tscore, color = 'sleep dist') )
i <- i + facet_wrap(~ id, ncol = 10) +
scale_color_manual("",
breaks = c("screen time", "dep", "anx", "sleep dist"),
values = c("screen time" ="black", "dep"="blue", "anx"="red", "sleep dist"="green")) +
labs(x= "Wave", y= "z-score")
tiff("ind_lineplots.tiff", units="in", width=7.75, height=6, res=300)
i
dev.off()
##### plot variables by gender (male/female) #####
dat<- combined_dat %>%
filter(gender != "other")
# summarize variables by gender using ::skimr
sum_dat_bygender<- dat %>%
group_by(gender) %>%
skim( c(
age, st_total, sm_total,
st_pickups_total, health_steps_avg,
starts_with("covid_impact"), dep_tscore, si,
anx_tscore, sleep_tscore, soc_iso_tscore,
soc_comp_total) )
#summarize by gender by wave
sum_dat_bygenderwave<- dat %>%
group_by(gender, wave) %>%
skim( c(
age, st_total, sm_total,
st_pickups_total, health_steps_avg,
starts_with("covid_impact"), dep_tscore, si,
anx_tscore, sleep_tscore, soc_iso_tscore,
soc_comp_total) )
# plot st/sm and promis variables by gender
meanst_bygender <- sum_dat_bygenderwave %>%
select(wave, gender, skim_variable, numeric.mean) %>%
filter(skim_variable == 'st_total')
st_bygender <- ggplot(meanst_bygender, aes(wave, numeric.mean, color = gender)) +
geom_line(size = 1.5, show.legend = FALSE) +
geom_line(data=dat, aes(wj, st_total, group= id), color = 'lightgray', alpha = 0.3) +
geom_point(data=dat, aes(wj, st_total, color=gender), alpha = 0.3, show.legend = FALSE) +
xlab("Wave") + ylab("Screentime (hours)") +
theme_classic()
meansm_bygender <- sum_dat_bygenderwave %>%
select(wave, gender, skim_variable, numeric.mean) %>%
filter(skim_variable == 'sm_total')
sm_bygender <- ggplot(meansm_bygender, aes(wave, numeric.mean, color = gender)) +
geom_line(size = 1.5, show.legend = FALSE) +
geom_line(data=dat, aes(wj, sm_total, group= id), color = 'lightgray', alpha = 0.3) +
geom_point(data=dat, aes(wj, sm_total, color=gender), alpha = 0.3, show.legend = FALSE) +
xlab("Wave") + ylab("Social Media (hours)") +
scale_y_continuous(limits = c(0, 65)) +
theme_classic()
meanpups_bygender <- sum_dat_bygenderwave %>%
select(wave, gender, skim_variable, numeric.mean) %>%
filter(skim_variable == 'st_pickups_total')
pups_bygender <- ggplot(meanpups_bygender, aes(wave, numeric.mean, color = gender)) +
geom_line(size = 1.5) +
geom_line(data=dat, aes(wj, st_pickups_total, group= id), color = 'lightgray', alpha = 0.3) +
geom_point(data=dat, aes(wj, st_pickups_total, color=gender), alpha = 0.3) +
xlab("Wave") + ylab("Pickups") +
theme_classic()
meandep_bygender <- sum_dat_bygenderwave %>%
select(wave, gender, skim_variable, numeric.mean) %>%
filter(skim_variable == 'dep_tscore')
dep_bygender <- ggplot(meandep_bygender, aes(wave, numeric.mean, color = gender)) +
geom_line(size = 1.5,show.legend = FALSE) +
geom_line(data=dat, aes(wj, dep_tscore, group= id), color = 'lightgray', alpha = 0.3) +
geom_point(data=dat, aes(wj, dep_tscore, color=gender), alpha = 0.3,show.legend = FALSE) +
xlab("Wave") + ylab("Depression") +
theme_classic()
meananx_bygender <- sum_dat_bygenderwave %>%
select(wave, gender, skim_variable, numeric.mean) %>%
filter(skim_variable == 'anx_tscore')
anx_bygender <- ggplot(meananx_bygender, aes(wave, numeric.mean, color = gender)) +
geom_line(size = 1.5,show.legend = FALSE) +
geom_line(data=dat, aes(wj, anx_tscore, group= id), color = 'lightgray', alpha = 0.3) +
geom_point(data=dat, aes(wj, anx_tscore, color=gender), alpha = 0.3,show.legend = FALSE) +
xlab("Wave") + ylab("Anxiety") +
theme_classic()
meaniso_bygender <- sum_dat_bygenderwave %>%
select(wave, gender, skim_variable, numeric.mean) %>%
filter(skim_variable == 'soc_iso_tscore')
iso_bygender <- ggplot(meaniso_bygender, aes(wave, numeric.mean, color = gender)) +
geom_line(size = 1.5,show.legend = FALSE) +
geom_line(data=dat, aes(wj, soc_iso_tscore, group= id), color = 'lightgray', alpha = 0.3) +
geom_point(data=dat, aes(wj, soc_iso_tscore, color=gender), alpha = 0.3,show.legend = FALSE) +
xlab("Wave") + ylab("Social Isolation") +
theme_classic()
meansleep_bygender <- sum_dat_bygenderwave %>%
select(wave, gender, skim_variable, numeric.mean) %>%
filter(skim_variable == 'sleep_tscore')
sleep_bygender <- ggplot(meansleep_bygender, aes(wave, numeric.mean, color = gender)) +
geom_line(size = 1.5,show.legend = FALSE) +
geom_line(data=dat, aes(wj, sleep_tscore, group= id), color = 'lightgray', alpha = 0.3) +
geom_point(data=dat, aes(wj, sleep_tscore, color=gender), alpha = 0.3) +
xlab("Wave") + ylab("Sleep Disturbance") +
theme_classic()
#combine plots
usevars_bygender<- st_bygender + sm_bygender + pups_bygender
promisvars_bygender<- dep_bygender + anx_bygender + iso_bygender + sleep_bygender
tiff("usevars_bygender.tiff", units="in", width=7, height=5, res=300)
usevars_bygender
dev.off()
tiff("promisvars_bygender.tiff", units="in", width=7, height=5, res=300)
promisvars_bygender
dev.off()
###### plot aggregated usage_var~promis_var scatterplots by gender #####
# st ~ dep
st_dep<- ggplot(dat, aes(st_total,dep_tscore, color=gender) ) +
geom_point(alpha = 0.2,show.legend = FALSE) +
stat_cor(aes(label= ..r.label.. ), method = "pearson", label.x = 100, na.rm = TRUE, size = 3, show.legend = FALSE) +
geom_smooth(method='lm',show.legend = FALSE)
# sm ~ dep
sm_dep<- ggplot(dat, aes(sm_total,dep_tscore, color=gender) ) +
geom_point(alpha = 0.2,show.legend = FALSE) +
stat_cor(aes(label= ..r.label.. ), method = "pearson", label.x = 35, na.rm = TRUE, size = 3, show.legend = FALSE) +
geom_smooth(method='lm',show.legend = FALSE) +
scale_x_continuous(limits = c(0,65))
# st ~ anx
st_anx<- ggplot(dat, aes(st_total,anx_tscore, color=gender) ) +
geom_point(alpha = 0.2,show.legend = FALSE) +
stat_cor(aes(label= ..r.label.. ), method = "pearson", label.x = 100, na.rm = TRUE, size = 3, show.legend = FALSE) +
geom_smooth(method='lm',show.legend = FALSE)
# sm ~ anx
sm_anx<- ggplot(dat, aes(sm_total,anx_tscore, color=gender) ) +
geom_point(alpha = 0.2,show.legend = FALSE) +
stat_cor(aes(label= ..r.label.. ), method = "pearson", label.x = 35, na.rm = TRUE, size = 3, show.legend = FALSE) +
geom_smooth(method='lm',show.legend = FALSE) +
scale_x_continuous(limits = c(0, 65))
# st ~ iso
st_iso<- ggplot(dat, aes(st_total,soc_iso_tscore, color=gender) ) +
geom_point(alpha = 0.2,show.legend = FALSE) +
stat_cor(aes(label= ..r.label.. ), method = "pearson", label.x = 100, na.rm = TRUE, size = 3, show.legend = FALSE) +
geom_smooth(method='lm',show.legend = FALSE)
# sm ~ iso
sm_iso<- ggplot(dat, aes(sm_total,soc_iso_tscore, color=gender) ) +
geom_point(alpha = 0.2,show.legend = FALSE) +
stat_cor(aes(label= ..r.label.. ), method = "pearson", label.x = 35, na.rm = TRUE, size = 3, show.legend = FALSE) +
geom_smooth(method='lm',show.legend = TRUE) +
scale_x_continuous(limits = c(0, 65))
# st ~ sleep
st_sleep <- ggplot(dat, aes(st_total,sleep_tscore, color=gender) ) +
geom_point(alpha = 0.2,show.legend = FALSE) +
stat_cor(aes(label= ..r.label.. ), method = "pearson", label.x = 100, na.rm = TRUE, size = 3, show.legend = FALSE) +
geom_smooth(method='lm',show.legend = FALSE)
# sm ~ sleep
sm_sleep <- ggplot(dat, aes(sm_total,sleep_tscore, color=gender) ) +
geom_point(alpha = 0.2,show.legend=FALSE) +
stat_cor(aes(label= ..r.label.. ), method = "pearson", label.x = 35, na.rm = TRUE, size = 3, show.legend = FALSE) +
geom_smooth(method='lm', show.legend = FALSE) +
scale_x_continuous(limits = c(0, 65))
gender.scatters<-st_dep + sm_dep + st_anx + sm_anx + st_iso + sm_iso + st_sleep + sm_sleep
tiff("gender.scatters.tiff", units="in", width=7, height=5, res=300)
gender.scatters
dev.off()