-
Notifications
You must be signed in to change notification settings - Fork 0
/
data visualization R code.Rmd
548 lines (494 loc) · 34.6 KB
/
data visualization R code.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
---
title: "Data Visualization in R"
author: "Annalise LaPlume"
date: "`r format(Sys.time(), '%B %d, %Y')`"
output:
html_document:
highlight: textmate
theme: paper
toc: yes
toc_depth: 3
toc_float: true
pdf_document:
toc: yes
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE, error=TRUE}
## load packages
if(!require("car"))install.packages("car") # Basic functions
if(!require("plyr"))install.packages("plyr")
if(!require("base"))install.packages("base")
if(!require("hablar"))install.packages("hablar")
if(!require("tidyverse"))install.packages("tidyverse")
if(!require("psych"))install.packages("psych") # Calculate descriptive statistics
if(!require("ggplot2"))install.packages("ggplot2") # Plot results
if(!require("raincloudplots"))install.packages("raincloudplots") # Raincloud plots
if(!require("ggpubr"))install.packages("ggpubr") # Plot functions (e.g., correlations)
source("R_rainclouds.R") # Raincloud plots scripts
source("summarySE.R") # Raincloud plots calculations
## Figure settings
# width and height variables for saved plots
w = 6
h = 3
```
```{r import data, include=FALSE, error=TRUE}
## import dataset
ds <- read.csv("C:/Users/.../sample_data.csv"", # Enter location of datafile here
header = TRUE, sep = ",")
# View sample of data
head(ds)
# Check format of variables
str(ds)
# Reformat variables
ds <- ds %>%
convert(num(score, age), # Specify numberic variables
fct(id, group, sex, age_group)) # Specify factors/group variables
# Set baseline (reference) level
levels(ds$group) # View levels of group
ds$group = relevel(ds$group, ref="placebo") # Reorder levels to placebo as reference
levels(ds$group)
```
# Descriptive statistics
## n
Number of individuals per group
```{r group n, include=TRUE, echo=FALSE}
table(ds$group)
```
Balanced number in each group
## Background variables
Sex
```{r sex n, include=TRUE, echo=FALSE}
table(ds$sex)
```
Age group
```{r age group n, include=TRUE, echo=FALSE}
table(ds$age_group)
```
Age
```{r age, echo=FALSE}
plyr::ddply(ds, ~group, summarise,
mean=round(mean(age), 1), sd=round(sd(age), 1),
min=min(age), max=max(age))
```
## Dependent variables
Test score (Assume lower score=worse, and higher score=better)
```{r score, echo=FALSE}
plyr::ddply(ds, ~group, summarise,
mean=round(mean(score), 1), sd=round(sd(score), 1),
min=min(score), max=max(score))
```
# Distribution plots
## Histogram
```{r histogram, echo=FALSE}
ggplot(data=ds, # Data
aes(x=score, # Variable (x)
na.rm= TRUE)) + # Remove missing values
geom_histogram( # Add histogram
fill="grey", colour="black", # Format bin colour (fill) and outline (colour)
position = "identity", # Statistic to present
bins = 10, # Number of bins/bars (can also specify binwidth)
alpha=0.4) + # Bar transparency
labs(x="Score", y="Frequency", # Axis labels
title="Histogram") + # Plot title
theme_classic() + # Remove background grid
theme(legend.position = "none", # Remove legend
axis.text=element_text(size=12), # Axis elements font size
axis.title=element_text(size=12), # Axis elements font size
plot.title=element_text(size=14, face="bold", # Title font size and bolding
hjust = 0.5)) # Title reposition to centre
# Alternate way to draw a histogram
hist(ds$score, # Variable
xlab="Score", ylab="Frequency", # Axis labels
main="Histogram", # Plot title
breaks=10) # Number of bins/bars
```
Different bar widths/number
```{r histogram bar widths, echo=FALSE}
ggplot(data=ds, # Data
aes(x=age, # Variable (x)
na.rm= TRUE)) + # Remove missing values
geom_histogram( # Add histogram
fill="grey", colour="black", # Format bin colour (fill) and outline (colour)
position = "identity", # Statistic to present
bins = 10, # Number of bins/bars (can also specify binwidth)
alpha=0.4) + # Bar transparency
labs(x="Age", y="Frequency", # Axis labels
title="Histogram (10 bins)") + # Plot title
theme_classic() + # Remove background grid
theme(legend.position = "none", # Remove legend
axis.text=element_text(size=12), # Axis elements font size
axis.title=element_text(size=12), # Axis elements font size
plot.title=element_text(size=14, face="bold", # Title font size and bolding
hjust = 0.5)) # Title reposition to centre
ggplot(data=ds, # Data
aes(x=age, # Variable (x)
na.rm= TRUE)) + # Remove missing values
geom_histogram( # Add histogram
fill="grey", colour="black", # Format bin colour (fill) and outline (colour)
position = "identity", # Statistic to present
bins = 7, # Number of bins/bars (can also specify binwidth)
alpha=0.4) + # Bar transparency
labs(x="Age", y="Frequency", # Axis labels
title="Histogram (7 bins)") + # Plot title
theme_classic() + # Remove background grid
theme(legend.position = "none", # Remove legend
axis.text=element_text(size=12), # Axis elements font size
axis.title=element_text(size=12), # Axis elements font size
plot.title=element_text(size=14, face="bold", # Title font size and bolding
hjust = 0.5)) # Title reposition to centre
```
## Dotplot
```{r dotplot, echo=FALSE}
ggplot(data=ds, # Data
aes(x=group, y = score, # Variables (x, y)
colour=group, # Grouping variable
na.rm= TRUE)) + # Remove missing values
geom_point(size = 1.2, # Data points (size)
alpha = .4, # Data points (transparency)
position = position_jitter(width = 0.1)) + # Data points (random variation)
labs(x="Group", y="Score", # Axis labels
title="Dotplot") + # Plot title
theme_classic() + # Remove background grid
theme(legend.position = "none", # Remove legend
axis.text=element_text(size=12), # Axis elements font size
axis.title=element_text(size=12), # Axis elements font size
plot.title=element_text(size=14, face="bold", # Title font size and bolding
hjust = 0.5)) # Title reposition to centre
```
## Boxplot
```{r boxplot, echo=FALSE}
ggplot(data=ds, # Data
aes(x=group, y = score, # Variables (x, y)
colour=group, # Grouping variable
na.rm= TRUE)) + # Remove missing values
labs(x="Group", y="Score", # Axis labels
title="Boxplot") + # Plot title
theme_classic() + # Format background
theme(legend.position = "none", # Remove legend
axis.text=element_text(size=12), # Axis elements font size
axis.title=element_text(size=12), # Axis font size
plot.title=element_text(size=14, face="bold", # Axis font size and bolding
hjust = 0.5)) + # Title reposition to centre
geom_boxplot(notch=FALSE, # Add boxplot outline
alpha=0, width=0.3) # Format boxplot
```
Dotplot with superimposed boxplot
```{r boxplot dotplot, echo=FALSE}
ggplot(data=ds, # Data
aes(x=group, y = score, # Variables (x, y)
colour=group, # Grouping variable
na.rm= TRUE)) + # Remove missing values
geom_point(size = 1.2, # Data points (size)
alpha = .4, # Data points (transparency)
position = position_jitter(width = 0.1)) + # Data points (random variation)
labs(x="Group", y="Score", # Axis labels
title="Boxplot-dotplot") + # Plot title
theme_classic() + # Format background
theme(legend.position = "none", # Remove legend
axis.text=element_text(size=12), # Axis elements font size
axis.title=element_text(size=12), # Axis font size
plot.title=element_text(size=14, face="bold", # Axis font size and bolding
hjust = 0.5)) + # Title reposition to centre
geom_boxplot(notch=FALSE, # Add boxplot outline
alpha=0, colour="grey46", width=0.3) # Format boxplot
```
## Violin plot
```{r violin, echo=FALSE}
ggplot(data=ds, # Data
aes(x=group, y = score, # Variables (x, y)
colour=group, # Grouping variable
na.rm= TRUE)) + # Remove missing values
labs(x="Group", y="Score", title="Violin plot") + # Axis labels and plot title
theme_classic() + # Format background
theme(legend.position = "none", # Remove legend
axis.text=element_text(size=12), # Axis elements font size
axis.title=element_text(size=12), # Axis font size
plot.title=element_text(size=14, face="bold", # Title font size and bolding
hjust = 0.5)) + # Title reposition to centre
geom_violin (alpha=0, width=0.3) # Add violin plot
```
Dotplot with superimposed violin plot
```{r violin dotplot, echo=FALSE}
ggplot(data=ds, # Data
aes(x=group, y = score, # Variables (x, y)
colour=group, # Grouping variable
na.rm= TRUE)) + # Remove missing values
geom_point(size = 1.2, # Data points (size)
alpha = .4, # Data points (transparency)
position = position_jitter(width = 0.1)) + # Data points (random variation)
labs(x="Group", y="Score", title="Violin-dotplot") + # Axis labels and plot title
theme_classic() + # Format background
theme(legend.position = "none", # Remove legend
axis.text=element_text(size=12), # Axis elements font size
axis.title=element_text(size=12), # Axis font size
plot.title=element_text(size=14, face="bold", # Title font size and bolding
hjust = 0.5)) + # Title reposition to centre
geom_violin ( # Add violin plot outline
alpha=0, colour="grey46", width=0.3) + # Format violin plot
stat_summary(fun.data = "mean_sdl", # Add mean and error bars
fun.args = list(mult = 1),
geom = "pointrange",
color = "black", size=0.2)
```
## Boxplot with many groups/categories
```{r boxplot groups, echo=FALSE}
ggplot(data=ds, # Data
aes(x=age_group, y = score, # Variables (x, y)
colour=age_group, fill=age_group, # Grouping variable
na.rm= TRUE)) + # Remove missing values
geom_point(size = 1.2, # Data points (size)
alpha = .4, # Data points (transparency)
position = position_jitter(width = 0.1)) + # Data points (random variation)
labs(x="Age Group", y="Score", # Axis labels
title="Boxplot with many groups") + # Plot title
theme_classic() + # Format background
theme(legend.position = "none", # Remove legend
axis.text=element_text(size=12), # Axis elements font size
axis.title=element_text(size=12), # Axis font size
plot.title=element_text(size=14, face="bold", # Title font size and bolding
hjust = 0.5)) + # Title reposition to centre
geom_boxplot(notch=FALSE, # Add boxplot outline
alpha=0) # Format boxplot
```
## Violin plot with many groups/categories
```{r violin groups, echo=FALSE}
ggplot(data=ds, # Data
aes(x=age_group, y = score, # Variables (x, y)
colour=age_group, # Grouping variable
na.rm= TRUE)) + # Remove missing values
geom_point(size = 1.2, # Data points (size)
alpha = .4, # Data points (transparency)
position = position_jitter(width = 0.1)) + # Data points (random variation)
labs(x="Group", y="Score", # Axis labels and
title="Violin plot with many groups") + # Plot title
theme_classic() + # Format background
theme(legend.position = "none", # Remove legend
axis.text=element_text(size=12), # Axis elements font size
axis.title=element_text(size=12), # Axis font size
plot.title=element_text(size=14, face="bold", # Title font size and bolding
hjust = 0.5)) + # Title reposition to centre
geom_violin (alpha=0) + # Format violin plot
stat_summary(fun.data = "mean_sdl", # Add mean and error bars
fun.args = list(mult = 1),
geom = "pointrange",
color = "grey", size=0.2)
```
# x-y plots
## Scatterplot
```{r scatterplot, echo=FALSE}
ggplot(data=ds, # Data
aes(x=age, y = score, colour=age, # Variables (x, y)
na.rm= TRUE)) + # Remove missing values
geom_point(size = 1.2, # Data points (size)
alpha = .4, # Data points (transparency)
position = position_jitter(width = 0.1)) + # Data points (random variation)
labs(x="Age", y="Score", title="Scatterplot") + # Axis labels and plot title
theme_classic() + # Format background
theme(legend.position = "none", # Remove legend
axis.text=element_text(size=12), # Axis elements font size
axis.title=element_text(size=12), # Axis font size
plot.title=element_text(size=14, face="bold", # Title font size and bolding
hjust = 0.5)) # Title reposition to centre
```
## Scatterplot with groups
Two groups
```{r scatterplot 2 groups, echo=FALSE}
ggplot(data=ds, # Data
aes(x=age, y = score, # Variables (x, y)
colour=group, # Grouping variable
na.rm= TRUE)) + # Remove missing values
geom_point(size = 1.2, # Data points (size)
alpha = .4, # Data points (transparency)
position = position_jitter(width = 0.1)) + # Data points (random variation)
labs(x="Age", y="Score", colour="Group", # Axis labels
title="Scatterplot with two groups") + # Plot title
theme_classic() + # Format background
theme(axis.text=element_text(size=12), # Axis elements font size
axis.title=element_text(size=12), # Axis font size
plot.title=element_text(size=14, face="bold", # Title font size and bolding
hjust = 0.5)) # Title reposition to centre
```
Many groups
```{r scatterplot many groups, echo=FALSE}
ggplot(data=ds, # Data
aes(x=age, y = score, colour=age_group, # Variables (x, y)
na.rm= TRUE)) + # Remove missing values
geom_point(size = 1.2, # Data points (size)
alpha = .4, # Data points (transparency)
position = position_jitter(width = 0.1)) + # Data points (random variation)
labs(x="Age", y="Score", colour="Age Group", # Axis labels
title="Scatterplot with many groups") + # Plot title
theme_classic() + # Format background
theme(axis.text=element_text(size=12), # Axis elements font size
axis.title=element_text(size=12), # Axis font size
plot.title=element_text(size=14, face="bold", # Title font size and bolding
hjust = 0.5)) # Title reposition to centre
```
## Line graph
Linear regression, line of best fit
```{r line graph, echo=FALSE}
ggplot(data=ds, # Data
aes(x=age, y = score, colour=age, # Variables (x, y)
na.rm= TRUE)) + # Remove missing values
geom_point(size = 1.2, # Data points (size)
alpha = .4, # Data points (transparency)
position = position_jitter(width = 0.1)) + # Data points (random variation)
labs(x="Age", y="Score", # Axis labels
title="Line graph") + # Plot title
theme_classic() + # Format background
theme(legend.position = "none", # Remove legend
axis.text=element_text(size=12), # Axis elements font size
axis.title=element_text(size=12), # Axis font size
plot.title=element_text(size=14, face="bold", # Title font size and bolding
hjust = 0.5)) + # Title reposition to centre
geom_smooth(method = "lm") + # straight line graph, 95% CI shaded in grey
stat_cor(method = "pearson") # Pearson correlation
```
Add effect size (R-squared)
```{r line graph R2, echo=FALSE}
ggplot(data=ds, # Data
aes(x=age, y = score, colour=age , # Variables (x, y)
na.rm= TRUE)) + # Remove missing values
geom_point(size = 1.2, # Data points (size)
alpha = .4, # Data points (transparency)
position = position_jitter(width = 0.1)) + # Data points (random variation)
labs(x="Age", y="Score", # Axis labels
title="Line graph") + # Plot title
theme_classic() + # Format background
theme(legend.position = "none", # Remove legend
axis.text=element_text(size=12), # Axis elements font size
axis.title=element_text(size=12), # Axis font size
plot.title=element_text(size=14, face="bold", # Title font size and bolding
hjust = 0.5)) + # Title reposition to centre
geom_smooth(method = "lm") + # straight line graph, 95% CI shaded in grey
stat_cor(method = "pearson", # Pearson correlation
aes(label = paste(..rr.label.., # R-squared
..p.label.., sep = "~`,`~"))) # p-value
```
## Line graph with two groups
Two groups, both lines on the same graph
```{r line graph 2 groups, echo=FALSE}
ggplot(data=ds, # Data
aes(x=age, y = score, # Variables (x, y)
colour=group, # Grouping variable
na.rm= TRUE)) + # Remove missing values
geom_point(size = 1.2, # Data points (size)
alpha = .4, # Data points (transparency)
position = position_jitter(width = 0.1)) + # Data points (random variation)
labs(x="Age", y="Score", colour="Group", # Axis labels
title="Line graph by Group") + # Plot title
theme_classic() + # Format background
geom_smooth(method = "lm") + # straight line graph, 95% CI shaded in grey
stat_cor(method = "pearson", # Pearson correlation
aes(label = paste(..rr.label.., # R-squared
..p.label.., sep = "~`,`~"))) + # p-value
theme(legend.position = "none", # Remove legend
axis.text=element_text(size=12), # Axis elements font size
axis.title=element_text(size=12), # Axis font size
plot.title=element_text(size=14, # Title font size
face="bold", # Title bolding
hjust = 0.5)) # Title reposition to centre
```
Two groups, side-by-side
```{r line graph many groups, echo=FALSE}
ggplot(data=ds, # Data
aes(x=age, y = score, # Variables (x, y)
colour=group, # Grouping variable
na.rm= TRUE)) + # Remove missing values
geom_point(size = 1.2, # Data points (size)
alpha = .4, # Data points (transparency)
position = position_jitter(width = 0.1)) + # Data points (random variation)
labs(x="Age", y="Score", colour="Group", # Axis labels
title="Line graph by Group") + # Plot title
theme_classic() + # Format background
geom_smooth(method = "lm") + # Straight line graph, 95% CI shaded in grey
stat_cor(method = "pearson", # Pearson correlation
aes(label = paste(..rr.label.., # R-squared
..p.label.., sep = "~`,`~"))) + # p-value
facet_wrap(. ~ group) + # Facet into separate graphs
theme(legend.position = "none", # Remove legend
axis.text=element_text(size=12), # Axis elements font size
axis.title=element_text(size=12), # Axis font size
plot.title=element_text(size=14, # Title font size
face="bold", # Title bolding
hjust = 0.5), # Title reposition to centre
strip.text = element_text(size = 14)) # Text size of panel
```
## Smooth line graphs
Locally weighted regression scatterplot smoothing (loess)
```{r loess, echo=FALSE}
ggplot(data=ds, # Data
aes(x=age, y = score, colour=age, # Variables (x, y)
na.rm= TRUE)) + # Remove missing values
geom_point(size = 1.2, # Data points (size)
alpha = .4, # Data points (transparency)
position = position_jitter(width = 0.1)) + # Data points (random variation)
labs(x="Age", y="Score", # Axis labels
title="Smooth line graph") + # Plot title
theme_classic() + # Format background
theme(legend.position = "none", # Remove legend
axis.text=element_text(size=12), # Axis elements font size
axis.title=element_text(size=12), # Axis font size
plot.title=element_text(size=14, # Title font size
face="bold", # Title bolding
hjust = 0.5)) + # Title reposition to centre
geom_smooth(method = "loess", # straight line graph, 95% CI shaded in grey
span=1) # control amount of smoothing
```
## Slope graph
```{r import repeated measures data, include=FALSE, error=TRUE}
## import dataset
ds_rep <- read.csv("C:/Users/.../rep_measures_data.csv",
header = TRUE, sep = ",")
# View sample of data
head(ds_rep) # Arranged in long format
# Check format of variables
str(ds_rep)
# Reformat variables
ds_rep <- ds_rep %>%
convert(num(score), # Specify numberic variables
fct(id, group, session)) # Specify factors/group variables
# Set baseline (reference) level
levels(ds_rep$group) # View levels of group
ds_rep$group = relevel(ds_rep$group, ref="placebo") # Reorder levels to placebo as reference
levels(ds_rep$group) # Check levels of group
levels(ds_rep$session) # View levels of session
ds_rep$session = relevel(ds_rep$session, ref="initial") # Reorder levels to initial as reference
levels(ds_rep$session) # Check levels of session
```
View longitudinal dotplot
```{r dotplot repeated measures, echo=FALSE}
ggplot(data=ds_rep, # Data
aes(x=session, y = score, # Variables (x, y)
colour=group, # Grouping variable
na.rm= TRUE)) + # Remove missing values
geom_point(size = 1.2, # Data points (size)
alpha = .4, # Data points (transparency)
position = position_jitter(width = 0.1)) + # Data points (random variation)
labs(x="Session", y="Score", # Axis labels
title="Dotplot for repeated measures") + # Plot title
theme_classic() + # Remove background grid
theme(legend.position = "none", # Remove legend
axis.text=element_text(size=12), # Axis elements font size
axis.title=element_text(size=12), # Axis elements font size
plot.title=element_text(size=14, face="bold", # Title font size and bolding
hjust = 0.5)) # Title reposition to centre
```
```{r slope graph, echo=FALSE}
ggplot(data=ds_rep, # Data
aes(x=session, y = score, # Variables (x, y)
colour=group, # Grouping variable
group=id, # Link participant scores to each other
na.rm= TRUE)) + # Remove missing values
labs(x="Session", y="Score", # Axis labels
title="Slope graph for repeated measures") + # Plot title
theme_classic() + # Remove background grid
theme(axis.text=element_text(size=12), # Axis elements font size
axis.title=element_text(size=12), # Axis elements font size
plot.title=element_text(size=14, face="bold", # Title font size and bolding
hjust = 0.5)) + # Title reposition to centre
geom_line(size = 1.2, alpha = .4, # Add lines
position = position_jitter(width = 0.2,
height=0.5))
```