-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathday5.Rmd
More file actions
553 lines (478 loc) · 13.7 KB
/
day5.Rmd
File metadata and controls
553 lines (478 loc) · 13.7 KB
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
---
title: "Intro to R 2025 day 5"
author: "Matt Cannon"
date: '2025-04-04'
output:
html_document:
code_folding: hide
toc: true
toc_depth: 2
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Load libraries
```{r}
library(tidyverse)
```
# Day 5
## General stuff
### geoms as layers
- When you're making a plot using ggplot2, think about it as if you're making a canvas and then adding layers to change it
- The layers are added in the order you add the code
- Layers can do things like add points, lines, or more abstract things like change the font of parts of the plot
- Each layer is separated by a "+" sign, which is pretty much the only time you'll see this in R
- The documentation for ggplot2 is quite good
- https://ggplot2.tidyverse.org/reference/
- One trick: skim the documentation for a plot that looks similar to what you want, then look at the code they used to make it
- Another: You can also do a google image search for ggplot and the type of plot you want to make then go to the page where the image is from. Often times you'll find the code they used to make it.
### Anatomy of a ggplot
ggplot( # This makes your "canvas"
data_frame_here, # The data you're using - must be a long form data.frame or tibble
aes( # "Aesthetics" - x, y, colors, etc - Applies to all layers unless overwritten
x = x here, # The name of the column that has your x values - note: don't put this in quotes
y = y here, # The name of the column that has your y values
other stuff # You can specify a lot of things here - check the documentation
)
) +
layer_1 + # Layers can be things like geom_point(), geom_line(), geom_boxplot(), etc
layer_2 + # You can add multiple different types of geoms, or even multiple of the same type
layer_3 +
other_stuff + # You can add other things like themes, labels, etc
more_stuff +
change_how_something_looks +
modify_the_plot_somehow
You can really customize the plot and the code can get quite long. Thinking about it as layers can help keep track of what you're doing
I'm not going over all the things in this document, just the ones marked with lots of pound signs ##############################################################
I put the rest in here for reference. If you go through it I've tried to drop in a bunch of random bits of code that might be useful to you in the future.
## Functions for today's activity, also for reference
#### geom_point() ##############################################################
Add a scatterplot layer to the plot
```{r}
ggplot(
mtcars,
aes(
x = hp,
y = carb
)
) +
geom_point()
# Set the overall look of the plot - I like the white background better than gray
# This will apply to all plots you make after you run this code, so you only need to run it once per session
theme_set(theme_bw()) # Changes default colors
theme_update(plot.title = element_text(hjust = 0.5)) # Force title to be in the center
ggplot(
mtcars,
aes(
x = hp,
y = carb
)
) +
geom_point() +
theme_bw()
```
#### aes() ################################################
Change colors/shapes/etc
- fill = color of the **inside** of a shape
- color = color of the **border** of a shape, note that points use color for the color of the point
- shape = the shape of the point
- size = the size of the point or line
- alpha = transparency of the point or line
- linetype = the type of line
- group = the group that the data is in, used for things like boxplots
- label = Text to plot somewhere, used in geom_text(), geom_label(), etc
If you put the argument outside of aes()
- applies to everything in that layer
- does not show up in the legend
If you put it inside aes()
- Applies to parts of the layer
- Shows up in legend
If it is in the aes() call inside of ggplot() `ggplot(aes(...))`
- It applies to all layers
- Unless another layer overwrites it
```{r}
mtcars %>%
ggplot(
aes(
x = as.factor(cyl),
y = hp
)
) +
geom_boxplot(
color = "red", # These specifications aren't in aes()
fill = "blue", # They apply to everything and we give specific values
size = 4
)
ggplot(
mtcars,
aes(
x = hp, # These specifications are in aes()
y = carb, # They apply to parts of the plot
color = as.factor(vs), # They show up in the legend
shape = as.factor(gear), # We give column names that have the groups we'll use
size = disp
)
) +
geom_point()
```
#### geom_histogram()
- Better looking than hist()
- Specify how many bars to show with `bins`
```{r}
hist(storms$pressure, n = 200)
ggplot(
storms,
aes(x = pressure)
) +
geom_histogram(bins = 200)
```
#### geom_density()
- Smooth your data out in a smoothed histogram-looking plot
- The adjust argument can make it more or less smooth - 0 is the raw data, 1 is very smooth
- This can actually hide some of the nuance in your data
```{r}
ggplot(
storms,
aes(
x = pressure,
fill = as.factor(category)
)
) +
geom_density(alpha = 0.1, adjust = 0.5) + # alpha sets transparency -- 0 is clear, 1 is opaque
scale_color_brewer(palette = "Set2") # The brewer color palettes are pretty, and "Set2" is color-blind friendly
```
#### geom_col() ################################################################
Add a barplot layer to the plot
```{r}
mtcars %>%
rownames_to_column("car_make") %>% # This makes the row names a column so we can use it in the plot
mutate(car_make = str_remove(car_make, " .+")) %>% # This removes everything after the first space in the car_make column
ggplot(
aes(
x = car_make,
y = hp
)
) +
geom_col(color = "red") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
ggtitle("this is a title") +
theme_update(plot.title = element_text(hjust = 0.5))
mtcars %>%
rownames_to_column("car_make") %>%
ggplot(aes(y = car_make, x = hp)) +
geom_col()
```
#### geom_errorbar()###########################################################
Add error bars as a layer to plot
```{r}
summarized_hp <-
mtcars %>%
group_by(cyl) %>%
summarize(
mean_hp = mean(hp),
sd_hp = sd(hp)
)
summarized_hp
ggplot(
summarized_hp,
aes(
x = as.factor(cyl),
y = mean_hp
)
) +
geom_col() +
geom_errorbar(
aes(
ymin = mean_hp - sd_hp,
ymax = mean_hp + sd_hp
),
width = 0.2,
color = "red"
)
# This has error bars in the back since that layer was added first!
ggplot(
summarized_hp,
aes(
x = as.factor(cyl),
y = mean_hp
)
) +
geom_errorbar(
aes(
ymin = mean_hp - sd_hp,
ymax = mean_hp + sd_hp
),
color = "red"
) +
geom_bar(stat = "identity") +
geom_point(color = "blue")
```
#### geom_jitter()
Drop-in replacement for geom_point()
Adds noise in x and y directions by default so you can see individual points
```{r}
ggplot(
summarized_hp,
aes(
x = as.factor(cyl),
y = mean_hp
)
) +
geom_col() +
geom_errorbar(
aes(
ymin = mean_hp - sd_hp,
ymax = mean_hp + sd_hp
)
) +
geom_jitter(
data = mtcars, # Note that we can provide an individual geom it's own data
aes(
x = as.factor(cyl),
y = hp
),
color = "red", height = 0
)
```
#### ggbeeswarm package
Adds noise in x direction and pulls things to the center to make it look nicer than jitter
```{r}
ggplot(
summarized_hp,
aes(
x = as.factor(cyl),
y = mean_hp
)
) +
geom_col() +
geom_errorbar(
aes(
ymin = mean_hp - sd_hp,
ymax = mean_hp + sd_hp
)
) +
ggbeeswarm::geom_beeswarm(
data = mtcars,
aes(
x = as.factor(cyl),
y = hp
),
color = "red"
)
```
#### geom_boxplot() ############################################################
Boxplots!
```{r}
mtcars %>%
ggplot(aes(x = as.factor(cyl), y = hp)) +
geom_boxplot() +
geom_violin(adjust = 1, fill = "red", alpha = 0.5)
```
#### geom_smooth
Add a regression line to your plot
Can specify the type of regression with `method`
Can remove gray confidence interval with `se = FALSE`
```{r}
ggplot(
mtcars,
aes(
x = mpg,
y = disp
)
) +
geom_point() +
geom_smooth()
```
#### Use multiple datasets in the same plot
Contents of the columns used as axes have to match
Each geom using it's own data needs to have "data = " in it
```{r}
ggplot(
summarized_hp,
aes(
x = as.factor(cyl),
y = mean_hp
)
) +
geom_col() +
geom_errorbar(
aes(
ymin = mean_hp - sd_hp,
ymax = mean_hp + sd_hp
),
width = 0.5
) +
geom_jitter(
data = mtcars,
aes(
x = as.factor(cyl),
y = hp
),
color = "red"
)
```
#### labs()
Add labels
```{r}
ggplot(mtcars) +
geom_point(aes(x = hp, y = disp)) +
labs(
x = "X label goes here",
y = "Y label goes here",
title = "This is a title",
subtitle = "Subtitle!",
caption = "Cannon et al, 2034!",
tag = "A"
)
```
#### facet_wrap() ##############################################################
```{r}
dplyr::storms
ggplot(
dplyr::storms,
aes(x = pressure)
) +
geom_histogram(bins = 100) +
facet_wrap(~ category + status)
ggplot(
dplyr::storms,
aes(x = pressure)
) +
geom_histogram(bins = 100) +
facet_wrap(~category, scales = "free_y")
ggplot(
dplyr::storms,
aes(x = pressure)
) +
geom_histogram(bins = 100) +
facet_wrap(
~category,
nrow = 1,
scales = "free_y"
)
```
#### theme()
A good guide to what theme elements are what:
https://henrywang.nl/ggplot2-theme-elements-demonstration/
You can change just about any aspect of how the plot looks with theme()
You'll have to look up how to change each specific bit
```{r}
ggplot(
storms,
aes(
x = pressure,
fill = category
)
) +
geom_density(alpha = 0.5) + # alpha sets transparency - 0 is clear, 1 is opaque
scale_color_brewer(palette = "Set2") + # The brewer color palettes are pretty, and "Set2" is color-blind friendly
theme(
legend.position = c(0.2, 0.9),
legend.direction = "horizontal",
legend.key.width = unit(2, "cm"),
legend.title = element_text(face = "bold"),
strip.background = element_rect(color = "white", fill = "white"),
strip.text.x = element_text(size = 30, face = "bold"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
axis.line.x = element_line(color = "black"),
axis.text.y = element_blank(),
axis.ticks.y = element_blank()
)
```
### ggsave()
```{r}
ggsave(
"examplePlot.png",
width = 10,
height = 8
)
plot_in_variable <-
ggplot(
dplyr::storms,
aes(x = pressure)
) +
geom_histogram(bins = 100) +
facet_wrap(~category)
# ggsave will get the plot size from how big your "plot" windows is in the right pane if you don't specify it!!!!!
ggsave(
"anotherExamplePlot.png",
plot = plot_in_variable
)
```
### heatmap() ##################################################################
If you like heatmaps also check out the pheatmap package
```{r}
heatmap(as.matrix(mtcars))
# Scaling the categories instead
heatmap(as.matrix(mtcars), scale = "column")
pheatmap::pheatmap(as.matrix(mtcars))
```
### PCA
```{r}
mtcars <- mtcars
pca_raw <- prcomp(mtcars, scale = TRUE)
?prcomp
# The output of prcomp is a list
# The element named "x" contains the actual principle components
# Though, it needs to be converted from a matrix to a dataframe for ggplot
pc_values <-
pca_raw$x %>%
as.data.frame() %>%
rownames_to_column("car_make")
ggplot(
pc_values,
aes(
x = PC1,
y = PC2
)
) +
geom_point() +
ggrepel::geom_text_repel(aes(label = car_make))
```
## Activity
## Recreate plots from a paper
- Data is from https://doi.org/10.1371/journal.pbio.2005756
- Najafov A, Zervantonakis IK, Mookhtiar AK, Greninger P, March RJ, Egan RK, et al. (2018) BRAF and AXL oncogenes drive RIPK3 expression loss in cancer. PLoS Biol 16(8): e2005756. https://doi.org/10.1371/journal.pbio.2005756
- I downloaded their figure data and made it easier to import:
- Fig1A_partial.txt
- Fig1C.txt
- Fig2F.txt
- Fig3F.txt
- You can see what the plots should look like in the corresponding files in materials/Figure_***.png files
- You don't have to make them look exact, feel free to play around with how you want them to look
#### Figure 1A - geom_boxplot()
```{r}
```
#### Figure 1C - heatmap()
```{r}
```
#### Figure 2F - geom_point()
```{r}
```
#### Figure 3F - geom_col
use geom_errorbars()
and facet_wrap() to split the plots by cell_line
```{r}
```
## If you're super fast:
Keep going, do the rest 🙃
Download the data from the supplemental data here:
https://doi.org/10.1371/journal.pbio.2005756.s001
####
# You're done! You made it through introduction to R!
####
Suggestions for what to do next:
- Try to practice working in R as much as you can
- Find ways that R can be useful for you and explore packages that are useful for the type of work you do
- You're still learning! Don't get down on yourself when you struggle, because you will
- The better you get at R, the less you'll struggle, but everyone runs into problems
- Read the documentation for functions/packages you think you'll use a lot
- Make all the errors!
- Then google them and try to understand what went wrong
- Go back through and re-read the materials to reinforce what you've learned and pick up little details you may have missed
- If you're going to the SCRGOT single cell workshop, do some reading on Seurat
- https://satijalab.org/seurat/
- The teams channel from the workshop shouldn't get deleted right away, so if you have problems, post there to see if people can help
- Also, if other people post, read what problems they ran into and try to learn from it