-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathData_Jamboree_ggplot_printout.Rmd
357 lines (276 loc) · 8.96 KB
/
Data_Jamboree_ggplot_printout.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
---
title: "Data Jamboree- ggplot2"
output: html_document
---
```{r global_options, include=FALSE}
knitr::opts_chunk$set(fig.show='hide', warning=FALSE, message=FALSE)
```
Link to paper on dataset: http://faculty.washington.edu/kenrice/heartgraphs/effectivegraphs.pdf
Dataset url: http://faculty.washington.edu/kenrice/heartgraphs/
###Setup
####Import packages (once per machine)
```{r install_packages, eval=FALSE}
install.packages("readr")
install.packages("ggplot2")
install.packages("ggbeeswarm")
install.packages("dplyr")
install.packages("MASS")
install.packages("hexbin")
```
####Load packages (once per R session)
```{r load_packages}
library(readr)
library(ggplot2)
library(ggbeeswarm)
library(dplyr)
library(MASS)
library(hexbin)
```
####Import data (using readr)
- Use readr package to import our csv
- assign the data file to an R object using (<-)
```{r import_data}
heart <- read_csv("http://faculty.washington.edu/kenrice/heartgraphs/nhaneslarge.csv", na=".") #na= tells R that . is an na value
```
- run the command head ( ) and check with your partner to see if you got the same output
```{r head, eval=FALSE}
head(heart)
```
## Univariate Plots: Folate intake by gender
### Basic ggplot Anatomy
ggplot code makes graphs by layering imformation on top of an empty plot
```{r basic_ggplot}
ggplot(heart, aes(x=DR1TFOLA))
#This says we want ggplot to use the data.frame heart and to plot DR1TFOLA on the x-axis.
```
### Histogram
create a histogram by adding a geom layer and using the "+" sign
```{r histogram}
ggplot(heart, aes(x=DR1TFOLA)) +
geom_histogram()
```
#### Labels
change x-axis label from "DR1TFOLA" to "Folate Intake"
```{r change_folate_label}
ggplot(heart, aes(x=DR1TFOLA)) +
geom_histogram() +
labs(x = "Folate intake") #x-axis label
```
#### Changing Colors
change outline color
```{r histogram_outline, warning=TRUE}
ggplot(heart, aes(x=DR1TFOLA)) +
geom_histogram(colour = "white") +
labs(x = "Folate intake")
```
change the fill of the bars
```{r histogram_fill}
ggplot(heart, aes(x=DR1TFOLA)) +
geom_histogram(colour = "white", fill = "peachpuff") + #yes, that is a name of a color
labs(x = "Folate intake")
```
#### Documentation and the Help Command
language will be different for each type of graph so make sure to reference your documentation by typing "?command"
```{r help}
?geom_histogram
```
#### Histogram Bins and ggplot Defaults
All of our histogram plots have given us this warning:
```{r error}
#`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
```
*This warning tells us that this is a default that ggplot chose for us given the data it has.*
change bin size
```{r histogram_bin}
ggplot(heart, aes(x=DR1TFOLA)) +
geom_histogram(colour = "white", fill = "peachpuff", bins = 50)
```
#### Facet Wrap
add facet_wrap for split plots based on a variable
```{r histogram_facet}
ggplot(heart, aes(x = DR1TFOLA)) +
geom_histogram(colour = "white", fill = "peachpuff", bins = 50) +
labs(x = "Folate intake") +
facet_wrap(~gender)
```
### Density Plot
change the geom_ histogram layer to geom_density
```{r density_facet}
ggplot(heart, aes(x = DR1TFOLA)) +
geom_density(colour = "white", fill = "peachpuff") + #Make sure to take out bin=50
labs(x = "Folate intake") +
facet_wrap(~gender)
```
#### Adding a third dimension onto one plot
plot all three variables from last plot (folate, density, and gender) on one by using color to differentiate gender
```{r density}
ggplot(heart, aes(x = DR1TFOLA)) +
geom_density(aes(colour = gender)) + #Notice moving the colour into aesthetics
labs(x = "Folate intake")
```
## Univariate Plots: Systolic blood pressure by gender
###Dotplot - Stripchart
create a stripchart of `BPXSAR`(or systolic blood pressure) by gender
```{r dotplot}
ggplot(heart, aes(x = gender, y = BPXSAR)) +
geom_point()
```
#### Customizing our Dotplot
add an alpha in the geom layer to make our points more transparent
- *Alpha works on a scale from 0 (clear) to 1 (opaque)*
```{r alpha}
ggplot(heart, aes(x = gender, y = BPXSAR)) +
geom_point(alpha = .1)
```
add jitter to space out our points
```{r jitter}
ggplot(heart, aes(x = gender, y = BPXSAR)) +
geom_jitter(alpha = .1)
```
Notes about Jitter:
- jitter automatically adds space (noise) to both the height and the width of your plots
- which variables won't be effected by noice - categorical
change jitter to width only
```{r jitter_width}
ggplot(heart, aes(x = gender, y = BPXSAR)) +
geom_jitter(alpha = .1, width = .5, height = 0)
```
change our x- and y-axis labels
```{r y_axis_bp}
ggplot(heart, aes(x = gender, y = BPXSAR)) +
geom_jitter(alpha = .1, width = .5, height = 0) +
labs(x = "", y = "Systolic BP (mmHg)")
```
### Beeswarm Plot
```{r beeswarm}
ggplot(heart, aes(x = gender, y = BPXSAR)) +
geom_beeswarm(alpha = .2) +
labs(x = "Systolic BP (mmHg)", y = "")
```
#### Stats layers
add statistics on top of your plots with stat_summary
```{r beeswarm_mean}
ggplot(heart, aes(x = gender, y = BPXSAR)) +
geom_beeswarm(alpha = .2) +
stat_summary(fun.y = "mean", geom = "point", colour = "orange") +
labs(x = "Systolic BP (mmHg)", y = "")
```
#### Layering geom types
add other plot types as geom layers
```{r boxplot_beeswarm}
ggplot(heart, aes(x = gender, y = BPXSAR)) +
geom_boxplot(outlier.shape = NA) + #outlier.shape=NA tells boxplot not to plot outliers
geom_beeswarm(alpha = .2) +
labs(x = "Systolic BP (mmHg)", y = "")
```
##### Layer order
order is important (will change the order things are plotted)
```{r beeswarm_boxplot}
ggplot(heart, aes(x = gender, y = BPXSAR)) +
geom_beeswarm(alpha = .2) +
geom_boxplot(outlier.shape = NA) +
labs(x = "Systolic BP (mmHg)", y = "")
```
### Extra Challenge
#### Violin plot
If that was easy for you, give this a shot. Try and recreate the plots below using what you learned above.
*Hint: We are using "geom_violin".*
```{r violin, echo=FALSE, fig.show='asis', fig.height=3, fig.width=4}
ggplot(heart, aes(x = gender, y = BPXSAR)) +
geom_violin(alpha = .2) +
labs(x = "Systolic BP (mmHg)", y = "")
```
Try adding some statistics to your plot. You can start with the mean and the median.
```{r violin_stats, echo=FALSE, fig.show='asis', fig.height=3, fig.width=4}
ggplot(heart, aes(x = gender, y = BPXSAR)) +
geom_violin(alpha = .2) +
stat_summary(fun.y = "mean", geom = "point", colour = "orange") +
stat_summary(fun.y = "median", geom = "point", colour = "blue") +
labs(x = "Systolic BP (mmHg)", y = "")
```
Try adding another geom layer.
*Hint: Do you want to include your outlier points?*
```{r violin_boxplot, echo=FALSE, fig.show='asis', fig.height=3, fig.width=4}
ggplot(heart, aes(x = gender, y = BPXSAR)) +
geom_violin(alpha = .2) +
geom_boxplot(width = .05) +
labs(x = "Systolic BP (mmHg)", y = "")
```
# Bivariate Plots: Age & Systolic Blood Pressure
Simple scatterplot
```{r}
ggplot(heart, aes(x = RIDAGEYR, y = BPXSAR)) +
geom_point() +
labs(x = "Age (years)", y = "Systolic BP (mmHg)")
```
If you have big n, try hexbin plot
```{r}
ggplot(heart, aes(x = RIDAGEYR, y = BPXSAR)) +
geom_hex() +
labs(x = "Age (years)", y = "Systolic BP (mmHg)")
```
Add linear regression line with SE
```{r}
ggplot(heart, aes(x = RIDAGEYR, y = BPXSAR)) +
geom_point() +
geom_smooth(method = "lm") +
labs(x = "Age (years)", y = "Systolic BP (mmHg)")
```
Default is loess line
```{r}
ggplot(heart, aes(x = RIDAGEYR, y = BPXSAR)) +
geom_point() +
geom_smooth() +
labs(x = "Age (years)", y = "Systolic BP (mmHg)")
```
Add splines
```{r}
library(splines)
library(MASS)
ggplot(heart, aes(x = RIDAGEYR, y = BPXSAR)) +
geom_point() +
stat_smooth(method = "lm", formula = y ~ ns(x, 3)) +
labs(x = "Age (years)", y = "Systolic BP (mmHg)")
```
# Multivariable Plots: Body Mass Index & Systolic BP, by gender and age
Just copy this:
```{r}
library(dplyr)
heart2 <- heart %>%
mutate(age_cat = cut(RIDAGEYR,c(0,30,55,100)))
```
Recreate theirs first
```{r}
ggplot(heart2, aes(x = BMXBMI, y = BPXSAR)) +
geom_point() +
stat_smooth(aes(colour = gender), method = "lm") +
facet_wrap(~age_cat) +
labs(x = "Body Mass Index"~(kg/m^2), y = "Systolic BP (mmHg)")
```
Try with facet grid, update labels
```{r}
ggplot(heart2, aes(x = BMXBMI, y = BPXSAR)) +
geom_point() +
stat_smooth(aes(colour = gender), method = "lm") +
facet_grid(gender~age_cat) +
labs(x = "Body Mass Index"~(kg/m^2), y = "Systolic BP (mmHg)")
```
Play with colors!
```{r}
ggplot(heart2, aes(x = BMXBMI, y = BPXSAR, colour = gender)) +
geom_point(alpha = .5) +
stat_smooth(method = "lm") +
facet_grid(gender~age_cat) +
theme_minimal() +
labs(x = "Body Mass Index"~(kg/m^2), y = "Systolic BP (mmHg)") +
scale_color_manual(values = c("#B47CC7", "#D65F5F"), guide = FALSE)
```
```{r}
my_colors <- c("#C4AD66", "#77BEDB")
ggplot(heart2, aes(x = BMXBMI, y = BPXSAR, colour = gender)) +
geom_point(alpha = .5) +
stat_smooth(method = "lm") +
facet_grid(gender~age_cat) +
labs(x = "Body Mass Index"~(kg/m^2), y = "Systolic BP (mmHg)") +
scale_color_manual(values = my_colors, guide = FALSE)
```