forked from BCCHR-trainee-omics-group/StudyGroup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintro-to-gglot2.html
913 lines (683 loc) · 24.1 KB
/
intro-to-gglot2.html
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
<!DOCTYPE html>
<html lang="" xml:lang="">
<head>
<title>Introduction to ggplot2</title>
<meta charset="utf-8" />
<meta name="author" content="Victor Yuan" />
<meta name="date" content="2020-07-09" />
<link href="libs/remark-css-0.0.1/default.css" rel="stylesheet" />
<link href="libs/remark-css-0.0.1/middlebury-fonts.css" rel="stylesheet" />
<link rel="stylesheet" href="custom1.css" type="text/css" />
</head>
<body>
<textarea id="source">
class: center, middle, inverse, title-slide
# Introduction to ggplot2
## download @ bit.ly/2ZOShd4
### Victor Yuan
### 2020-07-09
---
# Set up
Install these packages
```r
install.packages(tidyverse)
```
Load libraries
```r
library(tidyverse)
```
```
## -- Attaching packages -------------------------------------------- tidyverse 1.3.0 --
```
```
## v ggplot2 3.3.0 v purrr 0.3.3
## v tibble 2.1.3 v dplyr 0.8.4
## v tidyr 1.0.2 v stringr 1.4.0
## v readr 1.3.1 v forcats 0.4.0
```
```
## -- Conflicts ----------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
```
---
# Load gene expression / methylation data
```r
geo_data <- read_csv('https://raw.githubusercontent.com/wvictor14/TOG/master/data/GSE98224.csv')
geo_data
```
```
## # A tibble: 48 x 159
## expr_geo_id meth_geo_id diagnosis tissue maternal_age maternal_bmi
## <chr> <chr> <chr> <chr> <dbl> <dbl>
## 1 GSM1940495 GSM2589532 PE Place~ 37 19.5
## 2 GSM1940496 GSM2589533 PE Place~ 40 25.7
## 3 GSM1940499 GSM2589534 PE Place~ 37 25
## 4 GSM1940500 GSM2589535 PE Place~ 38 26.2
## 5 GSM1940501 GSM2589536 PE Place~ 33 31.2
## 6 GSM1940502 GSM2589537 PE Place~ 26 31.2
## 7 GSM1940505 GSM2589538 PE Place~ 31 18.6
## 8 GSM1940506 GSM2589539 PE Place~ 37 25.2
## 9 GSM1940507 GSM2589540 non-PE Place~ 35 18.6
## 10 GSM1940508 GSM2589541 PE Place~ 32 26.6
## # ... with 38 more rows, and 153 more variables: maternal_ethnicity <chr>,
## # ga_weeks <dbl>, ga_days <dbl>, transcript_8033795 <dbl>,
## # transcript_8103881 <dbl>, transcript_7904014 <dbl>,
## # transcript_8127692 <dbl>, transcript_7990031 <dbl>,
## # transcript_8121144 <dbl>, transcript_8150846 <dbl>,
## # transcript_7962246 <dbl>, transcript_7941890 <dbl>,
## # transcript_7896644 <dbl>, transcript_7992897 <dbl>,
## # transcript_7973002 <dbl>, transcript_7979800 <dbl>,
## # transcript_8112007 <dbl>, transcript_8036686 <dbl>,
## # transcript_8001325 <dbl>, transcript_8180328 <dbl>,
## # transcript_8109283 <dbl>, transcript_8041223 <dbl>,
## # transcript_8144703 <dbl>, transcript_7997556 <dbl>,
## # transcript_7955896 <dbl>, transcript_7939897 <dbl>,
## # transcript_8035078 <dbl>, transcript_8113094 <dbl>,
## # transcript_7893397 <dbl>, transcript_8110708 <dbl>,
## # transcript_8102610 <dbl>, transcript_8083407 <dbl>,
## # transcript_8174592 <dbl>, transcript_7922299 <dbl>,
## # transcript_7979269 <dbl>, transcript_8074593 <dbl>,
## # transcript_7967810 <dbl>, transcript_8052562 <dbl>,
## # transcript_7927775 <dbl>, transcript_8005601 <dbl>,
## # transcript_8129974 <dbl>, transcript_8070295 <dbl>,
## # transcript_7952795 <dbl>, transcript_8044743 <dbl>,
## # transcript_7896053 <dbl>, transcript_7894489 <dbl>,
## # transcript_8048889 <dbl>, transcript_7894063 <dbl>,
## # transcript_8171539 <dbl>, transcript_8011396 <dbl>,
## # transcript_7983157 <dbl>, transcript_8171848 <dbl>,
## # transcript_8097443 <dbl>, cg04950931 <dbl>, cg21697851 <dbl>,
## # cg20092728 <dbl>, cg12804791 <dbl>, cg11619216 <dbl>, cg07802350 <dbl>,
## # cg13175060 <dbl>, cg25632577 <dbl>, cg11811391 <dbl>, cg20981848 <dbl>,
## # cg14025883 <dbl>, cg25493658 <dbl>, cg01491071 <dbl>, cg03777414 <dbl>,
## # cg20586124 <dbl>, cg16175792 <dbl>, cg25961733 <dbl>, cg13912117 <dbl>,
## # cg27307465 <dbl>, cg23825057 <dbl>, cg17949440 <dbl>, cg04098985 <dbl>,
## # cg16886987 <dbl>, cg22860917 <dbl>, cg21594328 <dbl>, cg23903035 <dbl>,
## # cg14393923 <dbl>, cg25103160 <dbl>, cg04640920 <dbl>, cg01522692 <dbl>,
## # cg23249922 <dbl>, cg15903956 <dbl>, cg10688297 <dbl>, cg07989490 <dbl>,
## # cg16090790 <dbl>, cg01519765 <dbl>, cg18444702 <dbl>, cg16404259 <dbl>,
## # cg12077460 <dbl>, cg22517735 <dbl>, cg01713086 <dbl>, cg16734734 <dbl>,
## # cg00886182 <dbl>, cg07891440 <dbl>, cg15715892 <dbl>, cg21368161 <dbl>,
## # cg03766264 <dbl>, ...
```
---
layout: false
class: inverse center middle text-white
# 3 essential components
## to every ggplot2 graph
### **Data**, **Geom**etry, **Aes**thetics
---
First step of every ggplot2 call is to *declare* the data.
.pull-left[
```r
*ggplot(data = geo_data)
```
]
.pull-right[
<img src="intro-to-gglot2_files/figure-html/our-first-plot-1-out-1.png" width="100%" height="100%" />
]
---
Then, we can assign variables in our data to different *aesthetics* of the plot.
.pull-left[
```r
ggplot(data = geo_data,
* aes(x = ga_weeks,
* y = cg20970886))
```
This is referred to as *aesthetic mapping*.
]
.pull-right[
<img src="intro-to-gglot2_files/figure-html/our-first-plot-2-out-1.png" width="100%" height="100%" />
]
---
Add **geometries (geoms)** to complete the plot.
.pull-left[
```r
ggplot(data = geo_data,
aes(x = ga_weeks,
y = cg20970886)) +
* geom_point()
```
Geoms are like saying what type of plot you want (e.g. scatterplot, boxplots, histograms... etc.)
]
.pull-right[
<img src="intro-to-gglot2_files/figure-html/our-first-plot-3-out-1.png" width="100%" height="100%" />
]
---
There are many *geoms*. Sometimes it makes sense to combine several.
.pull-left[
```r
ggplot(data = geo_data,
aes(x = ga_weeks,
y = cg20970886)) +
* geom_point() +
* geom_smooth(method = "lm")
```
]
.pull-right[
<img src="intro-to-gglot2_files/figure-html/our-first-plot-4-out-1.png" width="100%" height="100%" />
]
---
We can assign other variables to other aesthetics, e.g. color.
.pull-left[
```r
ggplot(data = geo_data,
aes(x = ga_weeks,
y = cg20970886,
* color = maternal_ethnicity)) +
geom_point() +
geom_smooth(method = "lm")
```
But note that this assigned maternal ethnicity to the color of both points and lines!
]
.pull-right[
<img src="intro-to-gglot2_files/figure-html/our-first-plot-5-out-1.png" width="100%" height="100%" />
]
---
To assign color exclusively to points (and not lines), put inside specific geom:
.pull-left[
```r
ggplot(data = geo_data,
aes(x = ga_weeks,
y = cg20970886)) +
* geom_point(aes(color = maternal_ethnicity)) +
geom_smooth(method = "lm")
```
]
.pull-right[
<img src="intro-to-gglot2_files/figure-html/our-first-plot-6-out-1.png" width="100%" height="100%" />
]
---
Can change the *shape* of points
.pull-left[
```r
ggplot(data = geo_data,
aes(x = ga_weeks,
y = cg20970886)) +
geom_point(aes(color = maternal_ethnicity),
* shape = 3) +
geom_smooth(method = "lm")
```
See [reference](https://ggplot2.tidyverse.org/reference/scale_shape.html) for complete list of shapes.
]
.pull-right[
<img src="intro-to-gglot2_files/figure-html/our-first-plot-6-2-out-1.png" width="100%" height="100%" />
]
---
A common mistake is to forget the aesthetic call.
.pull-left[
```r
ggplot(data = geo_data,
aes(x = ga_weeks,
y = cg20970886)) +
* geom_point(color = "blue",
shape = 3) +
geom_smooth(method = "lm")
```
]
.pull-right[
<img src="intro-to-gglot2_files/figure-html/our-first-plot-7-out-1.png" width="100%" height="100%" />
]
This assigns color to all the data
---
At this point, we've covered the 3 essential components to any ggplot2 plot:
1. **Data** - declare with a `ggplot(data = ...)` call
2. **Aesthetics** - assign input to plot components with `aes()`, e.g. (x/y position, color)
3. **Geoms** - declare the type of geometry, e.g. `+ geom_point()` for points
---
# There are so many geoms
Each geom has their own required aesthetics, and optional ones
- `geom_point` requires `x` and `y`, and that they be numeric variables
- `geom_boxplot` requires `x` and `y`, but `x` must be categorical
- `geom_histogram` and `geom_density` requires `x`
- `geom_text` requires `x`, `y`, and `text`
Check out [tidyverse site](https://ggplot2.tidyverse.org/reference/#section-geoms) for full list.
You can visit help pages for more information on a specific geom's options (e.g. `?geom_point`)
Now we know the basics, we can explore ways to customize our plots
---
.left-code[
```r
*ggplot(data = geo_data)
```
We'll start by looking at the methylation of this CpG site between preeclamptic and non-preeclamptic samples
First we declare the data.
]
.right-plot[
<img src="intro-to-gglot2_files/figure-html/fine-tune-1-1-out-1.png" width="100%" height="100%" />
]
PE: diagnosed with preeclampsia
---
.left-code[
```r
ggplot(data = geo_data,
* aes(x = diagnosis,
* y = cg20970886,
* fill = diagnosis))
```
Then we declare the mappings of our variables to aesthetics
]
.right-plot[
<img src="intro-to-gglot2_files/figure-html/fine-tune-1-2-out-1.png" width="100%" height="100%" />
]
PE: diagnosed with preeclampsia
---
.left-code[
```r
ggplot(data = geo_data,
aes(x = diagnosis,
y = cg20970886,
fill = diagnosis)) +
* geom_boxplot()
```
To specify we want boxplots, we use `geom_boxplot`
]
.right-plot[
<img src="intro-to-gglot2_files/figure-html/fine-tune-1-3-out-1.png" width="100%" height="100%" />
]
PE: diagnosed with preeclampsia
---
.left-code[
```r
ggplot(data = geo_data,
aes(x = diagnosis,
y = cg20970886,
fill = diagnosis)) +
geom_boxplot() +
* geom_point()
```
It can be informative to plot all individual data points over top of the boxplots.
To add individual data points, we simply add another geometry, `geom_point`
But it's a bit hard to see when the points overlap each other..
]
.right-plot[
<img src="intro-to-gglot2_files/figure-html/fine-tune-2-1-out-1.png" width="100%" height="100%" />
]
---
.left-code[
```r
ggplot(data = geo_data,
aes(x = diagnosis,
y = cg20970886,
fill = diagnosis)) +
geom_boxplot() +
* geom_jitter()
```
`geom_jitter` adds "noise" so that the points are spread out horizontally.
]
.right-plot[
<img src="intro-to-gglot2_files/figure-html/fine-tune-2-2-out-1.png" width="100%" height="100%" />
]
---
layout: false
class: inverse center middle text-white
# Customizing your graphs
# Scales and themes
---
# Scales
`aes` determines which data variables are mapped to each component of the graph
`scale_*_*` functions determine *how* this mapping is done
`scale_<aes>_<type>` calls all start with "`scale_`" followed by the target aesthetic (e.g. x, y, color, fill), and finished by the type (e.g. discrete, continuous).
For example,
Want to change the limits on the y-axis? where the ticks appear? or maybe change to a log scale? Use
`scale_y_continuous(limits = c(0,1))` or
`scale_y_log10()`
Want to change colors? Use
`scale_color_discrete()` for categorical variables
`scale_color_continuous()` for continuous variables
---
.left-code[
```r
ggplot(data = geo_data,
aes(x = diagnosis,
y = cg20970886,
fill = diagnosis)) +
geom_boxplot() +
geom_jitter() +
* scale_fill_manual(values = c("orange", "#7ED7F2"))
```
Here I assign specific colors to the categories of the diagnosis variable.
I supplied a vector of colors (can be in hex code) of same length of the number of categories of the variable `diagnosis`.
]
.right-plot[
<img src="intro-to-gglot2_files/figure-html/fine-tune-3-out-1.png" width="100%" height="100%" />
]
---
.left-code[
```r
ggplot(data = geo_data,
aes(x = diagnosis,
y = cg20970886,
fill = diagnosis)) +
geom_boxplot() +
geom_jitter() +
scale_fill_manual(values = c("orange", "#7ED7F2")) +
* scale_x_discrete(labels = c("Controls",
* "Cases"))
```
Here I change the labels of my x-axis.
]
.right-plot[
<img src="intro-to-gglot2_files/figure-html/fine-tune-4-1-out-1.png" width="100%" height="100%" />
]
---
.left-code[
```r
ggplot(data = geo_data,
aes(x = diagnosis,
y = cg20970886,
fill = diagnosis)) +
geom_boxplot() +
geom_jitter() +
scale_fill_manual(values = c("orange", "#7ED7F2")) +
* scale_x_discrete(labels = c("non-PE" = "Controls",
* "PE" = "Cases"))
```
It's better to be explicit about which label corresponds to which category
]
.right-plot[
<img src="intro-to-gglot2_files/figure-html/fine-tune-4-2-out-1.png" width="100%" height="100%" />
]
---
.left-code[
```r
ggplot(data = geo_data,
aes(x = diagnosis,
y = cg20970886,
fill = diagnosis)) +
geom_boxplot() +
geom_jitter() +
scale_fill_manual(values = c("orange", "#7ED7F2")) +
scale_x_discrete(labels = c("non-PE" = "Controls",
"PE" = "Cases")) +
* scale_y_continuous(limits = c(0, 1),
* breaks = c(0, 0.5, 1))
```
Here I expand the y axis to 0 and 1, the natural range of methylation.
I also change where I want the ticks (i.e. "breaks") to appear.
Note that the y axis is a numeric variable and x axis is categorical, and how the respective scale calls reflect that.
]
.right-plot[
<img src="intro-to-gglot2_files/figure-html/fine-tune-5-out-1.png" width="100%" height="100%" />
]
---
.left-code[
```r
ggplot(data = geo_data,
aes(x = diagnosis,
y = cg20970886,
fill = diagnosis)) +
geom_boxplot() +
geom_jitter() +
scale_fill_manual(values = c("orange", "#7ED7F2")) +
scale_x_discrete(labels = c("non-PE" = "Controls",
"PE" = "Cases")) +
scale_y_continuous(limits = c(0, 1),
breaks = c(0, 0.5, 1)) +
* theme(axis.text = element_text(colour = 'blue'))
```
The **`theme()`** function call allows for a customization of the non-data components of a plot. Things like the title, labels, font size, gridlines, etc.
Pull up `?theme` to see a full description of all options
]
.right-plot[
<img src="intro-to-gglot2_files/figure-html/fine-tune-6-out-1.png" width="100%" height="100%" />
]
---
.left-code[
```r
ggplot(data = geo_data,
aes(x = diagnosis,
y = cg20970886,
fill = diagnosis)) +
geom_boxplot() +
geom_jitter() +
scale_fill_manual(values = c("orange", "#7ED7F2")) +
scale_x_discrete(labels = c("non-PE" = "Controls",
"PE" = "Cases")) +
scale_y_continuous(limits = c(0, 1),
breaks = c(0, 0.5, 1)) +
* theme(axis.text = element_text(colour = 'blue'),
* panel.grid.major= element_line(colour = 'black'),
* panel.grid.minor = element_blank())
```
Most `theme()` arguments will require an "`element_*`" as input.
The type of element depends on the type of input (e.g. `element_text` for `axis.text`, `element_rect` for `panel.border`).
`element_blank` to remove components.
]
.right-plot[
<img src="intro-to-gglot2_files/figure-html/fine-tune-7-out-1.png" width="100%" height="100%" />
]
---
.left-code[
```r
ggplot(data = geo_data,
aes(x = diagnosis,
y = cg20970886,
fill = diagnosis)) +
geom_boxplot() +
geom_jitter() +
scale_fill_manual(values = c("orange", "#7ED7F2")) +
scale_x_discrete(labels = c("non-PE" = "Controls",
"PE" = "Cases")) +
scale_y_continuous(limits = c(0, 1),
breaks = c(0, 0.5, 1)) +
* theme_bw(base_size = 20)
```
There are some predefined themes that look nice and easy to use.
- `theme_gray` - default ggplot2 theme
- `theme_classic` - minimal with no gridlines
- `theme_bw` - clean look with white background
[List of complete ggplot2 themes](https://ggplot2.tidyverse.org/reference/ggtheme.html)
]
.right-plot[
<img src="intro-to-gglot2_files/figure-html/fine-tune-8-out-1.png" width="100%" height="100%" />
]
---
.left-code[
```r
ggplot(data = geo_data,
aes(x = diagnosis,
y = cg20970886,
fill = diagnosis)) +
geom_boxplot() +
geom_jitter() +
scale_fill_manual(values = c("orange", "#7ED7F2")) +
scale_x_discrete(labels = c("non-PE" = "Controls",
"PE" = "Cases")) +
scale_y_continuous(limits = c(0, 1),
breaks = c(0, 0.5, 1)) +
theme_bw(base_size = 20) +
* theme(legend.position = 'top')
```
You can customize these complete themes by calling `theme()` after e.g. `theme_bw()`
]
.right-plot[
<img src="intro-to-gglot2_files/figure-html/fine-tune-9-out-1.png" width="100%" height="100%" />
]
---
.left-code[
```r
*p <- ggplot(data = geo_data,
aes(x = diagnosis,
y = cg20970886,
fill = diagnosis)) +
geom_boxplot() +
geom_jitter() +
scale_fill_manual(values = c("orange", "#7ED7F2")) +
scale_x_discrete(labels = c("non-PE" = "Controls",
"PE" = "Cases")) +
scale_y_continuous(limits = c(0, 1),
breaks = c(0, 0.5, 1)) +
theme_bw(base_size = 20) +
theme(legend.position = 'top')
```
]
.right-plot[
There are a couple of options to save plots in R.
Probably the simplest way is to use `ggsave` from `ggplot2`.
First thing to do is to assign your plot into an object.
I assigned our plot to the object named `p`
]
---
.left-code[
```r
p <- ggplot(data = geo_data,
aes(x = diagnosis,
y = cg20970886,
fill = diagnosis)) +
geom_boxplot() +
geom_jitter() +
scale_fill_manual(values = c("orange", "#7ED7F2")) +
scale_x_discrete(labels = c("non-PE" = "Controls",
"PE" = "Cases")) +
scale_y_continuous(limits = c(0, 1),
breaks = c(0, 0.5, 1)) +
theme_bw(base_size = 20) +
theme(legend.position = 'top')
*ggsave(plot = p,
* filename = "this-plot.png",
* device = 'png',
* dpi = 72,
* height = 5,
* width = 7)
```
]
.right-plot[
Then we can call `ggsave` on object `p`.
I would recommend specifying the following options:
- `filename`, the name and location where you want the plot to be saved
- `device`, the type of image file (e.g. "pdf", "png", "tiff", etc...)
- `height`, `width` - determines the dimensions of your plot
- `dpi`, resolution
After you run the code, check your local directory for the png file.
]
---
# Resources
- Stack exchange for online help
- TOG study group / slack
- [Past TOG workshops](https://github.com/BCCHR-trainee-omics-group/StudyGroup)
- [ggplot2 extensions](https://exts.ggplot2.tidyverse.org/)
- [ggplot2 cheatsheet](https://www.rstudio.com/wp-content/uploads/2015/03/ggplot2-cheatsheet.pdf)
- [r 4 data science data visualization chapter](https://r4ds.had.co.nz/data-visualisation.html)
- [Eva Maerey's ggplot2 grammar guide](https://evamaerey.github.io/ggplot2_grammar_guide/about)
---
</textarea>
<style data-target="print-only">@media screen {.remark-slide-container{display:block;}.remark-slide-scaler{box-shadow:none;}}</style>
<script src="https://remarkjs.com/downloads/remark-latest.min.js"></script>
<script>var slideshow = remark.create({
"ratio": "16:9",
"highlightStyle": "github",
"highlightLines": true,
"countIncrementalSlides": false
});
if (window.HTMLWidgets) slideshow.on('afterShowSlide', function (slide) {
window.dispatchEvent(new Event('resize'));
});
(function(d) {
var s = d.createElement("style"), r = d.querySelector(".remark-slide-scaler");
if (!r) return;
s.type = "text/css"; s.innerHTML = "@page {size: " + r.style.width + " " + r.style.height +"; }";
d.head.appendChild(s);
})(document);
(function(d) {
var el = d.getElementsByClassName("remark-slides-area");
if (!el) return;
var slide, slides = slideshow.getSlides(), els = el[0].children;
for (var i = 1; i < slides.length; i++) {
slide = slides[i];
if (slide.properties.continued === "true" || slide.properties.count === "false") {
els[i - 1].className += ' has-continuation';
}
}
var s = d.createElement("style");
s.type = "text/css"; s.innerHTML = "@media print { .has-continuation { display: none; } }";
d.head.appendChild(s);
})(document);
// delete the temporary CSS (for displaying all slides initially) when the user
// starts to view slides
(function() {
var deleted = false;
slideshow.on('beforeShowSlide', function(slide) {
if (deleted) return;
var sheets = document.styleSheets, node;
for (var i = 0; i < sheets.length; i++) {
node = sheets[i].ownerNode;
if (node.dataset["target"] !== "print-only") continue;
node.parentNode.removeChild(node);
}
deleted = true;
});
})();
(function() {
"use strict"
// Replace <script> tags in slides area to make them executable
var scripts = document.querySelectorAll(
'.remark-slides-area .remark-slide-container script'
);
if (!scripts.length) return;
for (var i = 0; i < scripts.length; i++) {
var s = document.createElement('script');
var code = document.createTextNode(scripts[i].textContent);
s.appendChild(code);
var scriptAttrs = scripts[i].attributes;
for (var j = 0; j < scriptAttrs.length; j++) {
s.setAttribute(scriptAttrs[j].name, scriptAttrs[j].value);
}
scripts[i].parentElement.replaceChild(s, scripts[i]);
}
})();
(function() {
var links = document.getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
if (/^(https?:)?\/\//.test(links[i].getAttribute('href'))) {
links[i].target = '_blank';
}
}
})();
// adds .remark-code-has-line-highlighted class to <pre> parent elements
// of code chunks containing highlighted lines with class .remark-code-line-highlighted
(function(d) {
const hlines = d.querySelectorAll('.remark-code-line-highlighted');
const preParents = [];
const findPreParent = function(line, p = 0) {
if (p > 1) return null; // traverse up no further than grandparent
const el = line.parentElement;
return el.tagName === "PRE" ? el : findPreParent(el, ++p);
};
for (let line of hlines) {
let pre = findPreParent(line);
if (pre && !preParents.includes(pre)) preParents.push(pre);
}
preParents.forEach(p => p.classList.add("remark-code-has-line-highlighted"));
})(document);</script>
<script>
slideshow._releaseMath = function(el) {
var i, text, code, codes = el.getElementsByTagName('code');
for (i = 0; i < codes.length;) {
code = codes[i];
if (code.parentNode.tagName !== 'PRE' && code.childElementCount === 0) {
text = code.textContent;
if (/^\\\((.|\s)+\\\)$/.test(text) || /^\\\[(.|\s)+\\\]$/.test(text) ||
/^\$\$(.|\s)+\$\$$/.test(text) ||
/^\\begin\{([^}]+)\}(.|\s)+\\end\{[^}]+\}$/.test(text)) {
code.outerHTML = code.innerHTML; // remove <code></code>
continue;
}
}
i++;
}
};
slideshow._releaseMath(document);
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML';
if (location.protocol !== 'file:' && /^https?:/.test(script.src))
script.src = script.src.replace(/^https?:/, '');
document.getElementsByTagName('head')[0].appendChild(script);
})();
</script>
</body>
</html>