-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtutorial-data-visualization-with-ggplot.html
1042 lines (1002 loc) · 122 KB
/
tutorial-data-visualization-with-ggplot.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
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title> 5 Tutorial: Data visualization with ggplot | R for PR evaluation</title>
<meta name="description" content="Theories, models, methods and objects of controlling - M.A. Seminar at the IfKW, SS 2022" />
<meta name="generator" content="bookdown 0.27 and GitBook 2.6.7" />
<meta property="og:title" content=" 5 Tutorial: Data visualization with ggplot | R for PR evaluation" />
<meta property="og:type" content="book" />
<meta property="og:description" content="Theories, models, methods and objects of controlling - M.A. Seminar at the IfKW, SS 2022" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content=" 5 Tutorial: Data visualization with ggplot | R for PR evaluation" />
<meta name="twitter:description" content="Theories, models, methods and objects of controlling - M.A. Seminar at the IfKW, SS 2022" />
<meta name="author" content="Lara Kobilke, IfKW, Ludwig-Maximilians-Universität München" />
<meta name="date" content="2022-07-20" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<link rel="prev" href="exercise-2-test-your-knowledge.html"/>
<link rel="next" href="exercise-3-test-your-knowledge.html"/>
<script src="libs/jquery-3.6.0/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/fuse.js@6.4.6/dist/fuse.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-clipboard.css" rel="stylesheet" />
<link href="libs/anchor-sections-1.1.0/anchor-sections.css" rel="stylesheet" />
<link href="libs/anchor-sections-1.1.0/anchor-sections-hash.css" rel="stylesheet" />
<script src="libs/anchor-sections-1.1.0/anchor-sections.js"></script>
<style type="text/css">
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">
<div class="book-summary">
<nav role="navigation">
<ul class="summary">
<li><a href="./">Evaluation</a></li>
<li class="divider"></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i>General information on the course</a>
<ul>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html#what-can-i-learn-from-this-tutorial"><i class="fa fa-check"></i>What can I learn from this tutorial?</a></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html#what-can-i-do-if-i-have-a-question"><i class="fa fa-check"></i>What can I do if I have a question?</a></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html#what-can-i-do-if-i-have-problems-with-my-r-code"><i class="fa fa-check"></i>What can I do if I have problems with my R code?</a></li>
</ul></li>
<li class="chapter" data-level="1" data-path="tutorial-installing-understanding-rr-studio.html"><a href="tutorial-installing-understanding-rr-studio.html"><i class="fa fa-check"></i><b>1</b> Tutorial: Installing & Understanding R/R Studio</a>
<ul>
<li class="chapter" data-level="1.1" data-path="tutorial-installing-understanding-rr-studio.html"><a href="tutorial-installing-understanding-rr-studio.html#installing-r"><i class="fa fa-check"></i><b>1.1</b> Installing R</a></li>
<li class="chapter" data-level="1.2" data-path="tutorial-installing-understanding-rr-studio.html"><a href="tutorial-installing-understanding-rr-studio.html#installing-r-studio"><i class="fa fa-check"></i><b>1.2</b> Installing R Studio</a></li>
<li class="chapter" data-level="1.3" data-path="tutorial-installing-understanding-rr-studio.html"><a href="tutorial-installing-understanding-rr-studio.html#updating-r-and-r-studio"><i class="fa fa-check"></i><b>1.3</b> Updating R and R Studio</a>
<ul>
<li class="chapter" data-level="1.3.1" data-path="tutorial-installing-understanding-rr-studio.html"><a href="tutorial-installing-understanding-rr-studio.html#on-windows"><i class="fa fa-check"></i><b>1.3.1</b> On Windows</a></li>
<li class="chapter" data-level="1.3.2" data-path="tutorial-installing-understanding-rr-studio.html"><a href="tutorial-installing-understanding-rr-studio.html#on-mac"><i class="fa fa-check"></i><b>1.3.2</b> On MAC</a></li>
</ul></li>
<li class="chapter" data-level="1.4" data-path="tutorial-installing-understanding-rr-studio.html"><a href="tutorial-installing-understanding-rr-studio.html#how-does-r-work"><i class="fa fa-check"></i><b>1.4</b> How does R work?</a></li>
<li class="chapter" data-level="1.5" data-path="tutorial-installing-understanding-rr-studio.html"><a href="tutorial-installing-understanding-rr-studio.html#why-should-i-use-r"><i class="fa fa-check"></i><b>1.5</b> Why should I use R?</a></li>
<li class="chapter" data-level="1.6" data-path="tutorial-installing-understanding-rr-studio.html"><a href="tutorial-installing-understanding-rr-studio.html#how-does-r-studio-work"><i class="fa fa-check"></i><b>1.6</b> How does R Studio work?</a>
<ul>
<li class="chapter" data-level="1.6.1" data-path="tutorial-installing-understanding-rr-studio.html"><a href="tutorial-installing-understanding-rr-studio.html#source-writing-your-own-code"><i class="fa fa-check"></i><b>1.6.1</b> Source: Writing your own code</a></li>
<li class="chapter" data-level="1.6.2" data-path="tutorial-installing-understanding-rr-studio.html"><a href="tutorial-installing-understanding-rr-studio.html#console-printing-results"><i class="fa fa-check"></i><b>1.6.2</b> Console: Printing results</a></li>
<li class="chapter" data-level="1.6.3" data-path="tutorial-installing-understanding-rr-studio.html"><a href="tutorial-installing-understanding-rr-studio.html#environment-overview-of-objects"><i class="fa fa-check"></i><b>1.6.3</b> Environment: Overview of objects</a></li>
<li class="chapter" data-level="1.6.4" data-path="tutorial-installing-understanding-rr-studio.html"><a href="tutorial-installing-understanding-rr-studio.html#plotshelppackages-do-everything-else"><i class="fa fa-check"></i><b>1.6.4</b> Plots/Help/Packages: Do everything else</a></li>
</ul></li>
<li class="chapter" data-level="1.7" data-path="tutorial-installing-understanding-rr-studio.html"><a href="tutorial-installing-understanding-rr-studio.html#packages"><i class="fa fa-check"></i><b>1.7</b> Packages</a>
<ul>
<li class="chapter" data-level="1.7.1" data-path="tutorial-installing-understanding-rr-studio.html"><a href="tutorial-installing-understanding-rr-studio.html#installing-packages"><i class="fa fa-check"></i><b>1.7.1</b> Installing packages</a></li>
<li class="chapter" data-level="1.7.2" data-path="tutorial-installing-understanding-rr-studio.html"><a href="tutorial-installing-understanding-rr-studio.html#activating-packages"><i class="fa fa-check"></i><b>1.7.2</b> Activating packages</a></li>
<li class="chapter" data-level="1.7.3" data-path="tutorial-installing-understanding-rr-studio.html"><a href="tutorial-installing-understanding-rr-studio.html#getting-information-about-packages"><i class="fa fa-check"></i><b>1.7.3</b> Getting information about packages</a></li>
</ul></li>
<li class="chapter" data-level="1.8" data-path="tutorial-installing-understanding-rr-studio.html"><a href="tutorial-installing-understanding-rr-studio.html#take-aways"><i class="fa fa-check"></i><b>1.8</b> Take-Aways</a></li>
<li class="chapter" data-level="1.9" data-path="tutorial-installing-understanding-rr-studio.html"><a href="tutorial-installing-understanding-rr-studio.html#additional-tutorials"><i class="fa fa-check"></i><b>1.9</b> Additional tutorials</a></li>
</ul></li>
<li class="chapter" data-level="2" data-path="tutorial-using-r-as-a-calculator.html"><a href="tutorial-using-r-as-a-calculator.html"><i class="fa fa-check"></i><b>2</b> Tutorial: Using R as a calculator</a>
<ul>
<li class="chapter" data-level="2.1" data-path="tutorial-using-r-as-a-calculator.html"><a href="tutorial-using-r-as-a-calculator.html#using-variables-for-calculation"><i class="fa fa-check"></i><b>2.1</b> Using variables for calculation</a></li>
<li class="chapter" data-level="2.2" data-path="tutorial-using-r-as-a-calculator.html"><a href="tutorial-using-r-as-a-calculator.html#using-vectors-for-calculation"><i class="fa fa-check"></i><b>2.2</b> Using vectors for calculation</a></li>
<li class="chapter" data-level="2.3" data-path="tutorial-using-r-as-a-calculator.html"><a href="tutorial-using-r-as-a-calculator.html#selecting-values-from-a-vector"><i class="fa fa-check"></i><b>2.3</b> Selecting values from a vector</a></li>
<li class="chapter" data-level="2.4" data-path="tutorial-using-r-as-a-calculator.html"><a href="tutorial-using-r-as-a-calculator.html#take-aways-1"><i class="fa fa-check"></i><b>2.4</b> Take-Aways</a></li>
<li class="chapter" data-level="2.5" data-path="tutorial-using-r-as-a-calculator.html"><a href="tutorial-using-r-as-a-calculator.html#additional-tutorials-1"><i class="fa fa-check"></i><b>2.5</b> Additional tutorials</a></li>
</ul></li>
<li class="chapter" data-level="3" data-path="tutorial-working-with-data-files.html"><a href="tutorial-working-with-data-files.html"><i class="fa fa-check"></i><b>3</b> Tutorial: Working with data (files)</a>
<ul>
<li class="chapter" data-level="3.1" data-path="tutorial-working-with-data-files.html"><a href="tutorial-working-with-data-files.html#defining-your-working-directory"><i class="fa fa-check"></i><b>3.1</b> Defining your working directory</a>
<ul>
<li class="chapter" data-level="3.1.1" data-path="tutorial-working-with-data-files.html"><a href="tutorial-working-with-data-files.html#optional-setting-the-working-directory-on-a-remote-desktop"><i class="fa fa-check"></i><b>3.1.1</b> Optional: Setting the working directory on a remote desktop</a></li>
</ul></li>
<li class="chapter" data-level="3.2" data-path="tutorial-working-with-data-files.html"><a href="tutorial-working-with-data-files.html#import-data-from-your-working-directory"><i class="fa fa-check"></i><b>3.2</b> Import data from your working directory</a></li>
<li class="chapter" data-level="3.3" data-path="tutorial-working-with-data-files.html"><a href="tutorial-working-with-data-files.html#subsetting-variables-columns-in-data-frames"><i class="fa fa-check"></i><b>3.3</b> Subsetting variables / columns in data frames</a></li>
<li class="chapter" data-level="3.4" data-path="tutorial-working-with-data-files.html"><a href="tutorial-working-with-data-files.html#subsetting-observations-rows-in-data-frames"><i class="fa fa-check"></i><b>3.4</b> Subsetting observations / rows in data frames</a></li>
<li class="chapter" data-level="3.5" data-path="tutorial-working-with-data-files.html"><a href="tutorial-working-with-data-files.html#subsetting-values-cells-in-data-frames"><i class="fa fa-check"></i><b>3.5</b> Subsetting values / cells in data frames</a></li>
<li class="chapter" data-level="3.6" data-path="tutorial-working-with-data-files.html"><a href="tutorial-working-with-data-files.html#subsetting-data-with-conditions"><i class="fa fa-check"></i><b>3.6</b> Subsetting data with conditions</a></li>
<li class="chapter" data-level="3.7" data-path="tutorial-working-with-data-files.html"><a href="tutorial-working-with-data-files.html#take-aways-2"><i class="fa fa-check"></i><b>3.7</b> Take-Aways</a></li>
<li class="chapter" data-level="3.8" data-path="tutorial-working-with-data-files.html"><a href="tutorial-working-with-data-files.html#additional-tutorials-2"><i class="fa fa-check"></i><b>3.8</b> Additional tutorials</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="exercise-1.html"><a href="exercise-1.html"><i class="fa fa-check"></i>Exercise 1</a>
<ul>
<li class="chapter" data-level="" data-path="exercise-1.html"><a href="exercise-1.html#task-1"><i class="fa fa-check"></i>Task 1</a></li>
<li class="chapter" data-level="" data-path="exercise-1.html"><a href="exercise-1.html#task-2"><i class="fa fa-check"></i>Task 2</a></li>
<li class="chapter" data-level="" data-path="exercise-1.html"><a href="exercise-1.html#task-3"><i class="fa fa-check"></i>Task 3</a></li>
<li class="chapter" data-level="" data-path="exercise-1.html"><a href="exercise-1.html#task-4"><i class="fa fa-check"></i>Task 4</a></li>
<li class="chapter" data-level="" data-path="exercise-1.html"><a href="exercise-1.html#task-5"><i class="fa fa-check"></i>Task 5</a></li>
<li class="chapter" data-level="" data-path="exercise-1.html"><a href="exercise-1.html#task-6"><i class="fa fa-check"></i>Task 6</a></li>
</ul></li>
<li class="chapter" data-level="4" data-path="tutorial-data-management-with-tidyverse.html"><a href="tutorial-data-management-with-tidyverse.html"><i class="fa fa-check"></i><b>4</b> Tutorial: Data management with tidyverse</a>
<ul>
<li class="chapter" data-level="4.1" data-path="tutorial-data-management-with-tidyverse.html"><a href="tutorial-data-management-with-tidyverse.html#why-not-stick-with-base-r"><i class="fa fa-check"></i><b>4.1</b> Why not stick with Base R?</a></li>
<li class="chapter" data-level="4.2" data-path="tutorial-data-management-with-tidyverse.html"><a href="tutorial-data-management-with-tidyverse.html#tidyverse-packages"><i class="fa fa-check"></i><b>4.2</b> Tidyverse packages</a></li>
<li class="chapter" data-level="4.3" data-path="tutorial-data-management-with-tidyverse.html"><a href="tutorial-data-management-with-tidyverse.html#tidy-data"><i class="fa fa-check"></i><b>4.3</b> Tidy data</a></li>
<li class="chapter" data-level="4.4" data-path="tutorial-data-management-with-tidyverse.html"><a href="tutorial-data-management-with-tidyverse.html#the-pipe-operator"><i class="fa fa-check"></i><b>4.4</b> The pipe operator</a></li>
<li class="chapter" data-level="4.5" data-path="tutorial-data-management-with-tidyverse.html"><a href="tutorial-data-management-with-tidyverse.html#data-transformation-with-dplyr"><i class="fa fa-check"></i><b>4.5</b> Data transformation with dplyr</a>
<ul>
<li class="chapter" data-level="4.5.1" data-path="tutorial-data-management-with-tidyverse.html"><a href="tutorial-data-management-with-tidyverse.html#select"><i class="fa fa-check"></i><b>4.5.1</b> select()</a></li>
<li class="chapter" data-level="4.5.2" data-path="tutorial-data-management-with-tidyverse.html"><a href="tutorial-data-management-with-tidyverse.html#filter"><i class="fa fa-check"></i><b>4.5.2</b> filter()</a></li>
<li class="chapter" data-level="4.5.3" data-path="tutorial-data-management-with-tidyverse.html"><a href="tutorial-data-management-with-tidyverse.html#arrange"><i class="fa fa-check"></i><b>4.5.3</b> arrange()</a></li>
<li class="chapter" data-level="4.5.4" data-path="tutorial-data-management-with-tidyverse.html"><a href="tutorial-data-management-with-tidyverse.html#mutate"><i class="fa fa-check"></i><b>4.5.4</b> mutate()</a></li>
<li class="chapter" data-level="4.5.5" data-path="tutorial-data-management-with-tidyverse.html"><a href="tutorial-data-management-with-tidyverse.html#summarize-group_by"><i class="fa fa-check"></i><b>4.5.5</b> summarize() [+ group_by()]</a></li>
<li class="chapter" data-level="4.5.6" data-path="tutorial-data-management-with-tidyverse.html"><a href="tutorial-data-management-with-tidyverse.html#chaining-functions-in-a-pipe"><i class="fa fa-check"></i><b>4.5.6</b> Chaining functions in a pipe</a></li>
</ul></li>
<li class="chapter" data-level="4.6" data-path="tutorial-data-management-with-tidyverse.html"><a href="tutorial-data-management-with-tidyverse.html#take-aways-3"><i class="fa fa-check"></i><b>4.6</b> Take-Aways</a></li>
<li class="chapter" data-level="4.7" data-path="tutorial-data-management-with-tidyverse.html"><a href="tutorial-data-management-with-tidyverse.html#additional-tutorials-3"><i class="fa fa-check"></i><b>4.7</b> Additional tutorials</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="exercise-2-test-your-knowledge.html"><a href="exercise-2-test-your-knowledge.html"><i class="fa fa-check"></i>Exercise 2: Test your knowledge</a>
<ul>
<li class="chapter" data-level="" data-path="exercise-2-test-your-knowledge.html"><a href="exercise-2-test-your-knowledge.html#task-1-1"><i class="fa fa-check"></i>Task 1</a></li>
<li class="chapter" data-level="" data-path="exercise-2-test-your-knowledge.html"><a href="exercise-2-test-your-knowledge.html#task-2-1"><i class="fa fa-check"></i>Task 2</a></li>
<li class="chapter" data-level="" data-path="exercise-2-test-your-knowledge.html"><a href="exercise-2-test-your-knowledge.html#task-3-1"><i class="fa fa-check"></i>Task 3</a></li>
<li class="chapter" data-level="" data-path="exercise-2-test-your-knowledge.html"><a href="exercise-2-test-your-knowledge.html#task-4-1"><i class="fa fa-check"></i>Task 4</a></li>
<li class="chapter" data-level="" data-path="exercise-2-test-your-knowledge.html"><a href="exercise-2-test-your-knowledge.html#task-5-1"><i class="fa fa-check"></i>Task 5</a></li>
<li class="chapter" data-level="" data-path="exercise-2-test-your-knowledge.html"><a href="exercise-2-test-your-knowledge.html#task-6-1"><i class="fa fa-check"></i>Task 6</a></li>
<li class="chapter" data-level="" data-path="exercise-2-test-your-knowledge.html"><a href="exercise-2-test-your-knowledge.html#task-7"><i class="fa fa-check"></i>Task 7</a></li>
</ul></li>
<li class="chapter" data-level="5" data-path="tutorial-data-visualization-with-ggplot.html"><a href="tutorial-data-visualization-with-ggplot.html"><i class="fa fa-check"></i><b>5</b> Tutorial: Data visualization with ggplot</a>
<ul>
<li class="chapter" data-level="5.1" data-path="tutorial-data-visualization-with-ggplot.html"><a href="tutorial-data-visualization-with-ggplot.html#why-not-stick-with-base-r-1"><i class="fa fa-check"></i><b>5.1</b> Why not stick with Base R?</a></li>
<li class="chapter" data-level="5.2" data-path="tutorial-data-visualization-with-ggplot.html"><a href="tutorial-data-visualization-with-ggplot.html#components-of-a-ggplot-graph"><i class="fa fa-check"></i><b>5.2</b> Components of a ggplot graph</a></li>
<li class="chapter" data-level="5.3" data-path="tutorial-data-visualization-with-ggplot.html"><a href="tutorial-data-visualization-with-ggplot.html#installing-activating-ggplot"><i class="fa fa-check"></i><b>5.3</b> Installing & activating ggplot</a></li>
<li class="chapter" data-level="5.4" data-path="tutorial-data-visualization-with-ggplot.html"><a href="tutorial-data-visualization-with-ggplot.html#building-your-first-plot"><i class="fa fa-check"></i><b>5.4</b> Building your first plot</a>
<ul>
<li class="chapter" data-level="5.4.1" data-path="tutorial-data-visualization-with-ggplot.html"><a href="tutorial-data-visualization-with-ggplot.html#data"><i class="fa fa-check"></i><b>5.4.1</b> Data</a></li>
<li class="chapter" data-level="5.4.2" data-path="tutorial-data-visualization-with-ggplot.html"><a href="tutorial-data-visualization-with-ggplot.html#aesthetics"><i class="fa fa-check"></i><b>5.4.2</b> Aesthetics</a></li>
<li class="chapter" data-level="5.4.3" data-path="tutorial-data-visualization-with-ggplot.html"><a href="tutorial-data-visualization-with-ggplot.html#geometrics"><i class="fa fa-check"></i><b>5.4.3</b> Geometrics</a></li>
<li class="chapter" data-level="5.4.4" data-path="tutorial-data-visualization-with-ggplot.html"><a href="tutorial-data-visualization-with-ggplot.html#scales"><i class="fa fa-check"></i><b>5.4.4</b> Scales</a></li>
<li class="chapter" data-level="5.4.5" data-path="tutorial-data-visualization-with-ggplot.html"><a href="tutorial-data-visualization-with-ggplot.html#themes"><i class="fa fa-check"></i><b>5.4.5</b> Themes</a></li>
<li class="chapter" data-level="5.4.6" data-path="tutorial-data-visualization-with-ggplot.html"><a href="tutorial-data-visualization-with-ggplot.html#labs"><i class="fa fa-check"></i><b>5.4.6</b> Labs</a></li>
<li class="chapter" data-level="5.4.7" data-path="tutorial-data-visualization-with-ggplot.html"><a href="tutorial-data-visualization-with-ggplot.html#facets"><i class="fa fa-check"></i><b>5.4.7</b> Facets</a></li>
<li class="chapter" data-level="5.4.8" data-path="tutorial-data-visualization-with-ggplot.html"><a href="tutorial-data-visualization-with-ggplot.html#saving-graphs"><i class="fa fa-check"></i><b>5.4.8</b> Saving graphs</a></li>
</ul></li>
<li class="chapter" data-level="5.5" data-path="tutorial-data-visualization-with-ggplot.html"><a href="tutorial-data-visualization-with-ggplot.html#other-common-plot-types"><i class="fa fa-check"></i><b>5.5</b> Other common plot types</a>
<ul>
<li class="chapter" data-level="5.5.1" data-path="tutorial-data-visualization-with-ggplot.html"><a href="tutorial-data-visualization-with-ggplot.html#bar-plots"><i class="fa fa-check"></i><b>5.5.1</b> bar plots</a></li>
<li class="chapter" data-level="5.5.2" data-path="tutorial-data-visualization-with-ggplot.html"><a href="tutorial-data-visualization-with-ggplot.html#box-plots"><i class="fa fa-check"></i><b>5.5.2</b> box plots</a></li>
</ul></li>
<li class="chapter" data-level="5.6" data-path="tutorial-data-visualization-with-ggplot.html"><a href="tutorial-data-visualization-with-ggplot.html#take-aways-4"><i class="fa fa-check"></i><b>5.6</b> Take Aways</a></li>
<li class="chapter" data-level="5.7" data-path="tutorial-data-visualization-with-ggplot.html"><a href="tutorial-data-visualization-with-ggplot.html#additional-tutorials-4"><i class="fa fa-check"></i><b>5.7</b> Additional tutorials</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="exercise-3-test-your-knowledge.html"><a href="exercise-3-test-your-knowledge.html"><i class="fa fa-check"></i>Exercise 3: Test your knowledge</a>
<ul>
<li class="chapter" data-level="" data-path="exercise-3-test-your-knowledge.html"><a href="exercise-3-test-your-knowledge.html#task-1-2"><i class="fa fa-check"></i>Task 1</a></li>
<li class="chapter" data-level="" data-path="exercise-3-test-your-knowledge.html"><a href="exercise-3-test-your-knowledge.html#task-2-2"><i class="fa fa-check"></i>Task 2</a></li>
<li class="chapter" data-level="" data-path="exercise-3-test-your-knowledge.html"><a href="exercise-3-test-your-knowledge.html#task-3-2"><i class="fa fa-check"></i>Task 3</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="exercise-4-test-your-knowledge.html"><a href="exercise-4-test-your-knowledge.html"><i class="fa fa-check"></i>Exercise 4: Test your knowledge</a>
<ul>
<li class="chapter" data-level="" data-path="exercise-4-test-your-knowledge.html"><a href="exercise-4-test-your-knowledge.html#task-1-3"><i class="fa fa-check"></i>Task 1</a></li>
<li class="chapter" data-level="" data-path="exercise-4-test-your-knowledge.html"><a href="exercise-4-test-your-knowledge.html#task-2-3"><i class="fa fa-check"></i>Task 2</a></li>
<li class="chapter" data-level="" data-path="exercise-4-test-your-knowledge.html"><a href="exercise-4-test-your-knowledge.html#task-3-3"><i class="fa fa-check"></i>Task 3</a></li>
<li class="chapter" data-level="" data-path="exercise-4-test-your-knowledge.html"><a href="exercise-4-test-your-knowledge.html#task-4-2"><i class="fa fa-check"></i>Task 4</a></li>
<li class="chapter" data-level="" data-path="exercise-4-test-your-knowledge.html"><a href="exercise-4-test-your-knowledge.html#task-5-2"><i class="fa fa-check"></i>Task 5</a></li>
<li class="chapter" data-level="" data-path="exercise-4-test-your-knowledge.html"><a href="exercise-4-test-your-knowledge.html#task-6-2"><i class="fa fa-check"></i>Task 6</a></li>
<li class="chapter" data-level="" data-path="exercise-4-test-your-knowledge.html"><a href="exercise-4-test-your-knowledge.html#task-7-1"><i class="fa fa-check"></i>Task 7</a></li>
<li class="chapter" data-level="" data-path="exercise-4-test-your-knowledge.html"><a href="exercise-4-test-your-knowledge.html#task-8"><i class="fa fa-check"></i>Task 8</a></li>
</ul></li>
<li class="chapter" data-level="6" data-path="tutorial-text-manipulation-with-stringr.html"><a href="tutorial-text-manipulation-with-stringr.html"><i class="fa fa-check"></i><b>6</b> Tutorial: Text manipulation with stringr</a>
<ul>
<li class="chapter" data-level="6.1" data-path="tutorial-text-manipulation-with-stringr.html"><a href="tutorial-text-manipulation-with-stringr.html#whats-stringr"><i class="fa fa-check"></i><b>6.1</b> What’s stringr?</a></li>
<li class="chapter" data-level="6.2" data-path="tutorial-text-manipulation-with-stringr.html"><a href="tutorial-text-manipulation-with-stringr.html#working-with-strings"><i class="fa fa-check"></i><b>6.2</b> Working with strings</a></li>
<li class="chapter" data-level="6.3" data-path="tutorial-text-manipulation-with-stringr.html"><a href="tutorial-text-manipulation-with-stringr.html#working-with-string-patterns"><i class="fa fa-check"></i><b>6.3</b> Working with string patterns</a></li>
<li class="chapter" data-level="6.4" data-path="tutorial-text-manipulation-with-stringr.html"><a href="tutorial-text-manipulation-with-stringr.html#working-with-regular-expressions"><i class="fa fa-check"></i><b>6.4</b> Working with regular expressions</a></li>
<li class="chapter" data-level="6.5" data-path="tutorial-text-manipulation-with-stringr.html"><a href="tutorial-text-manipulation-with-stringr.html#take-aways-5"><i class="fa fa-check"></i><b>6.5</b> Take-Aways</a></li>
<li class="chapter" data-level="6.6" data-path="tutorial-text-manipulation-with-stringr.html"><a href="tutorial-text-manipulation-with-stringr.html#additional-tutorials-5"><i class="fa fa-check"></i><b>6.6</b> Additional tutorials</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="exercise-5-test-your-knowledge.html"><a href="exercise-5-test-your-knowledge.html"><i class="fa fa-check"></i>Exercise 5: Test your knowledge</a>
<ul>
<li class="chapter" data-level="" data-path="exercise-5-test-your-knowledge.html"><a href="exercise-5-test-your-knowledge.html#task-1-4"><i class="fa fa-check"></i>Task 1</a></li>
<li class="chapter" data-level="" data-path="exercise-5-test-your-knowledge.html"><a href="exercise-5-test-your-knowledge.html#task-2-4"><i class="fa fa-check"></i>Task 2</a></li>
<li class="chapter" data-level="" data-path="exercise-5-test-your-knowledge.html"><a href="exercise-5-test-your-knowledge.html#task-3-4"><i class="fa fa-check"></i>Task 3</a></li>
<li class="chapter" data-level="" data-path="exercise-5-test-your-knowledge.html"><a href="exercise-5-test-your-knowledge.html#task-4-3"><i class="fa fa-check"></i>Task 4</a></li>
<li class="chapter" data-level="" data-path="exercise-5-test-your-knowledge.html"><a href="exercise-5-test-your-knowledge.html#task-5-3"><i class="fa fa-check"></i>Task 5</a></li>
<li class="chapter" data-level="" data-path="exercise-5-test-your-knowledge.html"><a href="exercise-5-test-your-knowledge.html#task-6-3"><i class="fa fa-check"></i>Task 6</a></li>
<li class="chapter" data-level="" data-path="exercise-5-test-your-knowledge.html"><a href="exercise-5-test-your-knowledge.html#task-7-2"><i class="fa fa-check"></i>Task 7</a></li>
</ul></li>
<li class="chapter" data-level="7" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html"><i class="fa fa-check"></i><b>7</b> Tidy text analysis</a>
<ul>
<li class="chapter" data-level="7.1" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#what-is-tidy-text"><i class="fa fa-check"></i><b>7.1</b> What is tidy text?</a></li>
<li class="chapter" data-level="7.2" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#preprocessing-real-world-data"><i class="fa fa-check"></i><b>7.2</b> Preprocessing real-world data</a>
<ul>
<li class="chapter" data-level="7.2.1" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#tokenization"><i class="fa fa-check"></i><b>7.2.1</b> Tokenization</a></li>
<li class="chapter" data-level="" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#practice-tokenization"><i class="fa fa-check"></i>Practice: Tokenization</a></li>
<li class="chapter" data-level="7.2.2" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#stop-word-removal"><i class="fa fa-check"></i><b>7.2.2</b> Stop word removal</a></li>
<li class="chapter" data-level="" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#practice-stop-word-removal"><i class="fa fa-check"></i>Practice: Stop word removal</a></li>
<li class="chapter" data-level="7.2.3" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#lemmatizing-stemming"><i class="fa fa-check"></i><b>7.2.3</b> Lemmatizing & stemming</a></li>
<li class="chapter" data-level="" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#practice-lemmatizing-stemming"><i class="fa fa-check"></i>Practice: Lemmatizing & stemming</a></li>
<li class="chapter" data-level="7.2.4" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#pruning"><i class="fa fa-check"></i><b>7.2.4</b> Pruning</a></li>
<li class="chapter" data-level="" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#practice-pruning"><i class="fa fa-check"></i>Practice: Pruning</a></li>
</ul></li>
<li class="chapter" data-level="7.3" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#relative-word-frequencies"><i class="fa fa-check"></i><b>7.3</b> (Relative) word frequencies</a>
<ul>
<li class="chapter" data-level="7.3.1" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#advanced-word-frequency-comparisons"><i class="fa fa-check"></i><b>7.3.1</b> Advanced: Word frequency comparisons</a></li>
<li class="chapter" data-level="7.3.2" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#advanced-word-log-odds"><i class="fa fa-check"></i><b>7.3.2</b> Advanced: Word log odds</a></li>
</ul></li>
<li class="chapter" data-level="7.4" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#topic-modeling"><i class="fa fa-check"></i><b>7.4</b> Topic modeling</a>
<ul>
<li class="chapter" data-level="7.4.1" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#first-steps"><i class="fa fa-check"></i><b>7.4.1</b> First steps</a></li>
<li class="chapter" data-level="7.4.2" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#model-estimation-number-of-topics"><i class="fa fa-check"></i><b>7.4.2</b> Model estimation: Number of topics</a></li>
<li class="chapter" data-level="" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#practice-model-estimation"><i class="fa fa-check"></i>Practice: Model estimation</a></li>
<li class="chapter" data-level="7.4.3" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#inspect-the-topics"><i class="fa fa-check"></i><b>7.4.3</b> Inspect the topics</a></li>
<li class="chapter" data-level="7.4.4" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#stms"><i class="fa fa-check"></i><b>7.4.4</b> STMs</a></li>
</ul></li>
<li class="chapter" data-level="7.5" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#sentiment-analysis"><i class="fa fa-check"></i><b>7.5</b> Sentiment analysis</a>
<ul>
<li class="chapter" data-level="7.5.1" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#sentiment-over-time"><i class="fa fa-check"></i><b>7.5.1</b> Sentiment over time</a></li>
<li class="chapter" data-level="" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#practice-sentiment-over-time"><i class="fa fa-check"></i>Practice: Sentiment over time</a></li>
<li class="chapter" data-level="7.5.2" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#sentiment-over-time-periods"><i class="fa fa-check"></i><b>7.5.2</b> Sentiment over time periods</a></li>
<li class="chapter" data-level="7.5.3" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#sentiment-of-individual-users"><i class="fa fa-check"></i><b>7.5.3</b> Sentiment of individual users</a></li>
</ul></li>
<li class="chapter" data-level="7.6" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#take-aways-6"><i class="fa fa-check"></i><b>7.6</b> Take-Aways</a></li>
<li class="chapter" data-level="7.7" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#additional-tutorials-6"><i class="fa fa-check"></i><b>7.7</b> Additional tutorials</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html"><i class="fa fa-check"></i>Solutions</a>
<ul>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#solutions-for-exercise-1"><i class="fa fa-check"></i>Solutions for Exercise 1</a>
<ul>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-1-5"><i class="fa fa-check"></i>Task 1</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-2-5"><i class="fa fa-check"></i>Task 2</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-3-5"><i class="fa fa-check"></i>Task 3</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-4-4"><i class="fa fa-check"></i>Task 4</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-5-4"><i class="fa fa-check"></i>Task 5</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-6-4"><i class="fa fa-check"></i>Task 6</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#solutions-for-exercise-2"><i class="fa fa-check"></i>Solutions for Exercise 2</a>
<ul>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-1-6"><i class="fa fa-check"></i>Task 1</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-2-6"><i class="fa fa-check"></i>Task 2</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-3-6"><i class="fa fa-check"></i>Task 3</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-4-5"><i class="fa fa-check"></i>Task 4</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-5-5"><i class="fa fa-check"></i>Task 5</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-6-5"><i class="fa fa-check"></i>Task 6</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-7-3"><i class="fa fa-check"></i>Task 7</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#solutions-for-exercise-3"><i class="fa fa-check"></i>Solutions for Exercise 3</a>
<ul>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-1-7"><i class="fa fa-check"></i>Task 1</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-2-7"><i class="fa fa-check"></i>Task 2</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-3-7"><i class="fa fa-check"></i>Task 3</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#solutions-for-exercise-4"><i class="fa fa-check"></i>Solutions for Exercise 4</a>
<ul>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-1-8"><i class="fa fa-check"></i>Task 1</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-2-8"><i class="fa fa-check"></i>Task 2</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-3-8"><i class="fa fa-check"></i>Task 3</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-4-6"><i class="fa fa-check"></i>Task 4</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-5-6"><i class="fa fa-check"></i>Task 5</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-6-6"><i class="fa fa-check"></i>Task 6</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-7-4"><i class="fa fa-check"></i>Task 7</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-8-1"><i class="fa fa-check"></i>Task 8</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#solutions-for-exercise-5"><i class="fa fa-check"></i>Solutions for Exercise 5</a>
<ul>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-1-9"><i class="fa fa-check"></i>Task 1</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-2-9"><i class="fa fa-check"></i>Task 2</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-3-9"><i class="fa fa-check"></i>Task 3</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-4-7"><i class="fa fa-check"></i>Task 4</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-5-7"><i class="fa fa-check"></i>Task 5</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-6-7"><i class="fa fa-check"></i>Task 6</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-7-5"><i class="fa fa-check"></i>Task 7</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#solutions-for-exercise-6"><i class="fa fa-check"></i>Solutions for Exercise 6</a>
<ul>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-1-tokenization"><i class="fa fa-check"></i>Task 1: Tokenization</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-2-stop-word-removal"><i class="fa fa-check"></i>Task 2: Stop word removal</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-3-lemmatizing-stemming"><i class="fa fa-check"></i>Task 3: Lemmatizing & stemming</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-4-pruning"><i class="fa fa-check"></i>Task 4: Pruning</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-5-model-estimation"><i class="fa fa-check"></i>Task 5: Model estimation</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-6-word-topic-probabilities"><i class="fa fa-check"></i>Task 6: Word-topic probabilities</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-7-document-topic-probabilities"><i class="fa fa-check"></i>Task 7: Document-topic probabilities</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-8-sentiment-over-time"><i class="fa fa-check"></i>Task 8: Sentiment over time</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-9-sentiment-over-time-periods"><i class="fa fa-check"></i>Task 9: Sentiment over time periods</a></li>
</ul></li>
</ul></li>
<li class="divider"></li>
<li><a href="https://github.com/LKobilke/PR-Erfolgskontrolle" target="blank">Published with bookdown</a></li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i><a href="./">R for PR evaluation</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<section class="normal" id="section-">
<div id="tutorial-data-visualization-with-ggplot" class="section level1 hasAnchor" number="5">
<h1><span class="header-section-number"> 5</span> Tutorial: Data visualization with ggplot<a href="tutorial-data-visualization-with-ggplot.html#tutorial-data-visualization-with-ggplot" class="anchor-section" aria-label="Anchor link to header"></a></h1>
<p>After working through Tutorial 5, you’ll…</p>
<ul>
<li>know what each graphical component of a ggplot graph contributes to
the final visualization</li>
<li>understand the <code>grammer of graphics</code> (or simply: the ggplot2 syntax)
to combine graphical components</li>
<li>know how to make your own data visualizations using <code>ggplot2</code></li>
</ul>
<div id="why-not-stick-with-base-r-1" class="section level2 hasAnchor" number="5.1">
<h2><span class="header-section-number">5.1</span> Why not stick with Base R?<a href="tutorial-data-visualization-with-ggplot.html#why-not-stick-with-base-r-1" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>The <code>ggplot2</code> package, i.e. the data visualization package of
<code>tidyverse</code>, has become <strong>the</strong> R package for data visualization. While
<code>Base R</code> can be used to visualize data, the <code>ggplot2</code> package makes data
visualization so much easier that I recommend starting with <code>ggplot2</code>
right away and skipping data visualization in <code>Base R</code> altogether.</p>
<p>The <code>gg</code> in <code>ggplot2</code> stands for <code>grammar of graphics</code>, which means that
we can describe each component of a graph layer by layer and component
by component. You only have to provide <code>ggplot()</code> with a source object
(i.e. data) and specify what variables it should map to the aesthetical
attributes (color, shape, size) of certain geometric objects (points,
lines, bars) – and <code>ggplot</code> will take care of the rest! The inventor of
<code>ggplot2</code>, Hadley Wickham, describes the benefits of <code>ggplot2</code> like
this:</p>
<blockquote>
<p>“In order to unlock the full power of ggplot2, <strong>you’ll need to master
the underlying grammar</strong>. By understanding the grammar, and how its
components fit together, you can create a wider range of
visualizations, combine multiple sources of data, and customise to
your heart’s content… The grammar makes it easier for you to
<strong>iteratively update a plot</strong>, changing a single feature at a time.
The grammar is also useful because it suggests the high-level aspects
of a plot that can be changed, giving you a framework to think about
graphics, and hopefully shortening the distance from mind to paper. It
also <strong>encourages the use of graphics customised to a particular
problem</strong>, rather than relying on specific chart types.” (<a href="https://ggplot2-book.org/mastery.html">Wickham et
al., 2021, no page; bold words
inserted</a>)</p>
</blockquote>
<p>Just as <code>dplyr</code> simplifies data manipulation, <code>ggplot2</code> simplifies data
visualization. In addition, <code>ggplot2</code> and <code>dplyr</code> work hand in hand: You
can prepare your data selection and manipulation with <code>dplyr</code> and pipe
it directly into <code>ggplot</code> to turn your transformed data into a beautiful
graph.</p>
<p>With only a few lines of code, you can produce graphs like this one:</p>
<p><img src="PR-Erfolgskontrolle_files/figure-html/unnamed-chunk-71-1.png" width="120%" /></p>
<p>This is the code. Right now, it might still look a bit overwhelming to
you, but once you’ve understood the grammar of graphics, it really is a
just a small jigsaw puzzle. Moreover, you don’t usually start with
graphs that are this complicated, but with basic scatter or bar plots.</p>
<div class="sourceCode" id="cb144"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb144-1"><a href="tutorial-data-visualization-with-ggplot.html#cb144-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(ggplot2)</span>
<span id="cb144-2"><a href="tutorial-data-visualization-with-ggplot.html#cb144-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb144-3"><a href="tutorial-data-visualization-with-ggplot.html#cb144-3" aria-hidden="true" tabindex="-1"></a>plot <span class="ot"><-</span> starwars_data <span class="sc">%>%</span></span>
<span id="cb144-4"><a href="tutorial-data-visualization-with-ggplot.html#cb144-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(species <span class="sc">==</span> <span class="st">"Human"</span> <span class="sc">|</span> species <span class="sc">==</span> <span class="st">"Droid"</span>) <span class="sc">%>%</span></span>
<span id="cb144-5"><a href="tutorial-data-visualization-with-ggplot.html#cb144-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x =</span> height, <span class="at">y =</span> mass, <span class="at">size =</span> birth_year, <span class="at">fill =</span> species)) <span class="sc">+</span></span>
<span id="cb144-6"><a href="tutorial-data-visualization-with-ggplot.html#cb144-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="at">shape =</span> <span class="dv">21</span>, <span class="at">alpha =</span> <span class="fl">0.25</span>, <span class="at">color =</span> <span class="st">"black"</span>) <span class="sc">+</span></span>
<span id="cb144-7"><a href="tutorial-data-visualization-with-ggplot.html#cb144-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">limits =</span> <span class="fu">c</span>(<span class="dv">30</span>, <span class="dv">140</span>)) <span class="sc">+</span> </span>
<span id="cb144-8"><a href="tutorial-data-visualization-with-ggplot.html#cb144-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_continuous</span>(<span class="at">limits =</span> <span class="fu">c</span>(<span class="dv">90</span>, <span class="dv">210</span>)) <span class="sc">+</span></span>
<span id="cb144-9"><a href="tutorial-data-visualization-with-ggplot.html#cb144-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">breaks =</span> <span class="fu">c</span>(<span class="dv">40</span>, <span class="dv">60</span>, <span class="dv">80</span>, <span class="dv">100</span>, <span class="dv">120</span>, <span class="dv">140</span>, <span class="dv">160</span>)) <span class="sc">+</span></span>
<span id="cb144-10"><a href="tutorial-data-visualization-with-ggplot.html#cb144-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_continuous</span>(<span class="at">breaks =</span> <span class="fu">c</span>(<span class="dv">100</span>, <span class="dv">120</span>, <span class="dv">140</span>, <span class="dv">160</span>, <span class="dv">180</span>, <span class="dv">200</span>)) <span class="sc">+</span></span>
<span id="cb144-11"><a href="tutorial-data-visualization-with-ggplot.html#cb144-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_size</span>(<span class="at">range =</span> <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">11</span>), <span class="at">name =</span> <span class="st">"age"</span>) <span class="sc">+</span></span>
<span id="cb144-12"><a href="tutorial-data-visualization-with-ggplot.html#cb144-12" aria-hidden="true" tabindex="-1"></a> ggrepel<span class="sc">::</span><span class="fu">geom_text_repel</span>(<span class="fu">aes</span>(<span class="at">label =</span> name), <span class="at">size =</span> <span class="fl">2.3</span>) <span class="sc">+</span></span>
<span id="cb144-13"><a href="tutorial-data-visualization-with-ggplot.html#cb144-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_bw</span>() <span class="sc">+</span></span>
<span id="cb144-14"><a href="tutorial-data-visualization-with-ggplot.html#cb144-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">title =</span> <span class="st">"Mass vs. height of humans and droids in Star Wars"</span>,</span>
<span id="cb144-15"><a href="tutorial-data-visualization-with-ggplot.html#cb144-15" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> <span class="st">"Height (cm)"</span>, <span class="at">y =</span> <span class="st">"Weight (kg)"</span>) <span class="sc">+</span></span>
<span id="cb144-16"><a href="tutorial-data-visualization-with-ggplot.html#cb144-16" aria-hidden="true" tabindex="-1"></a> <span class="fu">facet_wrap</span>(<span class="sc">~</span>species)</span></code></pre></div>
<p>To visit the official documentation of <code>ggplot2</code>: - type <code>?ggplot2</code> in
your console - visit the <a href="https://www.rdocumentation.org/packages/ggplot2/versions/3.3.5">ggplot
documentation</a> -
visit the <a href="https://ggplot2.tidyverse.org/">ggplot homepage of the
tidyverse</a></p>
</div>
<div id="components-of-a-ggplot-graph" class="section level2 hasAnchor" number="5.2">
<h2><span class="header-section-number">5.2</span> Components of a ggplot graph<a href="tutorial-data-visualization-with-ggplot.html#components-of-a-ggplot-graph" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>As mentioned before, the main idea behind <code>ggplot</code> is to generate a
statistical plot by combining layers that represent geometric objects
(e.g. points and lines). By linking data to the aesthetic features of
these geometric objects (e.g. colors, size, transparency), the aesthetic
properties of the geometric objects may be controlled. In the words of
Wickham:</p>
<blockquote>
<p>“A graphic maps the <strong>data</strong> to the <strong>aesthetic</strong> attributes (colour,
shape, size) of <strong>geometric</strong> objects (points, lines, bars).” <a href="https://ggplot2-book.org/introduction.html">Wickham
et al., 2021, no page; bold words
inserted</a></p>
</blockquote>
<table>
<colgroup>
<col width="100%" />
</colgroup>
<tbody>
<tr class="odd">
<td><em>Image: The logic of adding layer by layer in ggplot (<a href="https://bookdown.org/sunboklee/introduction_to_r/week2-data-visualization-i.html">Source: R @ Ewah 2020</a>):</em></td>
</tr>
<tr class="even">
<td><img src="images/Tut5_plotlayers.png" /></td>
</tr>
</tbody>
</table>
<p>The <strong>necessary</strong> components of a <code>ggplot</code> graph are:</p>
<ol style="list-style-type: decimal">
<li>Source object / <code>data</code>: The data that you would like to visualize.</li>
<li>Geometries <code>geom_</code>: Geom options allow you to specify what geometric
objects will represent the data (i.e. points, bars, lines, and many
more).</li>
<li>Aesthetics <code>aes()</code>: Aesthetics allows you to map variables to the x-
and y-axis and to the aesthetics of those geometric objects (i.e.
position, color, size, shape, linetype, and transparency).</li>
</ol>
<p>The <strong>complementary, but not necessary</strong> components of a <code>ggplot</code> graph
are:</p>
<ol start="4" style="list-style-type: decimal">
<li>Scales <code>scale_</code>: Scale options allow you to fine-tune the mapping
from the variables to the aesthetics. You can fine-tune axis limits,
tick breaks, grid lines, or any other axis/geometric object
transformations that depend on the range of a specific scale.</li>
<li>Statistical transformations <code>stat_</code>: Allows you to produce
statistical summaries of the data for visualization (i.e. means and
standard deviations, fitted curves, and many more).</li>
<li>Coordinate system <code>coord_</code>: Allows you to change the appearance of
your coordinate system (i.e. flip the coordinates to turn horizontal
bar chart into a vertical one).</li>
<li>Position: to adjust overlapping objects, e.g. jittering, stacking or
dodging.</li>
<li>Facets <code>facet_</code>: Allows you to divide your plot into multiple
subplots.</li>
<li>Visual themes <code>theme()</code>: Allows you to specify the visual basics of
a plot, such as background, default typeface, sizes, and colors.</li>
<li>Axis labels <code>labs()</code>: Allows you to change the plot’s main title and
the axis labels.</li>
</ol>
</div>
<div id="installing-activating-ggplot" class="section level2 hasAnchor" number="5.3">
<h2><span class="header-section-number">5.3</span> Installing & activating ggplot<a href="tutorial-data-visualization-with-ggplot.html#installing-activating-ggplot" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>You can always activate <code>ggplot2</code> by activating the meta-package
<code>tidyverse</code>:</p>
<div class="sourceCode" id="cb145"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb145-1"><a href="tutorial-data-visualization-with-ggplot.html#cb145-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyverse)</span></code></pre></div>
<p>If for some reason you do not want to activate the whole <code>tidyverse</code>,
you should install <code>ggplot2</code> and activate this package separately:</p>
<div class="sourceCode" id="cb146"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb146-1"><a href="tutorial-data-visualization-with-ggplot.html#cb146-1" aria-hidden="true" tabindex="-1"></a><span class="fu">install.packages</span>(<span class="st">"ggplot2"</span>) <span class="co"># install the package (only on the first time)</span></span></code></pre></div>
<div class="sourceCode" id="cb147"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb147-1"><a href="tutorial-data-visualization-with-ggplot.html#cb147-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(ggplot2) <span class="co"># active the package</span></span></code></pre></div>
</div>
<div id="building-your-first-plot" class="section level2 hasAnchor" number="5.4">
<h2><span class="header-section-number">5.4</span> Building your first plot<a href="tutorial-data-visualization-with-ggplot.html#building-your-first-plot" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>In the next sections, you will create your very first plot – layer by
layer. We will look at some of the most important components that you
will regularly add to graphs and you will learn how to make use of them.</p>
<div id="data" class="section level3 hasAnchor" number="5.4.1">
<h3><span class="header-section-number">5.4.1</span> Data<a href="tutorial-data-visualization-with-ggplot.html#data" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Obviously, you need data to perform data visualization. Therefore, our
first step is to load the <em>starwars data</em>, but let’s keep only humans
and droids for now. To this end, assign your transformed data to a new
data frame called <em>human_droid_data</em>.</p>
<div class="sourceCode" id="cb148"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb148-1"><a href="tutorial-data-visualization-with-ggplot.html#cb148-1" aria-hidden="true" tabindex="-1"></a>human_droid_data <span class="ot"><-</span> dplyr<span class="sc">::</span>starwars <span class="sc">%>%</span></span>
<span id="cb148-2"><a href="tutorial-data-visualization-with-ggplot.html#cb148-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(species <span class="sc">==</span> <span class="st">"Human"</span> <span class="sc">|</span> species <span class="sc">==</span> <span class="st">"Droid"</span>)</span></code></pre></div>
<p>The function <code>ggplot()</code> can only create a plot if we explicitly tell the
function what data to use, so this graphical component is <strong>necessary</strong>.
Using our <code>dplyr</code> skills, let’s use the <em>human_droid_data</em> as our source
object and apply the <code>ggplot()</code> function to it by using a pipe (i.e.
%>%).</p>
<div class="sourceCode" id="cb149"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb149-1"><a href="tutorial-data-visualization-with-ggplot.html#cb149-1" aria-hidden="true" tabindex="-1"></a>human_droid_data <span class="sc">%>%</span> </span>
<span id="cb149-2"><a href="tutorial-data-visualization-with-ggplot.html#cb149-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>()</span></code></pre></div>
<p><img src="PR-Erfolgskontrolle_files/figure-html/unnamed-chunk-77-1.png" width="672" /></p>
<p>The <code>ggplot()</code> function creates a blank canvas (i.e. first layer). We
now have to draw on it.</p>
</div>
<div id="aesthetics" class="section level3 hasAnchor" number="5.4.2">
<h3><span class="header-section-number">5.4.2</span> Aesthetics<a href="tutorial-data-visualization-with-ggplot.html#aesthetics" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>To draw on this blank canvas, we must at least tell the <code>ggplot()</code>
function which variables to assign to the x- and y-axis by using the
<code>aes()</code> function. Thus, the Aesthetics graph component is also
<strong>necessary</strong> in every single plot.</p>
<div class="sourceCode" id="cb150"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb150-1"><a href="tutorial-data-visualization-with-ggplot.html#cb150-1" aria-hidden="true" tabindex="-1"></a>human_droid_data <span class="sc">%>%</span> </span>
<span id="cb150-2"><a href="tutorial-data-visualization-with-ggplot.html#cb150-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x =</span> height, <span class="at">y =</span> mass))</span></code></pre></div>
<p><img src="PR-Erfolgskontrolle_files/figure-html/unnamed-chunk-78-1.png" width="672" /></p>
<p>The <code>aes()</code> function allows you to specify the following arguments (and
many more, as you will learn over time):</p>
<ul>
<li><strong>x</strong>: the variable that should be mapped to the x axis</li>
<li><strong>y</strong>: the variable that should be mapped to the y</li>
<li><strong>size</strong>: the variable that should be used for determining the
<em>size</em> of a geometric object</li>
<li><strong>fill</strong>: the variable that should be used for <em>filling</em> a geometric
object with a specific color</li>
<li><strong>color</strong>: the variable that should be used for <em>outlining</em> a
geometric object with a specific color</li>
</ul>
</div>
<div id="geometrics" class="section level3 hasAnchor" number="5.4.3">
<h3><span class="header-section-number">5.4.3</span> Geometrics<a href="tutorial-data-visualization-with-ggplot.html#geometrics" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Finally, we can turn to the last <strong>necessary</strong> component of any ggplot
graph: the geometric objects that fill your canvas. The choice of these
geometric objects determines what kind of chart you create.</p>
<p>The <code>geom_</code> component of the <code>ggplot()</code> function allows you to create
the following chart types (and many more, as you will learn over time):</p>
<ul>
<li><strong>geom_bar()</strong>: to create a bar chart</li>
<li><strong>geom_histogram</strong>: to create a histogram</li>
<li><strong>geom_line()</strong>: to create a line graph</li>
<li><strong>geom_point()</strong>: to create a scatter or bubble plot</li>
<li><strong>geom_boxplot()</strong>: to create a box plot</li>
</ul>
<p>Now let’s add the data points (x,y) with <em>geom_point()</em> to our canvas to
make it a scatter plot:</p>
<div class="sourceCode" id="cb151"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb151-1"><a href="tutorial-data-visualization-with-ggplot.html#cb151-1" aria-hidden="true" tabindex="-1"></a>human_droid_data <span class="sc">%>%</span> </span>
<span id="cb151-2"><a href="tutorial-data-visualization-with-ggplot.html#cb151-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x =</span> height, <span class="at">y =</span> mass)) <span class="sc">+</span></span>
<span id="cb151-3"><a href="tutorial-data-visualization-with-ggplot.html#cb151-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>()</span></code></pre></div>
<p><img src="PR-Erfolgskontrolle_files/figure-html/unnamed-chunk-79-1.png" width="672" /></p>
<p>That’s a scatter plot for sure! And you only needed three <strong>necessary</strong>
components to create it:</p>
<ol style="list-style-type: decimal">
<li>data (i.e. a source object),</li>
<li>aesthetics <code>aes()</code>,</li>
<li>and geometric objects <code>geom_</code>.</li>
</ol>
</div>
<div id="scales" class="section level3 hasAnchor" number="5.4.4">
<h3><span class="header-section-number">5.4.4</span> Scales<a href="tutorial-data-visualization-with-ggplot.html#scales" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>A scale is a mapping from data to the final values that computers can
use to actually show the aesthetics. In this sense, a scale
<strong>regulates</strong> the aesthetic mapping of variables to aesthetics.
Providing a <code>scale_</code> is not <strong>necessary</strong> to create a graph, but it
allows you to fine-tune aesthetic mappings to customize your graph.
<code>scale_</code> is very powerful and over time, you will learn about a lot of
things that you can customize with it. For now, we will only focus on a
few of these.</p>
<p>We will use <code>scale_</code> to :</p>
<ul>
<li>change the limits and ticks of the x and y axis</li>
<li>change how a third variable (besides x and y) is mapped to the
aesthetics of our geometric object</li>
</ul>
<p>First, we will use <code>scale_</code> to modify the x and the y axis by providing
the graph with new axis limits.</p>
<div class="sourceCode" id="cb152"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb152-1"><a href="tutorial-data-visualization-with-ggplot.html#cb152-1" aria-hidden="true" tabindex="-1"></a>human_droid_data <span class="sc">%>%</span></span>
<span id="cb152-2"><a href="tutorial-data-visualization-with-ggplot.html#cb152-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x =</span> height, <span class="at">y =</span> mass)) <span class="sc">+</span></span>
<span id="cb152-3"><a href="tutorial-data-visualization-with-ggplot.html#cb152-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>() <span class="sc">+</span></span>
<span id="cb152-4"><a href="tutorial-data-visualization-with-ggplot.html#cb152-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">limits =</span> <span class="fu">c</span>(<span class="dv">30</span>, <span class="dv">140</span>)) <span class="sc">+</span> <span class="co"># modify the y axis limits</span></span>
<span id="cb152-5"><a href="tutorial-data-visualization-with-ggplot.html#cb152-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_continuous</span>(<span class="at">limits =</span> <span class="fu">c</span>(<span class="dv">90</span>, <span class="dv">210</span>)) <span class="co"># modify the x axis limits</span></span></code></pre></div>
<p><img src="PR-Erfolgskontrolle_files/figure-html/unnamed-chunk-80-1.png" width="672" /></p>
<p>Second, we’ll add more ticks to make the graph better readable.</p>
<div class="sourceCode" id="cb153"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb153-1"><a href="tutorial-data-visualization-with-ggplot.html#cb153-1" aria-hidden="true" tabindex="-1"></a>human_droid_data <span class="sc">%>%</span></span>
<span id="cb153-2"><a href="tutorial-data-visualization-with-ggplot.html#cb153-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x =</span> height, <span class="at">y =</span> mass)) <span class="sc">+</span></span>
<span id="cb153-3"><a href="tutorial-data-visualization-with-ggplot.html#cb153-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>() <span class="sc">+</span></span>
<span id="cb153-4"><a href="tutorial-data-visualization-with-ggplot.html#cb153-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">limits =</span> <span class="fu">c</span>(<span class="dv">30</span>, <span class="dv">140</span>)) <span class="sc">+</span> </span>
<span id="cb153-5"><a href="tutorial-data-visualization-with-ggplot.html#cb153-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_continuous</span>(<span class="at">limits =</span> <span class="fu">c</span>(<span class="dv">90</span>, <span class="dv">210</span>)) <span class="sc">+</span></span>
<span id="cb153-6"><a href="tutorial-data-visualization-with-ggplot.html#cb153-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">breaks =</span> <span class="fu">c</span>(<span class="dv">40</span>, <span class="dv">60</span>, <span class="dv">80</span>, <span class="dv">100</span>, <span class="dv">120</span>, <span class="dv">140</span>, <span class="dv">160</span>)) <span class="sc">+</span> <span class="co"># choose where the ticks of the y axis appear</span></span>
<span id="cb153-7"><a href="tutorial-data-visualization-with-ggplot.html#cb153-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_continuous</span>(<span class="at">breaks =</span> <span class="fu">c</span>(<span class="dv">100</span>, <span class="dv">120</span>, <span class="dv">140</span>, <span class="dv">160</span>, <span class="dv">180</span>, <span class="dv">200</span>)) <span class="co"># choose where the ticks of the x axis appear</span></span></code></pre></div>
<p><img src="PR-Erfolgskontrolle_files/figure-html/unnamed-chunk-81-1.png" width="672" /></p>
<p>Until now, we have used <code>scale_</code> to transform only the axes. But we can
also use it to change the mapping of variables to geometric objects. To
demonstrate this, we now add another variable to our graph, namely the
age (<em>birth_year</em>) of the humanoid and droid Star Wars characters. Let’s
map age to our data points (i.e. <code>geom_point()</code>) so that larger bubbles
reflect older age.</p>
<div class="sourceCode" id="cb154"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb154-1"><a href="tutorial-data-visualization-with-ggplot.html#cb154-1" aria-hidden="true" tabindex="-1"></a>human_droid_data <span class="sc">%>%</span></span>
<span id="cb154-2"><a href="tutorial-data-visualization-with-ggplot.html#cb154-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x =</span> height, <span class="at">y =</span> mass, <span class="at">size=</span>birth_year)) <span class="sc">+</span> <span class="co"># map birth_year (age) to the size of the following geometric objects</span></span>
<span id="cb154-3"><a href="tutorial-data-visualization-with-ggplot.html#cb154-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>() <span class="sc">+</span></span>
<span id="cb154-4"><a href="tutorial-data-visualization-with-ggplot.html#cb154-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">limits =</span> <span class="fu">c</span>(<span class="dv">30</span>, <span class="dv">140</span>)) <span class="sc">+</span> </span>
<span id="cb154-5"><a href="tutorial-data-visualization-with-ggplot.html#cb154-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_continuous</span>(<span class="at">limits =</span> <span class="fu">c</span>(<span class="dv">90</span>, <span class="dv">210</span>)) <span class="sc">+</span></span>
<span id="cb154-6"><a href="tutorial-data-visualization-with-ggplot.html#cb154-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">breaks =</span> <span class="fu">c</span>(<span class="dv">40</span>, <span class="dv">60</span>, <span class="dv">80</span>, <span class="dv">100</span>, <span class="dv">120</span>, <span class="dv">140</span>, <span class="dv">160</span>)) <span class="sc">+</span> </span>
<span id="cb154-7"><a href="tutorial-data-visualization-with-ggplot.html#cb154-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_continuous</span>(<span class="at">breaks =</span> <span class="fu">c</span>(<span class="dv">100</span>, <span class="dv">120</span>, <span class="dv">140</span>, <span class="dv">160</span>, <span class="dv">180</span>, <span class="dv">200</span>)) </span></code></pre></div>
<p><img src="PR-Erfolgskontrolle_files/figure-html/unnamed-chunk-82-1.png" width="672" /></p>
<p>Personally, I feel like these bubbles could use a little bit of
rescaling to make age differences stand out more. In addition, you could
get a nicer title for the size legend than “birth_year”. Let’s try that.</p>
<div class="sourceCode" id="cb155"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb155-1"><a href="tutorial-data-visualization-with-ggplot.html#cb155-1" aria-hidden="true" tabindex="-1"></a>human_droid_data <span class="sc">%>%</span></span>
<span id="cb155-2"><a href="tutorial-data-visualization-with-ggplot.html#cb155-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x =</span> height, <span class="at">y =</span> mass, <span class="at">size=</span>birth_year)) <span class="sc">+</span></span>
<span id="cb155-3"><a href="tutorial-data-visualization-with-ggplot.html#cb155-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>() <span class="sc">+</span></span>
<span id="cb155-4"><a href="tutorial-data-visualization-with-ggplot.html#cb155-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">limits =</span> <span class="fu">c</span>(<span class="dv">30</span>, <span class="dv">140</span>)) <span class="sc">+</span> </span>
<span id="cb155-5"><a href="tutorial-data-visualization-with-ggplot.html#cb155-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_continuous</span>(<span class="at">limits =</span> <span class="fu">c</span>(<span class="dv">90</span>, <span class="dv">210</span>)) <span class="sc">+</span></span>
<span id="cb155-6"><a href="tutorial-data-visualization-with-ggplot.html#cb155-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">breaks =</span> <span class="fu">c</span>(<span class="dv">40</span>, <span class="dv">60</span>, <span class="dv">80</span>, <span class="dv">100</span>, <span class="dv">120</span>, <span class="dv">140</span>, <span class="dv">160</span>)) <span class="sc">+</span> </span>
<span id="cb155-7"><a href="tutorial-data-visualization-with-ggplot.html#cb155-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_continuous</span>(<span class="at">breaks =</span> <span class="fu">c</span>(<span class="dv">100</span>, <span class="dv">120</span>, <span class="dv">140</span>, <span class="dv">160</span>, <span class="dv">180</span>, <span class="dv">200</span>)) <span class="sc">+</span></span>
<span id="cb155-8"><a href="tutorial-data-visualization-with-ggplot.html#cb155-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_size</span>(<span class="at">range =</span> <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">11</span>), <span class="at">name =</span> <span class="st">"age"</span>) <span class="co"># sets the bubbles' size in a range between 1 and 11 and renames the respective legend title to "age"</span></span></code></pre></div>
<p><img src="PR-Erfolgskontrolle_files/figure-html/unnamed-chunk-83-1.png" width="672" /></p>
<p>Perfect! The legend spells “age” and age differences seem a bit more
obvious now. Unfortunately, some data points are now overlapping.</p>
<p>I think this is a good time to introduce you to the differences between
using <code>scale_</code> and adding aesthetics to the <code>geom_</code> objects directly.
While the former allows you to change the mapping from <strong>variables</strong> to
the aesthetics of geometric objects, the latter one allows you to
provide a <strong>constant</strong>. This means that the aesthetic mapping does not
depend on the values of a variable, but is set to a single default
value. To demonstrate this and fix the overlap of our bubbles, we change
the transparency value of the bubbles so that they become transparent.
Note that all the values given are constants, which means that they do
not depend on a third variable like <em>birth_year</em>.</p>
<div class="sourceCode" id="cb156"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb156-1"><a href="tutorial-data-visualization-with-ggplot.html#cb156-1" aria-hidden="true" tabindex="-1"></a>human_droid_data <span class="sc">%>%</span></span>
<span id="cb156-2"><a href="tutorial-data-visualization-with-ggplot.html#cb156-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x =</span> height, <span class="at">y =</span> mass, <span class="at">size=</span>birth_year)) <span class="sc">+</span></span>
<span id="cb156-3"><a href="tutorial-data-visualization-with-ggplot.html#cb156-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="at">shape =</span> <span class="dv">21</span>, <span class="at">fill =</span> <span class="st">"black"</span>, <span class="at">alpha =</span> <span class="fl">0.25</span>, <span class="at">color =</span> <span class="st">"black"</span>) <span class="sc">+</span> <span class="co"># shape = 21 is creating bubbles that have a border (i.e. outline), fill = "black" fills the bubble with black ink, alpha = 0.25 to make the bubbles` black ink 25% transparent and color = "black" to make the border (i.e. outline) pitch black</span></span>
<span id="cb156-4"><a href="tutorial-data-visualization-with-ggplot.html#cb156-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">limits =</span> <span class="fu">c</span>(<span class="dv">30</span>, <span class="dv">140</span>)) <span class="sc">+</span> </span>
<span id="cb156-5"><a href="tutorial-data-visualization-with-ggplot.html#cb156-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_continuous</span>(<span class="at">limits =</span> <span class="fu">c</span>(<span class="dv">90</span>, <span class="dv">210</span>)) <span class="sc">+</span></span>
<span id="cb156-6"><a href="tutorial-data-visualization-with-ggplot.html#cb156-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">breaks =</span> <span class="fu">c</span>(<span class="dv">40</span>, <span class="dv">60</span>, <span class="dv">80</span>, <span class="dv">100</span>, <span class="dv">120</span>, <span class="dv">140</span>, <span class="dv">160</span>)) <span class="sc">+</span> </span>
<span id="cb156-7"><a href="tutorial-data-visualization-with-ggplot.html#cb156-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_continuous</span>(<span class="at">breaks =</span> <span class="fu">c</span>(<span class="dv">100</span>, <span class="dv">120</span>, <span class="dv">140</span>, <span class="dv">160</span>, <span class="dv">180</span>, <span class="dv">200</span>)) <span class="sc">+</span></span>
<span id="cb156-8"><a href="tutorial-data-visualization-with-ggplot.html#cb156-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_size</span>(<span class="at">range =</span> <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">11</span>), <span class="at">name =</span> <span class="st">"age"</span>)</span></code></pre></div>
<p><img src="PR-Erfolgskontrolle_files/figure-html/unnamed-chunk-84-1.png" width="672" /></p>
<p>This looks way more readable. I think we are ready to move on to
<a href="tutorial-data-visualization-with-ggplot.html#themes">Themes</a>.</p>
</div>
<div id="themes" class="section level3 hasAnchor" number="5.4.5">
<h3><span class="header-section-number">5.4.5</span> Themes<a href="tutorial-data-visualization-with-ggplot.html#themes" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Just like <code>scale_</code>, <code>theme_</code>is an optional ggplot component, i.e. <strong>not
necessary</strong>. Themes are visually appealing presets for charts, e.g.,
they influence whether grid lines are visible or whether certain color
palettes are applied to the data. By using themes you can make your
graphs more beautiful and give them a consistent style without any
effort, which is especially useful for longer texts like theses. To
familiarize yourself with the various options, take a look at this
<a href="https://ggplot2.tidyverse.org/reference/ggtheme.html">overview of all ggplot2
themes</a>.</p>
<p>If you don’t like grid lines, for example, <em>theme_classic()</em> might be to
your taste:</p>
<div class="sourceCode" id="cb157"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb157-1"><a href="tutorial-data-visualization-with-ggplot.html#cb157-1" aria-hidden="true" tabindex="-1"></a>human_droid_data <span class="sc">%>%</span></span>
<span id="cb157-2"><a href="tutorial-data-visualization-with-ggplot.html#cb157-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x =</span> height, <span class="at">y =</span> mass, <span class="at">size=</span>birth_year)) <span class="sc">+</span></span>
<span id="cb157-3"><a href="tutorial-data-visualization-with-ggplot.html#cb157-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="at">shape =</span> <span class="dv">21</span>, <span class="at">fill =</span> <span class="st">"black"</span>, <span class="at">alpha =</span> <span class="fl">0.25</span>, <span class="at">color =</span> <span class="st">"black"</span>) <span class="sc">+</span></span>
<span id="cb157-4"><a href="tutorial-data-visualization-with-ggplot.html#cb157-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">limits =</span> <span class="fu">c</span>(<span class="dv">30</span>, <span class="dv">140</span>)) <span class="sc">+</span> </span>
<span id="cb157-5"><a href="tutorial-data-visualization-with-ggplot.html#cb157-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_continuous</span>(<span class="at">limits =</span> <span class="fu">c</span>(<span class="dv">90</span>, <span class="dv">210</span>)) <span class="sc">+</span></span>
<span id="cb157-6"><a href="tutorial-data-visualization-with-ggplot.html#cb157-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">breaks =</span> <span class="fu">c</span>(<span class="dv">40</span>, <span class="dv">60</span>, <span class="dv">80</span>, <span class="dv">100</span>, <span class="dv">120</span>, <span class="dv">140</span>, <span class="dv">160</span>)) <span class="sc">+</span> </span>
<span id="cb157-7"><a href="tutorial-data-visualization-with-ggplot.html#cb157-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_continuous</span>(<span class="at">breaks =</span> <span class="fu">c</span>(<span class="dv">100</span>, <span class="dv">120</span>, <span class="dv">140</span>, <span class="dv">160</span>, <span class="dv">180</span>, <span class="dv">200</span>)) <span class="sc">+</span></span>
<span id="cb157-8"><a href="tutorial-data-visualization-with-ggplot.html#cb157-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_size</span>(<span class="at">range =</span> <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">11</span>), <span class="at">name =</span> <span class="st">"age"</span>) <span class="sc">+</span></span>
<span id="cb157-9"><a href="tutorial-data-visualization-with-ggplot.html#cb157-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_classic</span>()</span></code></pre></div>
<p><img src="PR-Erfolgskontrolle_files/figure-html/unnamed-chunk-85-1.png" width="672" /></p>
<p>Personally, I really enjoy the <em>theme_bw()</em> (black-and-white theme). So
let’s apply it to our graph:</p>
<div class="sourceCode" id="cb158"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb158-1"><a href="tutorial-data-visualization-with-ggplot.html#cb158-1" aria-hidden="true" tabindex="-1"></a>human_droid_data <span class="sc">%>%</span></span>
<span id="cb158-2"><a href="tutorial-data-visualization-with-ggplot.html#cb158-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x =</span> height, <span class="at">y =</span> mass, <span class="at">size=</span>birth_year)) <span class="sc">+</span></span>
<span id="cb158-3"><a href="tutorial-data-visualization-with-ggplot.html#cb158-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="at">shape =</span> <span class="dv">21</span>, <span class="at">fill =</span> <span class="st">"black"</span>, <span class="at">alpha =</span> <span class="fl">0.25</span>, <span class="at">color =</span> <span class="st">"black"</span>) <span class="sc">+</span></span>
<span id="cb158-4"><a href="tutorial-data-visualization-with-ggplot.html#cb158-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">limits =</span> <span class="fu">c</span>(<span class="dv">30</span>, <span class="dv">140</span>)) <span class="sc">+</span> </span>
<span id="cb158-5"><a href="tutorial-data-visualization-with-ggplot.html#cb158-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_continuous</span>(<span class="at">limits =</span> <span class="fu">c</span>(<span class="dv">90</span>, <span class="dv">210</span>)) <span class="sc">+</span></span>
<span id="cb158-6"><a href="tutorial-data-visualization-with-ggplot.html#cb158-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">breaks =</span> <span class="fu">c</span>(<span class="dv">40</span>, <span class="dv">60</span>, <span class="dv">80</span>, <span class="dv">100</span>, <span class="dv">120</span>, <span class="dv">140</span>, <span class="dv">160</span>)) <span class="sc">+</span> </span>
<span id="cb158-7"><a href="tutorial-data-visualization-with-ggplot.html#cb158-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_continuous</span>(<span class="at">breaks =</span> <span class="fu">c</span>(<span class="dv">100</span>, <span class="dv">120</span>, <span class="dv">140</span>, <span class="dv">160</span>, <span class="dv">180</span>, <span class="dv">200</span>)) <span class="sc">+</span></span>
<span id="cb158-8"><a href="tutorial-data-visualization-with-ggplot.html#cb158-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_size</span>(<span class="at">range =</span> <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">11</span>), <span class="at">name =</span> <span class="st">"age"</span>) <span class="sc">+</span></span>
<span id="cb158-9"><a href="tutorial-data-visualization-with-ggplot.html#cb158-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_bw</span>()</span></code></pre></div>
<p><img src="PR-Erfolgskontrolle_files/figure-html/unnamed-chunk-86-1.png" width="672" /></p>
</div>
<div id="labs" class="section level3 hasAnchor" number="5.4.6">
<h3><span class="header-section-number">5.4.6</span> Labs<a href="tutorial-data-visualization-with-ggplot.html#labs" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Again, <code>labs()</code> is not a <strong>necessary</strong>, but an optional component of
your graph. Using the <code>labs()</code> function allows you to set a main title
for your plot and to change the labels of the x and y axis.</p>
<p>Let’s try it:</p>
<div class="sourceCode" id="cb159"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb159-1"><a href="tutorial-data-visualization-with-ggplot.html#cb159-1" aria-hidden="true" tabindex="-1"></a>human_droid_data <span class="sc">%>%</span></span>
<span id="cb159-2"><a href="tutorial-data-visualization-with-ggplot.html#cb159-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x =</span> height, <span class="at">y =</span> mass, <span class="at">size=</span>birth_year)) <span class="sc">+</span></span>
<span id="cb159-3"><a href="tutorial-data-visualization-with-ggplot.html#cb159-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="at">shape =</span> <span class="dv">21</span>, <span class="at">fill =</span> <span class="st">"black"</span>, <span class="at">alpha =</span> <span class="fl">0.25</span>, <span class="at">color =</span> <span class="st">"black"</span>) <span class="sc">+</span></span>
<span id="cb159-4"><a href="tutorial-data-visualization-with-ggplot.html#cb159-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">limits =</span> <span class="fu">c</span>(<span class="dv">30</span>, <span class="dv">140</span>)) <span class="sc">+</span> </span>
<span id="cb159-5"><a href="tutorial-data-visualization-with-ggplot.html#cb159-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_continuous</span>(<span class="at">limits =</span> <span class="fu">c</span>(<span class="dv">90</span>, <span class="dv">210</span>)) <span class="sc">+</span></span>
<span id="cb159-6"><a href="tutorial-data-visualization-with-ggplot.html#cb159-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">breaks =</span> <span class="fu">c</span>(<span class="dv">40</span>, <span class="dv">60</span>, <span class="dv">80</span>, <span class="dv">100</span>, <span class="dv">120</span>, <span class="dv">140</span>, <span class="dv">160</span>)) <span class="sc">+</span> </span>
<span id="cb159-7"><a href="tutorial-data-visualization-with-ggplot.html#cb159-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_continuous</span>(<span class="at">breaks =</span> <span class="fu">c</span>(<span class="dv">100</span>, <span class="dv">120</span>, <span class="dv">140</span>, <span class="dv">160</span>, <span class="dv">180</span>, <span class="dv">200</span>)) <span class="sc">+</span></span>
<span id="cb159-8"><a href="tutorial-data-visualization-with-ggplot.html#cb159-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_size</span>(<span class="at">range =</span> <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">11</span>), <span class="at">name =</span> <span class="st">"age"</span>) <span class="sc">+</span></span>
<span id="cb159-9"><a href="tutorial-data-visualization-with-ggplot.html#cb159-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_bw</span>() <span class="sc">+</span></span>
<span id="cb159-10"><a href="tutorial-data-visualization-with-ggplot.html#cb159-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">title =</span> <span class="st">"Mass vs. height of humans and droids in Star Wars"</span>,</span>
<span id="cb159-11"><a href="tutorial-data-visualization-with-ggplot.html#cb159-11" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> <span class="st">"Height (cm)"</span>, <span class="at">y =</span> <span class="st">"Weight (kg)"</span>)</span></code></pre></div>
<p><img src="PR-Erfolgskontrolle_files/figure-html/unnamed-chunk-87-1.png" width="672" /></p>
<p>Now that we’ve added a main title, it becomes clear that we can’t really
distinguish the data points that represent humans from those that
represent droids.</p>
</div>
<div id="facets" class="section level3 hasAnchor" number="5.4.7">
<h3><span class="header-section-number">5.4.7</span> Facets<a href="tutorial-data-visualization-with-ggplot.html#facets" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Faceting divides plot into tiny subplots, which display different
subsets of your data. Facets are an effective way to explore your data
because they allow you to rapidly detect divergent patterns in these
subsets. Of course, faceting is optional, i.e. <strong>not necessary</strong>. You
don’t need faceting if you don’t want to compare different groups within
your data.</p>
<p>The two approaches to faceting are:</p>
<ul>
<li><code>facet_wrap()</code>: uses the levels of <strong>one (or more) variable(s)</strong> to
create groups + panels for each group; useful if you have a single
categorical variable with many levels</li>
<li><code>facet_grid()</code>: produces a matrix of panels defined by <strong>two
variables</strong> which form the rows and columns</li>
</ul>
<table>
<colgroup>
<col width="100%" />
</colgroup>
<tbody>
<tr class="odd">
<td><em>Image: The logic of faceting (<a href="https://ggplot2-book.org/facet.html">Source: Wickham et al., 2021</a>):</em></td>
</tr>
<tr class="even">
<td><img src="images/Tut5_faceting.jpg" /></td>
</tr>
</tbody>
</table>
<p>Let’s use the <code>facet_wrap()</code> function to create two subplots for our two
different levels of the <em>species</em> variable: Droid and Human. You can
provide two arguments to <code>facet_wrap()</code>:</p>
<ul>
<li><code>~</code>, followed by the grouping variable</li>
<li><code>nrow</code>: the number of rows in which panels should be placed</li>
</ul>
<div class="sourceCode" id="cb160"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb160-1"><a href="tutorial-data-visualization-with-ggplot.html#cb160-1" aria-hidden="true" tabindex="-1"></a>human_droid_data <span class="sc">%>%</span></span>
<span id="cb160-2"><a href="tutorial-data-visualization-with-ggplot.html#cb160-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x =</span> height, <span class="at">y =</span> mass, <span class="at">size=</span>birth_year)) <span class="sc">+</span></span>
<span id="cb160-3"><a href="tutorial-data-visualization-with-ggplot.html#cb160-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="at">shape =</span> <span class="dv">21</span>, <span class="at">fill =</span> <span class="st">"black"</span>, <span class="at">alpha =</span> <span class="fl">0.25</span>, <span class="at">color =</span> <span class="st">"black"</span>) <span class="sc">+</span></span>
<span id="cb160-4"><a href="tutorial-data-visualization-with-ggplot.html#cb160-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">limits =</span> <span class="fu">c</span>(<span class="dv">30</span>, <span class="dv">140</span>)) <span class="sc">+</span> </span>
<span id="cb160-5"><a href="tutorial-data-visualization-with-ggplot.html#cb160-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_continuous</span>(<span class="at">limits =</span> <span class="fu">c</span>(<span class="dv">90</span>, <span class="dv">210</span>)) <span class="sc">+</span></span>
<span id="cb160-6"><a href="tutorial-data-visualization-with-ggplot.html#cb160-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">breaks =</span> <span class="fu">c</span>(<span class="dv">40</span>, <span class="dv">60</span>, <span class="dv">80</span>, <span class="dv">100</span>, <span class="dv">120</span>, <span class="dv">140</span>, <span class="dv">160</span>)) <span class="sc">+</span> </span>
<span id="cb160-7"><a href="tutorial-data-visualization-with-ggplot.html#cb160-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_continuous</span>(<span class="at">breaks =</span> <span class="fu">c</span>(<span class="dv">100</span>, <span class="dv">120</span>, <span class="dv">140</span>, <span class="dv">160</span>, <span class="dv">180</span>, <span class="dv">200</span>)) <span class="sc">+</span></span>
<span id="cb160-8"><a href="tutorial-data-visualization-with-ggplot.html#cb160-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_size</span>(<span class="at">range =</span> <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">11</span>), <span class="at">name =</span> <span class="st">"age"</span>) <span class="sc">+</span></span>
<span id="cb160-9"><a href="tutorial-data-visualization-with-ggplot.html#cb160-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_bw</span>() <span class="sc">+</span></span>
<span id="cb160-10"><a href="tutorial-data-visualization-with-ggplot.html#cb160-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">title =</span> <span class="st">"Mass vs. height of humans and droids in Star Wars"</span>,</span>
<span id="cb160-11"><a href="tutorial-data-visualization-with-ggplot.html#cb160-11" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> <span class="st">"Height (cm)"</span>, <span class="at">y =</span> <span class="st">"Weight (kg)"</span>) <span class="sc">+</span></span>
<span id="cb160-12"><a href="tutorial-data-visualization-with-ggplot.html#cb160-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">facet_wrap</span>(<span class="sc">~</span>species, <span class="at">nrow=</span><span class="dv">2</span>) <span class="co"># using ~grouping_variable and nrow = 2 shows the two panels on top of each other</span></span></code></pre></div>
<p><img src="PR-Erfolgskontrolle_files/figure-html/unnamed-chunk-88-1.png" width="672" /></p>
<p>Great, finally we can distinguish the data points representing humans
from those representing droids! On the left side, however, the panel
with the humans looks a bit empty. Maybe we should put them next to each
other.</p>
<div class="sourceCode" id="cb161"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb161-1"><a href="tutorial-data-visualization-with-ggplot.html#cb161-1" aria-hidden="true" tabindex="-1"></a>human_droid_data <span class="sc">%>%</span></span>
<span id="cb161-2"><a href="tutorial-data-visualization-with-ggplot.html#cb161-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x =</span> height, <span class="at">y =</span> mass, <span class="at">size=</span>birth_year)) <span class="sc">+</span></span>
<span id="cb161-3"><a href="tutorial-data-visualization-with-ggplot.html#cb161-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="at">shape =</span> <span class="dv">21</span>, <span class="at">fill =</span> <span class="st">"black"</span>, <span class="at">alpha =</span> <span class="fl">0.25</span>, <span class="at">color =</span> <span class="st">"black"</span>) <span class="sc">+</span></span>
<span id="cb161-4"><a href="tutorial-data-visualization-with-ggplot.html#cb161-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">limits =</span> <span class="fu">c</span>(<span class="dv">30</span>, <span class="dv">140</span>)) <span class="sc">+</span> </span>
<span id="cb161-5"><a href="tutorial-data-visualization-with-ggplot.html#cb161-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_continuous</span>(<span class="at">limits =</span> <span class="fu">c</span>(<span class="dv">90</span>, <span class="dv">210</span>)) <span class="sc">+</span></span>
<span id="cb161-6"><a href="tutorial-data-visualization-with-ggplot.html#cb161-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">breaks =</span> <span class="fu">c</span>(<span class="dv">40</span>, <span class="dv">60</span>, <span class="dv">80</span>, <span class="dv">100</span>, <span class="dv">120</span>, <span class="dv">140</span>, <span class="dv">160</span>)) <span class="sc">+</span> </span>
<span id="cb161-7"><a href="tutorial-data-visualization-with-ggplot.html#cb161-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_continuous</span>(<span class="at">breaks =</span> <span class="fu">c</span>(<span class="dv">100</span>, <span class="dv">120</span>, <span class="dv">140</span>, <span class="dv">160</span>, <span class="dv">180</span>, <span class="dv">200</span>)) <span class="sc">+</span></span>
<span id="cb161-8"><a href="tutorial-data-visualization-with-ggplot.html#cb161-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_size</span>(<span class="at">range =</span> <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">11</span>), <span class="at">name =</span> <span class="st">"age"</span>) <span class="sc">+</span></span>
<span id="cb161-9"><a href="tutorial-data-visualization-with-ggplot.html#cb161-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_bw</span>() <span class="sc">+</span></span>
<span id="cb161-10"><a href="tutorial-data-visualization-with-ggplot.html#cb161-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">title =</span> <span class="st">"Mass vs. height of humans and droids in Star Wars"</span>,</span>
<span id="cb161-11"><a href="tutorial-data-visualization-with-ggplot.html#cb161-11" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> <span class="st">"Height (cm)"</span>, <span class="at">y =</span> <span class="st">"Weight (kg)"</span>) <span class="sc">+</span></span>
<span id="cb161-12"><a href="tutorial-data-visualization-with-ggplot.html#cb161-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">facet_wrap</span>(<span class="sc">~</span>species) <span class="co"># nrow=1 is the default, so you don´t have to call it explicitly</span></span></code></pre></div>
<p><img src="PR-Erfolgskontrolle_files/figure-html/unnamed-chunk-89-1.png" width="672" /></p>
<p>I like that!</p>
</div>
<div id="saving-graphs" class="section level3 hasAnchor" number="5.4.8">
<h3><span class="header-section-number">5.4.8</span> Saving graphs<a href="tutorial-data-visualization-with-ggplot.html#saving-graphs" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>I think it’s time that we save this plot. To finish of this
“masterpiece” (and make it less triste), let’s add some final colors
before saving. We’ll fill our bubbles with colorful ink based on the
<em>species</em> variable, so we need to add <code>fill=species</code> to the <code>aes()</code> and
remove the default black ink provided in the <code>geom_point()</code> function.</p>
<div class="sourceCode" id="cb162"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb162-1"><a href="tutorial-data-visualization-with-ggplot.html#cb162-1" aria-hidden="true" tabindex="-1"></a>human_droid_data <span class="sc">%>%</span></span>
<span id="cb162-2"><a href="tutorial-data-visualization-with-ggplot.html#cb162-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x =</span> height, <span class="at">y =</span> mass, <span class="at">size=</span>birth_year, <span class="at">fill=</span>species)) <span class="sc">+</span></span>
<span id="cb162-3"><a href="tutorial-data-visualization-with-ggplot.html#cb162-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="at">shape =</span> <span class="dv">21</span>, <span class="at">alpha =</span> <span class="fl">0.25</span>, <span class="at">color =</span> <span class="st">"black"</span>) <span class="sc">+</span></span>
<span id="cb162-4"><a href="tutorial-data-visualization-with-ggplot.html#cb162-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">limits =</span> <span class="fu">c</span>(<span class="dv">30</span>, <span class="dv">140</span>)) <span class="sc">+</span> </span>
<span id="cb162-5"><a href="tutorial-data-visualization-with-ggplot.html#cb162-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_continuous</span>(<span class="at">limits =</span> <span class="fu">c</span>(<span class="dv">90</span>, <span class="dv">210</span>)) <span class="sc">+</span></span>
<span id="cb162-6"><a href="tutorial-data-visualization-with-ggplot.html#cb162-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">breaks =</span> <span class="fu">c</span>(<span class="dv">40</span>, <span class="dv">60</span>, <span class="dv">80</span>, <span class="dv">100</span>, <span class="dv">120</span>, <span class="dv">140</span>, <span class="dv">160</span>)) <span class="sc">+</span> </span>
<span id="cb162-7"><a href="tutorial-data-visualization-with-ggplot.html#cb162-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_continuous</span>(<span class="at">breaks =</span> <span class="fu">c</span>(<span class="dv">100</span>, <span class="dv">120</span>, <span class="dv">140</span>, <span class="dv">160</span>, <span class="dv">180</span>, <span class="dv">200</span>)) <span class="sc">+</span></span>
<span id="cb162-8"><a href="tutorial-data-visualization-with-ggplot.html#cb162-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_size</span>(<span class="at">range =</span> <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">11</span>), <span class="at">name =</span> <span class="st">"age"</span>) <span class="sc">+</span></span>
<span id="cb162-9"><a href="tutorial-data-visualization-with-ggplot.html#cb162-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_bw</span>() <span class="sc">+</span></span>
<span id="cb162-10"><a href="tutorial-data-visualization-with-ggplot.html#cb162-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">title =</span> <span class="st">"Mass vs. height of humans and droids in Star Wars"</span>,</span>
<span id="cb162-11"><a href="tutorial-data-visualization-with-ggplot.html#cb162-11" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> <span class="st">"Height (cm)"</span>, <span class="at">y =</span> <span class="st">"Weight (kg)"</span>) <span class="sc">+</span></span>
<span id="cb162-12"><a href="tutorial-data-visualization-with-ggplot.html#cb162-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">facet_wrap</span>(<span class="sc">~</span>species)</span></code></pre></div>
<p><img src="PR-Erfolgskontrolle_files/figure-html/unnamed-chunk-90-1.png" width="672" /></p>
<p>Congratulations! You have just managed to recreate the plot from the
beginning of this tutorial! The only thing we haven’t covered yet is the
labeling of all data points, because for that you’d need the <code>ggrepel</code>
package to not mess up the labels - and that’s not part of <code>ggplot2</code>. So
let’s skip that and save our graph.</p>
<p>To use your graph in another document, e.g. a theses written in Word,
you’ll have to export the plot first. Therefore, you must assign your
plot to a new object and call the <code>ggsave()</code> function on that object.
The plot will be saved to your <strong>working directory</strong> and formatted
according to the <strong>file extension</strong> you specified (for example: .jpeg or
.png).</p>
<div class="sourceCode" id="cb163"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb163-1"><a href="tutorial-data-visualization-with-ggplot.html#cb163-1" aria-hidden="true" tabindex="-1"></a>plot <span class="ot"><-</span> human_droid_data <span class="sc">%>%</span></span>
<span id="cb163-2"><a href="tutorial-data-visualization-with-ggplot.html#cb163-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x =</span> height, <span class="at">y =</span> mass, <span class="at">size=</span>birth_year, <span class="at">fill=</span>species)) <span class="sc">+</span></span>
<span id="cb163-3"><a href="tutorial-data-visualization-with-ggplot.html#cb163-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="at">shape =</span> <span class="dv">21</span>, <span class="at">alpha =</span> <span class="fl">0.25</span>, <span class="at">color =</span> <span class="st">"black"</span>) <span class="sc">+</span></span>
<span id="cb163-4"><a href="tutorial-data-visualization-with-ggplot.html#cb163-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">limits =</span> <span class="fu">c</span>(<span class="dv">30</span>, <span class="dv">140</span>)) <span class="sc">+</span> </span>
<span id="cb163-5"><a href="tutorial-data-visualization-with-ggplot.html#cb163-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_continuous</span>(<span class="at">limits =</span> <span class="fu">c</span>(<span class="dv">90</span>, <span class="dv">210</span>)) <span class="sc">+</span></span>
<span id="cb163-6"><a href="tutorial-data-visualization-with-ggplot.html#cb163-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">breaks =</span> <span class="fu">c</span>(<span class="dv">40</span>, <span class="dv">60</span>, <span class="dv">80</span>, <span class="dv">100</span>, <span class="dv">120</span>, <span class="dv">140</span>, <span class="dv">160</span>)) <span class="sc">+</span> </span>
<span id="cb163-7"><a href="tutorial-data-visualization-with-ggplot.html#cb163-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_continuous</span>(<span class="at">breaks =</span> <span class="fu">c</span>(<span class="dv">100</span>, <span class="dv">120</span>, <span class="dv">140</span>, <span class="dv">160</span>, <span class="dv">180</span>, <span class="dv">200</span>)) <span class="sc">+</span></span>
<span id="cb163-8"><a href="tutorial-data-visualization-with-ggplot.html#cb163-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_size</span>(<span class="at">range =</span> <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">11</span>), <span class="at">name =</span> <span class="st">"age"</span>) <span class="sc">+</span></span>
<span id="cb163-9"><a href="tutorial-data-visualization-with-ggplot.html#cb163-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_bw</span>() <span class="sc">+</span></span>
<span id="cb163-10"><a href="tutorial-data-visualization-with-ggplot.html#cb163-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">title =</span> <span class="st">"Mass vs. height of humans and droids in Star Wars"</span>,</span>
<span id="cb163-11"><a href="tutorial-data-visualization-with-ggplot.html#cb163-11" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> <span class="st">"Height (cm)"</span>, <span class="at">y =</span> <span class="st">"Weight (kg)"</span>) <span class="sc">+</span></span>
<span id="cb163-12"><a href="tutorial-data-visualization-with-ggplot.html#cb163-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">facet_wrap</span>(<span class="sc">~</span>species)</span>
<span id="cb163-13"><a href="tutorial-data-visualization-with-ggplot.html#cb163-13" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb163-14"><a href="tutorial-data-visualization-with-ggplot.html#cb163-14" aria-hidden="true" tabindex="-1"></a><span class="fu">ggsave</span>(<span class="at">filename =</span> <span class="st">"mass_vs_height.jpeg"</span>, plot)</span></code></pre></div>
</div>
</div>
<div id="other-common-plot-types" class="section level2 hasAnchor" number="5.5">
<h2><span class="header-section-number">5.5</span> Other common plot types<a href="tutorial-data-visualization-with-ggplot.html#other-common-plot-types" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>I can’t give an overview of all possible types of plots, but I can at
least touch a bit on how other common types of <code>geom_</code> behave.</p>
<div id="bar-plots" class="section level3 hasAnchor" number="5.5.1">
<h3><span class="header-section-number">5.5.1</span> bar plots<a href="tutorial-data-visualization-with-ggplot.html#bar-plots" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Bar plots are very common. They are either (1) used to display the
frequency with which a certain factor level of a categorical variable
occurs or (2) to display relationships between a categorical variable
and a metric variable.</p>
<p>So let’s create a quick bar plot using the <em>sex</em> variable (categorical,
three factor levels) and get an overview on how many human and droidic
Star Wars characters are male, female, or do not have a sex.</p>
<div class="sourceCode" id="cb164"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb164-1"><a href="tutorial-data-visualization-with-ggplot.html#cb164-1" aria-hidden="true" tabindex="-1"></a>human_droid_data <span class="sc">%>%</span></span>
<span id="cb164-2"><a href="tutorial-data-visualization-with-ggplot.html#cb164-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x =</span> sex)) <span class="sc">+</span> <span class="co"># We only have to specify the variable that we want to get the count for (i.e. number of observations)</span></span>
<span id="cb164-3"><a href="tutorial-data-visualization-with-ggplot.html#cb164-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_bar</span>()</span></code></pre></div>
<p><img src="PR-Erfolgskontrolle_files/figure-html/unnamed-chunk-92-1.png" width="60%" /></p>
<p>Next, let’s look at the relationship between <em>sex</em> and the <em>height</em>
(metric) variable. We will produce a bar plot that displays the mean
height of each group:</p>
<div class="sourceCode" id="cb165"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb165-1"><a href="tutorial-data-visualization-with-ggplot.html#cb165-1" aria-hidden="true" tabindex="-1"></a>human_droid_data <span class="sc">%>%</span></span>
<span id="cb165-2"><a href="tutorial-data-visualization-with-ggplot.html#cb165-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x =</span> sex, <span class="at">y=</span>height)) <span class="sc">+</span> <span class="co"># Now we need to specify the variable that we want to summarize with mean statistics </span></span>
<span id="cb165-3"><a href="tutorial-data-visualization-with-ggplot.html#cb165-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_bar</span>(<span class="at">stat =</span> <span class="st">"summary"</span>, <span class="at">fun.y =</span> <span class="st">"mean"</span>) <span class="co"># apply the summary statistic of y (mean) to the geom_bars</span></span></code></pre></div>
<p><img src="PR-Erfolgskontrolle_files/figure-html/unnamed-chunk-93-1.png" width="60%" /></p>
<p>Maybe we want to sort the bars according to their mean. Let’s reorder
the factor levels manually.</p>
<div class="sourceCode" id="cb166"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb166-1"><a href="tutorial-data-visualization-with-ggplot.html#cb166-1" aria-hidden="true" tabindex="-1"></a>human_droid_data <span class="sc">%>%</span></span>
<span id="cb166-2"><a href="tutorial-data-visualization-with-ggplot.html#cb166-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">sex =</span> <span class="fu">factor</span>(sex, <span class="at">levels =</span> <span class="fu">c</span>(<span class="st">"male"</span>, <span class="st">"female"</span>, <span class="st">"none"</span>))) <span class="sc">%>%</span> </span>
<span id="cb166-3"><a href="tutorial-data-visualization-with-ggplot.html#cb166-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x =</span> sex, <span class="at">y=</span>height)) <span class="sc">+</span></span>
<span id="cb166-4"><a href="tutorial-data-visualization-with-ggplot.html#cb166-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">stat_summary</span>(<span class="at">geom =</span> <span class="st">"bar"</span>, <span class="at">fun =</span> <span class="st">"mean"</span>)</span></code></pre></div>
<p><img src="PR-Erfolgskontrolle_files/figure-html/unnamed-chunk-94-1.png" width="60%" /></p>
<p>And if we would like to have a horizontal bar plot, we can use the
<code>coord_</code> component of <code>ggplot()</code> to flip the coordinates.</p>
<div class="sourceCode" id="cb167"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb167-1"><a href="tutorial-data-visualization-with-ggplot.html#cb167-1" aria-hidden="true" tabindex="-1"></a>human_droid_data <span class="sc">%>%</span></span>
<span id="cb167-2"><a href="tutorial-data-visualization-with-ggplot.html#cb167-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">sex =</span> <span class="fu">factor</span>(sex, <span class="at">levels =</span> <span class="fu">c</span>(<span class="st">"male"</span>, <span class="st">"female"</span>, <span class="st">"none"</span>))) <span class="sc">%>%</span> </span>
<span id="cb167-3"><a href="tutorial-data-visualization-with-ggplot.html#cb167-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x =</span> sex, <span class="at">y=</span>height)) <span class="sc">+</span></span>
<span id="cb167-4"><a href="tutorial-data-visualization-with-ggplot.html#cb167-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">stat_summary</span>(<span class="at">geom =</span> <span class="st">"bar"</span>, <span class="at">fun =</span> <span class="st">"mean"</span>) <span class="sc">+</span></span>
<span id="cb167-5"><a href="tutorial-data-visualization-with-ggplot.html#cb167-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">coord_flip</span>()</span></code></pre></div>
<p><img src="PR-Erfolgskontrolle_files/figure-html/unnamed-chunk-95-1.png" width="60%" /></p>
</div>
<div id="box-plots" class="section level3 hasAnchor" number="5.5.2">
<h3><span class="header-section-number">5.5.2</span> box plots<a href="tutorial-data-visualization-with-ggplot.html#box-plots" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Box plots are a great option to summarize metric variables (by groups).
They provide you with the Five-number-summary:</p>
<ul>
<li>the sample minimum (smallest observation) – lower whisker</li>
<li>the lower quartile – lower end of the box</li>
<li>the median (the middle value) – thick black line</li>
<li>the upper quartile – upper end of the box</li>
<li>the sample maximum (largest observation) – upper whisker</li>
</ul>
<p>Let’s create box plots of the height for human and droidic Star Wars
characters who are male, female, or do not have a sex.</p>
<div class="sourceCode" id="cb168"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb168-1"><a href="tutorial-data-visualization-with-ggplot.html#cb168-1" aria-hidden="true" tabindex="-1"></a>human_droid_data <span class="sc">%>%</span></span>
<span id="cb168-2"><a href="tutorial-data-visualization-with-ggplot.html#cb168-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x =</span> sex, <span class="at">y=</span>height)) <span class="sc">+</span></span>
<span id="cb168-3"><a href="tutorial-data-visualization-with-ggplot.html#cb168-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_boxplot</span>()</span></code></pre></div>
<p><img src="PR-Erfolgskontrolle_files/figure-html/unnamed-chunk-96-1.png" width="60%" /></p>
</div>
</div>
<div id="take-aways-4" class="section level2 hasAnchor" number="5.6">
<h2><span class="header-section-number">5.6</span> Take Aways<a href="tutorial-data-visualization-with-ggplot.html#take-aways-4" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<ul>
<li><strong>graph creation</strong>: <code>ggplot()</code></li>
<li><strong>mapping variables to aesthetics</strong>:
<code>aes(x, y, color, fill, size, etc.)</code></li>
<li><strong>chart type</strong>: <code>geom_bar()</code>, <code>geom_line()</code>, <code>geom_point()</code>,
<code>geom_boxplot()</code> (for example)</li>
<li><strong>titles</strong>: <code>labs()</code></li>
<li><strong>axis limits/ticks</strong>: <code>scale_x_continuous()</code>,
<code>scale_y_continuous()</code></li>
<li><strong>mapping variables to geom size</strong>: <code>scale_size()</code></li>
<li><strong>themes</strong>: <code>theme_classic()</code>, <code>theme_light()</code>, <code>theme_bw()</code> (for
example)</li>
<li><strong>faceting</strong>: <code>facet_wrap()</code> or <code>facet_grid</code></li>
<li><strong>save images</strong>: <code>ggsave()</code></li>
</ul>
</div>
<div id="additional-tutorials-4" class="section level2 hasAnchor" number="5.7">
<h2><span class="header-section-number">5.7</span> Additional tutorials<a href="tutorial-data-visualization-with-ggplot.html#additional-tutorials-4" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>You still have questions? The following tutorials & papers can help you
with that:</p>
<ul>
<li>Chang, W. R (2021). <em>R Graphics Codebook. Practical Recipes for
Visualizing Data.</em> <a href="https://r-graphics.org/">Link</a></li>
<li>Wickham, H., Navarro, D., & Pedersen, T. L. (2021). <em>ggplot2:
elegant graphics for data analysis.</em> Online, work-in-progress
version of the 3rd edition. <a href="https://ggplot2-book.org/">Link</a></li>
<li>Hehman, E., & Xie, S. Y. (2021). <em>Doing Better Data Visualization.
Advances in Methods and Practices in Psychological Science</em>. DOI:
10.1177/25152459211045334
<a href="https://doi.org/10.1177/25152459211045334">Link</a></li>
<li><a href="https://rc2e.com/graphics">R Codebook by J.D. Long and P. Teetor, Tutorial
10</a></li>
</ul>
<p>Now let’s see what you’ve learned so far: <a href="exercise-3-test-your-knowledge.html#exercise-3-test-your-knowledge">Exercise 3: Test your
knowledge</a>.</p>
</div>
</div>
</section>
</div>
</div>
</div>
<a href="exercise-2-test-your-knowledge.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="exercise-3-test-your-knowledge.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
</div>
</div>
<script src="libs/gitbook-2.6.7/js/app.min.js"></script>
<script src="libs/gitbook-2.6.7/js/clipboard.min.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-search.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-sharing.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-fontsettings.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-bookdown.js"></script>
<script src="libs/gitbook-2.6.7/js/jquery.highlight.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-clipboard.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {