forked from anhnguyendepocen/PPBDS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01-visualization.html
More file actions
2149 lines (2082 loc) · 221 KB
/
Copy path01-visualization.html
File metadata and controls
2149 lines (2082 loc) · 221 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
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 name="generator" content="pandoc" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta property="og:title" content="01-visualization.utf8" />
<meta property="og:type" content="book" />
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
TeX: { equationNumbers: { autoNumber: "AMS" } }
});
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.2/MathJax.js?config=TeX-AMS_CHTML-full" type="text/javascript"></script>
<meta name="description" content="01-visualization.utf8">
<title>01-visualization.utf8</title>
<link href="libs/tufte-css-2015.12.29/tufte.css" rel="stylesheet" />
<link href="libs/tufte-css-2015.12.29/envisioned.css" rel="stylesheet" />
<link href="libs/msmb-css-0/msmb.css" rel="stylesheet" />
<script src="libs/accessible-code-block-0.0.1/empty-anchor.js"></script>
<script src="libs/kePrint-0.0.1/kePrint.js"></script>
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
code.sourceCode > span { display: inline-block; line-height: 1.25; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
code.sourceCode > span:empty { height: 1.2em; }
code.sourceCode { white-space: pre; position: relative; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
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 {
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>
</head>
<body>
<!--bookdown:toc:start-->
<div id="TOC">
<ul class="navbar">
<li class="msmb"><p class="title"><p><p class="author"></p>
<li class="dropdown" style="float:right">
<a href="javascript:void(0)" class="dropbtn">▾ Chapters</a>
<div class="dropdown-content">
<a href="#visualization"><span class="toc-section-number">1</span> Visualization</a>
<a href="#what-are-r-and-rstudio"><span class="toc-section-number">2</span> What are R and RStudio?</a>
<a href="#how-do-i-code-in-r"><span class="toc-section-number">3</span> How do I code in R?</a>
<a href="#what-are-r-packages"><span class="toc-section-number">4</span> What are R packages?</a>
<a href="#explore-your-first-datasets"><span class="toc-section-number">5</span> Explore your first datasets</a>
<a href="#data-visualization-using-the-ggplot2-package"><span class="toc-section-number">6</span> Data Visualization using the ggplot2 Package</a>
<a href="#summary-of-geoms"><span class="toc-section-number">7</span> Summary of Geoms</a>
<a href="#scatterplots"><span class="toc-section-number">8</span> Scatterplots</a>
<a href="#linegraphs"><span class="toc-section-number">9</span> Linegraphs</a>
<a href="#histograms"><span class="toc-section-number">10</span> Histograms</a>
<a href="#boxplots"><span class="toc-section-number">11</span> Boxplots</a>
<a href="#barplots"><span class="toc-section-number">12</span> Barplots</a>
<a href="#more-layers"><span class="toc-section-number">13</span> More Layers</a>
<a href="#using-functions-from-the-tidyverse"><span class="toc-section-number">14</span> Using functions from the Tidyverse</a>
<a href="#data-types"><span class="toc-section-number">15</span> Data Types</a>
<a href="#importing-data"><span class="toc-section-number">16</span> Importing data</a>
</div>
</li>
</ul>
</div>
<!--bookdown:toc:end-->
<!--bookdown:body:start-->
<div id="visualization" class="section level1">
<h1><span class="header-section-number">Chapter 1</span> Visualization</h1>
<!-- Finalize Tufte recommendation. Settings for images? Wickham may have some advice. E-mail Kieran Healy and ask to see one of his Rmd files and his book_yaml. -->
<!-- Use r scales::comma() throughout -->
<!-- Get rid of knitr. Replace with **gt** package. We also use kable to make tables which we just show students. OK. Use gt for all those. -->
<!-- Do we really need gapminder and nycflights13? Why? I could imagine adding a special data set from PPBDS.data, but I won't want to overwhelm the students? FEC data might be a good thing to replace nycflights with since it can come with a bunch of tables. -->
<!-- Why are we doing any wrangling, as with gapminder? That is next week's project. Instead, we should only be doing graphics, with tibbles that require no wrangling. -->
<!-- Get rid of tiny sections, like ## Summary at line 770. There is no reason to have a subsection with only two sentences in it. Indeed, I don't think we need nearly as many sections as we have. At the same time, we do need to think about structure: how many sections, how many subsections, how long is each and so on. -->
<!-- * Should we start using webshot() to capture webpages? Thereby providing more detail. -->
<!-- * We should delete stuff that we do not want to maintain and that others are committed (?) to maintaining. So, why not direct students to *Friendly Git for the R User* for information on installation? -->
<!-- Include the current chapter 1 and 2. Lots of prettiness. Must give enough detail that you can do Congressional age exercise. -->
<!-- Give a tour of other cool graphics packages, including ggrepel, gghighlights. -->
<!-- Should have faceting and other graphics fun. -->
<!-- ggrepel: https://ggrepel.slowkow.com/articles/examples.html -->
<!-- Teach this trick: ggplot() + -->
<!-- <thing> + -->
<!-- <next thing> + -->
<!-- NULL -->
<!-- Add section on STYLE USING GGPLOT2 - How to add key parts to a graphic (title, subtitle, caption), Themes, ggtext, axis, axis adjustments -->
<!-- ::: {.fullwidth} -->
<p>Before we can start exploring data in R, there are some key concepts to understand first:</p>
<ol style="list-style-type: decimal">
<li>What are R and RStudio?</li>
<li>How do I code in R?</li>
<li>What are R packages?</li>
</ol>
<!-- What's the best way to deal with references? Should I leave them as is for now and then check if they're correct later? -->
<!-- Do we want to consider changing this first dataset to something more interesting and more gov related?? -->
<p>We’ll introduce these concepts in the upcoming sections. If you are already somewhat familiar with these concepts, feel free to skip to later in the chapter where we’ll introduce our first dataset: data for attitudes toward immigration-related policies.
<!-- ::: --></p>
</div>
<div id="what-are-r-and-rstudio" class="section level1">
<h1><span class="header-section-number">Chapter 2</span> What are R and RStudio?</h1>
<p>
<span class="marginnote shownote">
<!--
<div class="figure">--><span id="fig:unnamed-chunk-2"></span>
<img src="01-visualization/images/R_vs_RStudio_1.png" alt="Analogy of difference between R and RStudio." />
<!--
<p class="caption marginnote">-->FIGURE 2.1: Analogy of difference between R and RStudio.<!--</p>-->
<!--</div>--></span>
</p>
<p>Throughout this book, we assume that you are using R via RStudio. First time users often confuse the two. At its simplest, R is like a car’s engine while RStudio is like a car’s dashboard.</p>
<p>More precisely, R is a programming language that runs computations, while RStudio is an <em>integrated development environment (IDE)</em> that provides an interface by adding many convenient features and tools. So just as the way of having access to a speedometer, rearview mirrors, and a navigation system makes driving much easier, using RStudio’s interface makes using R much easier as well.</p>
</div>
<div id="installing-r-and-rstudio" class="section level2">
<h2><span class="header-section-number">2.1</span> Installing R and RStudio</h2>
<!-- ::: {.fullwidth} -->
<p>You will first need to download and install both R and RStudio (Desktop version) on your computer. It is important that you install R first and then install RStudio.</p>
<ol style="list-style-type: decimal">
<li><p><strong>You must do this first:</strong> Download and install R by going to <a href="https://cloud.r-project.org/" class="uri">https://cloud.r-project.org/</a>. </p></li>
<li><p><strong>You must do this second:</strong> Download and install RStudio at <a href="https://www.rstudio.com/products/rstudio/download/" class="uri">https://www.rstudio.com/products/rstudio/download/</a>.
<!-- ::: --></p></li>
</ol>
</div>
<div id="using-r-via-rstudio" class="section level2">
<h2><span class="header-section-number">2.2</span> Using R via RStudio</h2>
<p>
<span class="marginnote shownote">
<!--
<div class="figure">--><span id="fig:unnamed-chunk-4"></span>
<img src="01-visualization/images/R_vs_RStudio.png" alt="Icons of R versus RStudio on your computer." />
<!--
<p class="caption marginnote">-->FIGURE 2.2: Icons of R versus RStudio on your computer.<!--</p>-->
<!--</div>--></span>
</p>
<p>Recall our car analogy from earlier. Much as we don’t drive a car by interacting directly with the engine but rather by interacting with elements on the car’s dashboard, we won’t be using R directly but rather we will use RStudio’s interface. After you install R and RStudio on your computer, you’ll have two new <em>programs</em> (also called <em>applications</em>) you can open. We’ll always work in RStudio and not in the R application.</p>
<p>
<span class="marginnote shownote">
<!--
<div class="figure">--><span id="fig:unnamed-chunk-5"></span>
<img src="01-visualization/images/rstudio.png" alt="RStudio interface to R." />
<!--
<p class="caption marginnote">-->FIGURE 2.3: RStudio interface to R.<!--</p>-->
<!--</div>--></span>
</p>
<p><label for="tufte-mn-1" class="margin-toggle">⊕</label><input type="checkbox" id="tufte-mn-1" class="margin-toggle"><span class="marginnote"><span style="display: block;">(Note that slight differences might exist from the figure if the RStudio interface is updated after 2019 to not be this by default.)</span></span></p>
<p>After you open RStudio, you should see three <em>panes</em>, or panels, dividing the screen: the <em>console pane</em>, the <em>files pane</em>, and the <em>environment pane</em>. Over the course of this chapter, you’ll come to learn what purpose each of these panes serves.</p>
</div>
<!-- </div> -->
<div id="how-do-i-code-in-r" class="section level1">
<h1><span class="header-section-number">Chapter 3</span> How do I code in R?</h1>
<p>Now that you’re set up with R and RStudio, you are probably asking yourself, “OK. Now how do I use R?”. The first thing to note is that unlike other statistical software programs like Excel, SPSS, or Minitab that provide <a href="https://en.wikipedia.org/wiki/Point_and_click">point-and-click</a> interfaces, R is an <a href="https://en.wikipedia.org/wiki/Interpreted_language">interpreted language</a>. This means you have to type in commands written in <em>R code</em>. In other words, you have to code/program in R. Note that we’ll use the terms “coding” and “programming” interchangeably in this book.</p>
<p><label for="tufte-mn-2" class="margin-toggle">⊕</label><input type="checkbox" id="tufte-mn-2" class="margin-toggle"><span class="marginnote"><span style="display: block;">If you are new to the world of coding, R, and RStudio and feel you could benefit from a more detailed introduction, we suggest you check out the short book, <a href="https://rbasics.netlify.com/"><em>Getting Used to R, RStudio, and R Markdown</em></a>. It includes screencast recordings that you can follow along and pause as you learn. This book also contains an introduction to R Markdown, a tool used for reproducible research in R.</span></span></p>
<p>While it is not required to be a seasoned coder/computer programmer to use R, there is still a set of basic programming concepts that new R users need to understand.</p>
</div>
<div id="tips-on-learning-to-code" class="section level2">
<h2><span class="header-section-number">3.1</span> Tips on learning to code</h2>
<p>Learning to code/program is quite similar to learning a foreign language. It can be daunting and frustrating at first. Such frustrations are common and it is normal to feel discouraged as you learn. However, just as with learning a foreign language, if you put in the effort and are not afraid to make mistakes, anybody can learn and improve.</p>
<!-- ::: {.fullwidth} -->
<p>Here are a few useful tips to keep in mind as you learn to program:</p>
<ul>
<li><strong>Remember that computers are not actually that smart</strong>: You may think your computer or smartphone is “smart,” but really people spent a lot of time and energy designing them to appear “smart.” In reality, you have to tell a computer everything it needs to do. Furthermore, the instructions you give your computer can’t have any mistakes in them, nor can they be ambiguous in any way.</li>
<li><strong>Take the “copy, paste, and tweak” approach</strong>: Especially when you learn your first programming language or you need to understand particularly complicated code, it is often much easier to take existing code that you know works and modify it to suit your ends. This is as opposed to trying to type out the code from scratch. We call this the <em>“copy, paste, and tweak”</em> approach. So early on, we suggest not trying to write code from memory, but rather take existing examples we have provided you, then copy, paste, and tweak them to suit your goals. After you start feeling more confident, you can slowly move away from this approach and write code from scratch. Think of the “copy, paste, and tweak” approach as training wheels for learning to ride a bike. After getting comfortable, you won’t need them anymore.</li>
<li><strong>The best way to learn to code is by doing</strong>: Rather than learning to code for its own sake, we find that learning to code goes much smoother when you have a goal in mind or when you are working on a particular project, like analyzing data that you are interested in and that is important to you.</li>
<li><strong>Practice is key</strong>: Just as the only method to improve your foreign language skills is through lots of practice and speaking, the only method to improving your coding skills is through lots of practice. Write R code every day.</li>
</ul>
<!-- ::: -->
</div>
<div id="basic-programming-concepts-and-terminology" class="section level2">
<h2><span class="header-section-number">3.2</span> Basic programming concepts and terminology</h2>
<!-- ::: {.fullwidth} -->
<p>We now introduce some basic programming concepts and terminology. Instead of asking you to memorize all these concepts and terminology right now, we’ll guide you so that you’ll “learn by doing.” To help you learn, we will always use a different font to distinguish regular text from <code>computer_code</code>. The best way to master these topics is, in our opinions, through <a href="https://jamesclear.com/deliberate-practice-theory">deliberate practice</a> with R and lots of repetition.</p>
<ul>
<li><em>Console pane</em>: where you enter in commands.</li>
<li><em>Running code</em>: the act of telling R to perform an act by giving it commands in the console.</li>
<li><em>Objects</em>: where values are saved in R. We’ll show you how to <em>assign</em> values to objects and how to display the contents of objects.</li>
<li><em>Data types</em>: integers, doubles/numerics, logicals, and characters. Integers are values like -1, 0, 2, 4092. Doubles or numerics are a larger set of values containing both the integers but also fractions and decimal values like -24.932 and 0.8. Logicals are either <code>TRUE</code> or <code>FALSE</code> while characters are text such as “cabbage”, “Hamilton”, “The Wire is the greatest TV show ever”, and “This ramen is delicious.” Note that characters are often denoted with the quotation marks around them.</li>
<li><em>Vectors</em>: a series of values. These are created using the <code>c()</code> function, where <code>c()</code> stands for “combine” or “concatenate.” For example, <code>c(6, 11, 13, 31, 90, 92)</code> creates a six element series of positive integer values.</li>
<li><em>Factors</em>: <em>categorical data</em> are commonly represented in R as factors. Categorical data can also be represented as <em>strings</em>. We’ll study this difference as we progress through the book.</li>
<li><em>Data frames</em>: rectangular spreadsheets. They are representations of datasets in R where the rows correspond to <em>observations</em> and the columns correspond to <em>variables</em> that describe the observations. Modern data frames are called <em>tibbles</em>.<br />
</li>
<li><em>Conditionals</em>:
<ul>
<li>Testing for equality in R using <code>==</code> (and not <code>=</code>, which is typically used for assignment). For example, <code>2 + 1 == 3</code> compares <code>2 + 1</code> to <code>3</code> and is correct R code, while <code>2 + 1 = 3</code> will return an error.</li>
<li>Boolean algebra: <code>TRUE/FALSE</code> statements and mathematical operators such as <code><</code> (less than), <code><=</code> (less than or equal), and <code>!=</code> (not equal to). For example, <code>4 + 2 >= 3</code> will return <code>TRUE</code>, but <code>3 + 5 <= 1</code> will return <code>FALSE</code>.</li>
<li>Testing for inclusion with the <code>%in%</code> operator. For example, <code>"B" %in% c("A", "B")</code> returns <code>TRUE</code> while <code>"C" %in% c("A", "B")</code> returns <code>FALSE</code>.</li>
<li>Logical operators: <code>&</code> representing “and” as well as <code>|</code> representing “or.” For example, <code>(2 + 1 == 3) & (2 + 1 == 4)</code> returns <code>FALSE</code> since both clauses are not <code>TRUE</code> (only the first clause is <code>TRUE</code>). On the other hand, <code>(2 + 1 == 3) | (2 + 1 == 4)</code> returns <code>TRUE</code> since at least one of the two clauses is <code>TRUE</code>.</li>
</ul></li>
<li><em>Functions</em>, also called <em>commands</em>: perform tasks in R. They take in inputs called <em>arguments</em> and return outputs. You can either manually specify a function’s arguments or use the function’s <em>default values</em>.
<ul>
<li>For example, the function <code>seq()</code> in R generates a sequence of numbers. If you just run <code>seq()</code> it will return the value 1. That doesn’t seem very useful! This is because the default arguments are set as <code>seq(from = 1, to = 1)</code>. Thus, if you don’t pass in different values for <code>from</code> and <code>to</code> to change this behavior, R just assumes all you want is the number 1. You can change the argument values by updating the values after the <code>=</code> sign. If we try out <code>seq(from = 2, to = 5)</code> we get the result <code>2 3 4 5</code>, as we would expect.</li>
<li>We’ll work with functions a lot throughout this book and you’ll get lots of practice in understanding their behaviors. To further assist you in understanding when a function is mentioned in the book, we’ll also include the <code>()</code> after them as we did with <code>seq()</code> above.</li>
</ul></li>
<li><em>Help files</em>: provide documentation for various functions and datasets. You can bring up help files by adding a <code>?</code> before the name of a function or data frame and then run this in the console. You will then be presented with a page showing the corresponding documentation if it exists.</li>
</ul>
<p>This list is by no means an exhaustive list of all the programming concepts and terminology needed to become a savvy R user; such a list would be so large it wouldn’t be very useful, especially for novices. Rather, we feel this is a minimal list of programming concepts and terminology you need to know before getting started. We feel that you can learn the rest as you go. Remember that your mastery of all of these concepts and terminology will build as you practice.
<!-- ::: --></p>
</div>
<div id="errors-warnings-and-messages" class="section level2">
<h2><span class="header-section-number">3.3</span> Errors, warnings, and messages</h2>
<!-- ::: {.fullwidth} -->
<p>One thing that intimidates new R and RStudio users is how it reports <em>errors</em>, <em>warnings</em>, and <em>messages</em>. R reports errors, warnings, and messages in a glaring red font, which makes it seem like it is scolding you. However, seeing red text in the console is not always bad.</p>
<p>R will show red text in the console pane in three different situations:</p>
<ul>
<li><strong>Errors</strong>: When the red text is a legitimate error, it will be prefaced with “Error in…” and will try to explain what went wrong. Generally when there’s an error, the code will not run. For example, if you see <code>Error in ggplot(...) : could not find function "ggplot"</code>, it means that the <code>ggplot()</code> function is not accessible because the package that contains the function, <strong>ggplot2</strong>, was not loaded with <code>library(ggplot2)</code>. You cannot use the <code>ggplot()</code> function without the <strong>ggplot2</strong> package being loaded first.</li>
<li><strong>Warnings</strong>: When the red text is a warning, it will be prefaced with “Warning:” and R will try to explain why there’s a warning. Generally your code will still work, but with some caveats. If you create a scatterplot based on a dataset where two of the rows of data have missing entries, you will see this warning: <code>Warning: Removed 2 rows containing missing values (geom_point)</code>. R will still produce the scatterplot with all the remaining non-missing values, but it is warning you that two of the points aren’t there.</li>
<li><strong>Messages</strong>: When the red text doesn’t start with either “Error” or “Warning”, it’s <em>just a friendly message</em>. You’ll see these messages when you load <em>R packages</em> or when you read data saved in spreadsheet files with the <code>read_csv()</code> function as you’ll see in Chapter 2. These are helpful diagnostic messages. They don’t stop your code from working. Additionally, you’ll see these messages when you install packages too using <code>install.packages()</code>.</li>
</ul>
<p>Remember, when you see red text in the console, <em>don’t panic</em>. It doesn’t necessarily mean anything is wrong. Rather:</p>
<ul>
<li>If the text starts with “Error”, figure out what’s causing it. <span style="color:red">Think of errors as a red traffic light: something is wrong!</span></li>
<li>If the text starts with “Warning”, figure out if it’s something to worry about. For instance, if you get a warning about missing values in a scatterplot and you know there are missing values, you’re fine. If that’s surprising, look at your data and see what’s missing. <span style="color:gold">Think of warnings as a yellow traffic light: everything is working fine, but watch out/pay attention.</span></li>
<li>Otherwise, the text is just a message. Read it, wave back at R, and thank it for talking to you. <span style="color:green">Think of messages as a green traffic light: everything is working fine and keep on going!</span></li>
</ul>
<!-- ::: -->
</div>
<!-- </div> -->
<div id="what-are-r-packages" class="section level1">
<h1><span class="header-section-number">Chapter 4</span> What are R packages?</h1>
<p>Another point of confusion with many new R users is the idea of an R package. R packages extend the functionality of R by providing additional functions, data, and documentation. They are written by a worldwide community of R users and can be downloaded for free from the internet.</p>
<p>For example, among the many packages we will use in this book are the <strong>ggplot2</strong> package for data visualization and the <strong>dplyr</strong> package for data wrangling.</p>
<p>
<span class="marginnote shownote">
<!--
<div class="figure">--><span id="fig:unnamed-chunk-8"></span>
<img src="01-visualization/images/R_vs_R_packages.png" alt="Analogy of R versus R packages." />
<!--
<p class="caption marginnote">-->FIGURE 4.1: Analogy of R versus R packages.<!--</p>-->
<!--</div>--></span>
</p>
<p>R is like a new mobile phone: while it has a certain amount of features when you use it for the first time, it doesn’t have everything. R packages are like the apps you can download onto your phone from Apple’s App Store or Android’s Google Play.</p>
<p>Let’s continue this analogy by considering the Instagram app for editing and sharing pictures. Say you have purchased a new phone and you would like to share a photo you have just taken with friends on Instagram. You need to:</p>
<ol style="list-style-type: decimal">
<li><em>Install the app</em>: Since your phone is new and does not include the Instagram app, you need to download the app from either the App Store or Google Play. You do this once and you’re set for the time being. You might need to do this again in the future when there is an update to the app.</li>
<li><em>Open the app</em>: After you’ve installed Instagram, you need to open it.</li>
</ol>
<p>Once Instagram is open on your phone, you can then proceed to share your photo with your friends and family. The process is very similar for using an R package. You need to:</p>
<p>
<span class="marginnote shownote">
<!--
<div class="figure">--><span id="fig:unnamed-chunk-9"></span>
<img src="01-visualization/images/install_vs_load.jpg" alt="Installing versus loading an R package" />
<!--
<p class="caption marginnote">-->FIGURE 4.2: Installing versus loading an R package<!--</p>-->
<!--</div>--></span>
</p>
<ol style="list-style-type: decimal">
<li><em>Install the package</em>: This is like installing an app on your phone. Most packages are not installed by default when you install R and RStudio. Thus if you want to use a package for the first time, you need to install it first. Once you’ve installed a package, you likely won’t install it again unless you want to update it to a newer version.</li>
<li><em>“Load” the package</em>: “Loading” a package is like opening an app on your phone. Packages are not “loaded” by default when you start RStudio on your computer; you need to “load” each package you want to use every time you start RStudio.</li>
</ol>
<p>Let’s perform these two steps for the <strong>ggplot2</strong> package for data visualization.</p>
<!-- ::: {.fullwidth} -->
</div>
<div id="package-installation" class="section level2">
<h2><span class="header-section-number">4.1</span> Package installation</h2>
<p>Let’s install the <strong>ggplot2</strong> package. Type <code>install.packages("ggplot2")</code> in the console pane of RStudio and pressing Return/Enter on your keyboard. Note you must include the quotation marks around the name of the package.</p>
</div>
<div id="package-loading" class="section level2">
<h2><span class="header-section-number">4.2</span> Package loading</h2>
<p>Recall that after you’ve installed a package, you need to “load it.” In other words, you need to “open it.” We do this by using the <code>library()</code> command.</p>
<p>For example, to load the <strong>ggplot2</strong> package, run the following code in the console pane. What do we mean by “run the following code”? Either type or copy-and-paste the following code into the console pane and then hit the Enter key.</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1"></a><span class="kw">library</span>(ggplot2)</span></code></pre></div>
<p>If after running the earlier code, a blinking cursor returns next to the <code>></code> “prompt” sign, it means you were successful and the <strong>ggplot2</strong> package is now loaded and ready to use. If, however, you get a red “error message” that reads <code>...</code></p>
<pre><code>Error in library(ggplot2) : there is no package called ‘ggplot2’</code></pre>
<p><code>...</code> it means that you didn’t successfully install it. This is an example of an “error message”. If you get this error message, go back to the subsection on R package installation and make sure to install the <strong>ggplot2</strong> package before proceeding.</p>
</div>
<div id="package-use" class="section level2">
<h2><span class="header-section-number">4.3</span> Package use</h2>
<p>One very common mistake new R users make when wanting to use particular packages is they forget to “load” them first by using the <code>library()</code> command we just saw. Remember: <em>you have to load each package you want to use every time you start RStudio.</em> If you don’t first “load” a package, but attempt to use one of its features, you’ll see an error message similar to:</p>
<pre><code>Error: could not find function</code></pre>
<p>This is a different error message than the one you just saw on a package not having been installed yet. R is telling you that you are trying to use a function in a package that has not yet been “loaded.” R doesn’t know where to find the function you are using. Almost all new users forget to do this when starting out. However, you’ll remember with practice and after some time it will become second nature for you.
<!-- ::: --></p>
</div>
<!-- </div> -->
<div id="explore-your-first-datasets" class="section level1">
<h1><span class="header-section-number">Chapter 5</span> Explore your first datasets</h1>
<p>Let’s put everything we’ve learned so far into practice and start exploring some real data! Data comes to us in a variety of formats, from pictures to text to numbers. Throughout this book, we’ll focus on datasets that are saved in “spreadsheet”-type format. This is probably the most common way data are collected and saved in many fields. These “spreadsheet”-type datasets are called <em>data frames</em> in R. We’ll focus on working with data saved as data frames throughout this book. Again, “tibble” is the more modern term for “data frame,” but we will use both interchangeably.</p>
<p><label for="tufte-mn-3" class="margin-toggle">⊕</label><input type="checkbox" id="tufte-mn-3" class="margin-toggle"><span class="marginnote"><span style="display: block;">The PPBDS.data package was created specifically for this textbook and contains datasets used throughout the book. The package can be installed directly from GitHub using the command <code>remotes::install_github(“davidkane9/PPBDS.data”)</code>.</span></span></p>
<p>Let’s first load all the packages needed for this chapter, assuming you’ve already installed them.</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1"></a><span class="kw">library</span>(PPBDS.data)</span>
<span id="cb4-2"><a href="#cb4-2"></a><span class="kw">library</span>(dplyr)</span></code></pre></div>
</div>
<div id="trains-data-frame" class="section level2">
<h2><span class="header-section-number">5.1</span> <code>trains</code> data frame</h2>
<p><label for="tufte-mn-4" class="margin-toggle">⊕</label><input type="checkbox" id="tufte-mn-4" class="margin-toggle"><span class="marginnote"><span style="display: block;">See <a href="https://scholar.harvard.edu/files/renos/files/enostrains.pdf">“Causal effect of intergroup contact on attitudes,” by Ryan D. Enos, Proceedings of the National Academy of Sciences, Mar 2014, 111 (10)</a> for background and details on the <code>trains</code> dataset.</span></span></p>
<p>We’ll begin by exploring the <code>trains</code> data frame and get an idea of its structure. This dataset includes data for attitudes toward immigration-related policies, both before and after an experiment which randomly exposed a treated group to Spanish-speakers on a Boston commuter train platform. Individuals with a treatment value of “Treated” were exposed to two Spanish-speakers on their regular commute. “Control” individuals were not.</p>
<p>Run the following code in your console, either by typing it or by cutting-and-pasting it. It displays the contents of the <code>trains</code> data frame in your console. Note that depending on the size of your monitor, the output may vary slightly.</p>
<!-- ::: {.fullwidth} -->
<div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1"></a>trains</span></code></pre></div>
<pre><code>## # A tibble: 115 x 8
## gender liberal party age income att_start treatment att_end
## <chr> <lgl> <chr> <dbl> <dbl> <dbl> <fct> <dbl>
## 1 Female FALSE Democrat 31 135000 11 Treated 11
## 2 Female FALSE Republican 34 105000 9 Treated 10
## 3 Male TRUE Democrat 63 135000 3 Treated 5
## 4 Male FALSE Democrat 45 300000 11 Treated 11
## 5 Male TRUE Democrat 55 135000 8 Control 5
## 6 Female FALSE Democrat 37 87500 13 Treated 13
## 7 Female FALSE Republican 53 87500 13 Control 13
## 8 Male FALSE Democrat 36 135000 10 Treated 11
## 9 Female FALSE Democrat 54 105000 12 Control 12
## 10 Male FALSE Republican 42 135000 9 Treated 10
## # … with 105 more rows</code></pre>
<p>Let’s unpack this output:</p>
<ul>
<li><code>A tibble: 115 x 8</code>: A <code>tibble</code> is a specific kind of data frame in R. This particular data frame has
<ul>
<li><code>115</code> rows corresponding to different <em>observations</em>. Here, each observation is a person.</li>
<li><code>8</code> columns corresponding to 19 <em>variables</em> describing each observation.</li>
</ul></li>
<li><code>gender</code>, <code>liberal</code>, <code>party</code>, <code>age</code>, <code>income</code>, <code>att_start</code>, <code>treatment</code>, and <code>att_end</code> are the different variables of this dataset.</li>
<li><code>... with 105 more rows:</code> indicating to us that 105 more rows of data could not fit in this screen. R is only showing the first 10 rows, because if it showed all <code>115</code> rows, it would overwhelm your screen.</li>
</ul>
<p>Unfortunately, this output does not allow us to explore the data very well, but it does give a nice preview. Let’s look at some different ways to explore data frames.
<!-- ::: --></p>
<!-- ::: {.fullwidth} -->
</div>
<div id="exploring-data-frames" class="section level2">
<h2><span class="header-section-number">5.2</span> Exploring data frames</h2>
<p>There are many ways to get a feel for the data contained in a data frame such as <code>trains</code>. We present two functions that take as their “argument” (their input) the data frame in question. We also include a fourth method for exploring one particular column of a data frame:</p>
<ol style="list-style-type: decimal">
<li>Using the <code>View()</code> function, which brings up RStudio’s built-in data viewer.</li>
<li>Using the <code>glimpse()</code> function, which is included in the <strong>dplyr</strong> package.</li>
<li>Using the <code>$</code> “extraction operator,” which is used to view a single variable/column in a data frame.</li>
</ol>
<p><strong>1. <code>View()</code></strong>:</p>
<p>Run <code>View(trains)</code> in your console in RStudio, either by typing it or cutting-and-pasting it into the console pane. Explore this data frame in the resulting pop up viewer. You should get into the habit of viewing any data frames you encounter. Note the uppercase <code>V</code> in <code>View()</code>. R is case-sensitive, so you’ll get an error message if you run <code>view(trains)</code> instead of <code>View(trains)</code>.</p>
<p>By running <code>View(trains)</code>, we can explore the different <em>variables</em> listed in the columns. Observe that there are many different types of variables. Some of the variables including <code>age</code>, <code>income</code>, <code>att_start</code>, and <code>att_end</code> are what we will call <em>quantitative</em> variables. These variables are numerical in nature. Other variables here, including <code>gender</code>, <code>liberal</code>, <code>party</code>, and <code>treatment</code>, are <em>categorical</em>.</p>
<p>Note that if you look in the leftmost column of the <code>View(trains)</code> output, you will see a column of numbers. These are the row numbers of the dataset. If you glance across a row with the same number, say row 5, you can get an idea of what each row is representing. This will allow you to identify what object is being described in a given row by taking note of the values of the columns in that specific row. This is often called the <em>observational unit</em>. The observational unit in this example is an individual participating in the experiment on the Boston commuter train platform.</p>
<p>You can identify the observational unit by determining what “thing” is being measured or described by each of the variables. We’ll talk more about observational units in a later section on <em>identification</em> and <em>measurement</em> variables.</p>
<p><strong>2. <code>glimpse()</code></strong>:</p>
<p>The second way we’ll cover to explore a data frame is using the <code>glimpse()</code> function included in the <strong>dplyr</strong> package. Thus, you can only use the <code>glimpse()</code> function after you’ve loaded the <strong>dplyr</strong> package by running <code>library(dplyr)</code>. This function provides us with an alternative perspective for exploring a data frame than the <code>View()</code> function:</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1"></a><span class="kw">glimpse</span>(trains)</span></code></pre></div>
<pre><code>## Rows: 115
## Columns: 8
## $ gender <chr> "Female", "Female", "Male", "Male", "Male", "Female", "Fema…
## $ liberal <lgl> FALSE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE…
## $ party <chr> "Democrat", "Republican", "Democrat", "Democrat", "Democrat…
## $ age <dbl> 31, 34, 63, 45, 55, 37, 53, 36, 54, 42, 33, 50, 24, 40, 53,…
## $ income <dbl> 135000, 105000, 135000, 300000, 135000, 87500, 87500, 13500…
## $ att_start <dbl> 11, 9, 3, 11, 8, 13, 13, 10, 12, 9, 10, 11, 13, 6, 8, 13, 7…
## $ treatment <fct> Treated, Treated, Treated, Treated, Control, Treated, Contr…
## $ att_end <dbl> 11, 10, 5, 11, 5, 13, 13, 11, 12, 10, 9, 9, 13, 7, 8, 13, 8…</code></pre>
<p>Observe that <code>glimpse()</code> will give you the first few entries of each variable in a row after the variable name. In addition, the <em>data type</em> of the variable is given immediately after each variable’s name inside <code>< ></code>. Here, <code>dbl</code> refers to “double”, which is computer coding terminology for quantitative/numerical variables. While not a data type in <code>trains</code>, <code>int</code> refers to “integer” and is another data type that also represents quantitative/numerical variables. “Doubles” take up twice the size to store on a computer compared to integers.</p>
<p>In contrast, <code>chr</code> refers to “character”, which is computer terminology for text data. In most forms, text data, such as the <code>gender</code> or <code>party</code> of a person, are categorical variables. The <code>liberal</code> variable is another data type: <code>lgl</code>. These types of variables represent logical data (True/False). Finally, the <code>trains</code> dataset also includes the data type <code>fct</code>. <code>fct</code> refers to “factor” and describes a variable that is nominal, or in this case the <code>treatment</code> variable.</p>
<!-- Might be useful resources, commenting this out for future reference -->
<!-- These types of variables represent date and time combinations. However, we won't work with dates and times in this book; we leave this topic for other data science books like [*Introduction to Data Science* by Tiffany-Anne Timbers, Melissa Lee, and Trevor Campbell](https://ubc-dsci.github.io/introduction-to-datascience/) or [*R for Data Science*](https://r4ds.had.co.nz/dates-and-times.html) [@rds2016]. -->
<p><strong>3. <code>$</code> operator</strong></p>
<p>Lastly, the <code>$</code> operator allows us to extract and then explore a single variable within a data frame. For example, run the following in your console</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1"></a>trains<span class="op">$</span>age</span></code></pre></div>
<pre><code>## [1] 31 34 63 45 55 37 53 36 54 42 33 50 24 40 53 50 33 33 32 57 41 36 43 25 41
## [26] 33 44 46 41 28 36 37 38 48 20 52 38 45 55 38 45 44 36 29 42 43 54 39 31 50
## [51] 60 67 54 44 50 20 57 25 60 44 35 54 52 47 60 47 22 56 50 21 29 45 46 42 23
## [76] 29 60 41 30 61 21 46 53 45 46 63 21 31 35 22 68 27 22 30 59 56 32 35 23 60
## [101] 50 31 43 30 54 52 52 50 37 27 55 42 68 52 50</code></pre>
<p>We used the <code>$</code> operator to extract only the <code>age</code> variable and return it as a vector. We’ll only be occasionally exploring data frames using the <code>$</code> operator, instead favoring the <code>View()</code> and <code>glimpse()</code> functions.
<!-- ::: --></p>
<!-- ::: {.fullwidth} -->
</div>
<div id="identification-and-measurement-variables" class="section level2">
<h2><span class="header-section-number">5.3</span> Identification and measurement variables</h2>
<p>There is a subtle difference between the kinds of variables that you will encounter in data frames. There are <em>identification variables</em> and <em>measurement variables</em>. For example, let’s explore the <code>qscores</code> data frame by showing the output of <code>glimpse(qscores)</code>:</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1"></a><span class="kw">glimpse</span>(qscores)</span></code></pre></div>
<pre><code>## Rows: 748
## Columns: 8
## $ course_name <chr> " Introduction to Black Poetry", " American Democracy",…
## $ department <chr> "AFRAMER", "AFRAMER", "AFRAMER", "AFRAMER", "AFRAMER", …
## $ course_number <chr> "100Y", "123Z", "125X", "130X", "131Y", "199X", "199Y",…
## $ term <chr> "2019S", "2019S", "2019S", "2019S", "2019S", "2019S", "…
## $ enrollment <int> 49, 49, 40, 23, 20, 19, 40, 22, 18, 29, 35, 17, 17, 21,…
## $ workload <dbl> 2.6, 3.6, 5.2, 7.2, 3.5, 7.2, 4.2, 2.9, 1.5, 2.6, 2.6, …
## $ overall <dbl> 4.2, 4.4, 4.5, 4.4, 4.9, 4.8, 4.7, 4.9, 4.9, 4.0, 4.8, …
## $ prof_name <chr> "Jesse McCarthy", "Cornel West", "Elizabeth Kai Hinton"…</code></pre>
<p>The variables <code>course_name</code> and <code>course_number</code> are what we will call <em>identification variables</em>, variables that uniquely identify each observational unit. Sometimes a single variable might not be enough to uniquely identify each observational unit: combinations of variables might be needed. In this case, the identification variables uniquely identify Harvard courses. Such variables are mainly used in practice to uniquely identify each row in a data frame. <code>course_number</code> gives the unique course number provided by Harvard for that course, while the <code>course_name</code> variable gives the longer official name of the course. The remaining variables (<code>department</code>, <code>term</code>, <code>enrollment</code>, <code>workload</code>, <code>overall</code>, <code>prof_name</code>) are often called <em>measurement</em> or <em>characteristic</em> variables: variables that describe properties of each observational unit. For example, <code>overall</code> describes the numeric variable representing average of students’ rating of course (1 to 5 scale).
<!-- ::: --></p>
</div>
<!-- </div> -->
<div id="data-visualization-using-the-ggplot2-package" class="section level1">
<h1><span class="header-section-number">Chapter 6</span> Data Visualization using the ggplot2 Package</h1>
<p><label for="tufte-mn-5" class="margin-toggle">⊕</label><input type="checkbox" id="tufte-mn-5" class="margin-toggle"><span class="marginnote"><span style="display: block;"><strong>ggplot2</strong> is rooted in the data visualization theory known as <em>the grammar of graphics</em> developed by Leland Wilkinson. Similarly to a linguistic grammar, “the grammar of graphics” defines a set of rules for constructing <em>statistical graphics</em> by combining different types of <em>layers</em>. This grammar has been implemented in a variety of data visualization software platforms like R, but also <a href="https://plot.ly/">Plotly</a> and <a href="https://www.tableau.com/">Tableau</a>.</span></span></p>
<p>We begin the development of your data science toolbox with data visualization. By visualizing data, we gain valuable insights we couldn’t initially obtain from just looking at the raw data values. We’ll use the <strong>ggplot2</strong> package, as it provides an easy way to customize your plots.</p>
<p>At their most basic, graphics/plots/charts (we use these terms interchangeably in this book) provide a nice way to explore the patterns in data, such as the presence of <em>outliers</em>, <em>distributions</em> of individual variables, and <em>relationships</em> between groups of variables. Graphics are designed to emphasize the findings and insights you want your audience to understand. This does, however, require a balancing act. On the one hand, you want to highlight as many interesting findings as possible. On the other hand, you don’t want to include so much information that it overwhelms your audience.</p>
<p><label for="tufte-mn-6" class="margin-toggle">⊕</label><input type="checkbox" id="tufte-mn-6" class="margin-toggle"><span class="marginnote"><span style="display: block;">In short, the grammar tells us that:</span>
<span style="display: block;"><strong>A statistical graphic is a <code>mapping</code> of <code>data</code> variables to <code>aes</code>thetic attributes of <code>geom</code>etric objects.</strong></span></span></p>
<p>We can break a graphic into the following three essential components:</p>
<ol style="list-style-type: decimal">
<li><code>data</code>: the dataset containing the variables of interest.</li>
<li><code>geom</code>: the geometric object in question. This refers to the type of object we can observe in a plot. For example: points, lines, and bars.</li>
<li><code>aes</code>: aesthetic attributes of the geometric object. For example, x/y position, color, shape, and size. Aesthetic attributes are <em>mapped</em> to variables in the dataset.</li>
</ol>
<p>
<span class="marginnote shownote">
<!--
<div class="figure">--><span id="fig:unnamed-chunk-20"></span>
<img src="01-visualization/images/ggplot2_exploratory.png" alt="Artwork by @allison_horst" />
<!--
<p class="caption marginnote">-->FIGURE 6.1: Artwork by <span class="citation">@allison_horst</span><!--</p>-->
<!--</div>--></span>
</p>
<p>These three components are specified in the <code>ggplot()</code> function included in the <strong>ggplot2</strong> package. For the purposes of this book, we’ll always provide the <code>ggplot()</code> function with the following arguments (i.e., inputs) at a minimum:</p>
<ul>
<li>The data frame where the variables exist: the <code>data</code> argument.</li>
<li>The mapping of the variables to aesthetic attributes: the <code>mapping</code> argument which specifies the <code>aes</code>thetic attributes involved.</li>
</ul>
<p>After we’ve specified these components, we then add <em>layers</em> to the plot using the <code>+</code> sign. The most essential layer to add to a plot is the layer that specifies which type of <code>geom</code>etric object we want the plot to involve: points, lines, bars, and others. Other layers we can add to a plot include the plot title, axes labels, visual themes for the plots, and facets.</p>
</div>
<div id="gapminder-data" class="section level2">
<h2><span class="header-section-number">6.1</span> Gapminder data</h2>
<!-- ::: {.fullwidth} -->
<p>In February 2006, a Swedish physician and data advocate named Hans Rosling gave a TED talk titled <a href="https://www.ted.com/talks/hans_rosling_shows_the_best_stats_you_ve_ever_seen">“The best stats you’ve ever seen”</a> where he presented global economic, health, and development data from the website <a href="http://www.gapminder.org/tools/#_locale_id=en;&chart-type=bubbles">gapminder.org</a>. For example, for data on 142 countries in 2007, let’s consider only a few countries in the following table as a peak into the data.</p>
<pre><code>## # A tibble: 3 x 5
## Country Continent `Life Expectancy` Population `GDP per Capita`
## <fct> <fct> <dbl> <int> <dbl>
## 1 Afghanistan Asia 43.8 31889923 975.
## 2 Albania Europe 76.4 3600523 5937.
## 3 Algeria Africa 72.3 33333216 6223.</code></pre>
<p>Each row in this table corresponds to a country in 2007. For each row, we have 5 columns:</p>
<ol style="list-style-type: decimal">
<li><strong>Country</strong>: Name of country.</li>
<li><strong>Continent</strong>: Which of the five continents the country is part of. Note that “Americas” includes countries in both North and South America and that Antarctica is excluded.</li>
<li><strong>Life Expectancy</strong>: Life expectancy in years.</li>
<li><strong>Population</strong>: Number of people living in the country.</li>
<li><strong>GDP per Capita</strong>: Gross domestic product (in US dollars).</li>
</ol>
<p>Now consider the following scatterplot, which plots this for all 142 of the data’s countries.</p>
<!--
Note that R will deal with large numbers using scientific notation. So in the legend for "Population", 1.25e+09 is 1.25 $\times$ 10^9^ = 1,250,000,000 = 1.25 billion.
-->
<p><img src="01-visualization_files/figure-html/unnamed-chunk-23-1.png" width="672" style="display: block; margin: auto;" /></p>
<p>Let’s view this plot through the grammar of graphics:</p>
<ol style="list-style-type: decimal">
<li>The <code>data</code> variable <strong>GDP per Capita</strong> gets mapped to the <code>x</code>-position <code>aes</code>thetic of the points.</li>
<li>The <code>data</code> variable <strong>Life Expectancy</strong> gets mapped to the <code>y</code>-position <code>aes</code>thetic of the points.</li>
<li>The <code>data</code> variable <strong>Population</strong> gets mapped to the <code>size</code> <code>aes</code>thetic of the points.</li>
<li>The <code>data</code> variable <strong>Continent</strong> gets mapped to the <code>color</code> <code>aes</code>thetic of the points.</li>
</ol>
<p>We’ll see shortly that <code>data</code> corresponds to the particular data frame where our data is saved and that “data variables” correspond to particular columns in the data frame. Furthermore, the type of <code>geom</code>etric object considered in this plot are points. That being said, while in this example we are considering points, graphics are not limited to just points. We can also use lines, bars, and other geometric objects.</p>
<p>Let’s summarize the three essential components of a graphic:</p>
<pre><code>## # A tibble: 4 x 3
## `data variable` aes geom
## <chr> <chr> <chr>
## 1 GDP per Capita x point
## 2 Life Expectancy y point
## 3 Population size point
## 4 Continent color point</code></pre>
<!-- ::: -->
</div>
<!-- </div> -->
<div id="summary-of-geoms" class="section level1">
<h1><span class="header-section-number">Chapter 7</span> Summary of Geoms</h1>
<p>This table summarizes the differences among the <code>geom</code>s, or visual marks that represent data points, covered in this chapter. Using them, you’ll be able to visualize the distributions and relationships of variables contained in a wide array of datasets. This will be even more the case as we start to map more variables to more of each <code>geom</code>etric object’s <code>aes</code>thetic attribute options, further unlocking the awesome power of the <strong>ggplot2</strong> package.</p>
<div class="fullwidth">
<style>html {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Helvetica Neue', 'Fira Sans', 'Droid Sans', Arial, sans-serif;
}
#bfycptfpit .gt_table {
display: table;
border-collapse: collapse;
margin-left: auto;
margin-right: auto;
color: #333333;
font-size: 16px;
background-color: #FFFFFF;
width: auto;
border-top-style: solid;
border-top-width: 2px;
border-top-color: #A8A8A8;
border-right-style: none;
border-right-width: 2px;
border-right-color: #D3D3D3;
border-bottom-style: solid;
border-bottom-width: 2px;
border-bottom-color: #A8A8A8;
border-left-style: none;
border-left-width: 2px;
border-left-color: #D3D3D3;
}
#bfycptfpit .gt_heading {
background-color: #FFFFFF;
text-align: center;
border-bottom-color: #FFFFFF;
border-left-style: none;
border-left-width: 1px;
border-left-color: #D3D3D3;
border-right-style: none;
border-right-width: 1px;
border-right-color: #D3D3D3;
}
#bfycptfpit .gt_title {
color: #333333;
font-size: 125%;
font-weight: initial;
padding-top: 4px;
padding-bottom: 4px;
border-bottom-color: #FFFFFF;
border-bottom-width: 0;
}
#bfycptfpit .gt_subtitle {
color: #333333;
font-size: 85%;
font-weight: initial;
padding-top: 0;
padding-bottom: 4px;
border-top-color: #FFFFFF;
border-top-width: 0;
}
#bfycptfpit .gt_bottom_border {
border-bottom-style: solid;
border-bottom-width: 2px;
border-bottom-color: #D3D3D3;
}
#bfycptfpit .gt_col_headings {
border-top-style: solid;
border-top-width: 2px;
border-top-color: #D3D3D3;
border-bottom-style: solid;
border-bottom-width: 2px;
border-bottom-color: #D3D3D3;
border-left-style: none;
border-left-width: 1px;
border-left-color: #D3D3D3;
border-right-style: none;
border-right-width: 1px;
border-right-color: #D3D3D3;
}
#bfycptfpit .gt_col_heading {
color: #333333;
background-color: #FFFFFF;
font-size: 100%;
font-weight: normal;
text-transform: inherit;
border-left-style: none;
border-left-width: 1px;
border-left-color: #D3D3D3;
border-right-style: none;
border-right-width: 1px;
border-right-color: #D3D3D3;
vertical-align: bottom;
padding-top: 5px;
padding-bottom: 6px;
padding-left: 5px;
padding-right: 5px;
overflow-x: hidden;
}
#bfycptfpit .gt_column_spanner_outer {
color: #333333;
background-color: #FFFFFF;
font-size: 100%;
font-weight: normal;
text-transform: inherit;
padding-top: 0;
padding-bottom: 0;
padding-left: 4px;
padding-right: 4px;
}
#bfycptfpit .gt_column_spanner_outer:first-child {
padding-left: 0;
}
#bfycptfpit .gt_column_spanner_outer:last-child {
padding-right: 0;
}
#bfycptfpit .gt_column_spanner {
border-bottom-style: solid;
border-bottom-width: 2px;
border-bottom-color: #D3D3D3;
vertical-align: bottom;
padding-top: 5px;
padding-bottom: 6px;
overflow-x: hidden;
display: inline-block;
width: 100%;
}
#bfycptfpit .gt_group_heading {
padding: 8px;
color: #333333;
background-color: #FFFFFF;
font-size: 100%;
font-weight: initial;
text-transform: inherit;
border-top-style: solid;
border-top-width: 2px;
border-top-color: #D3D3D3;
border-bottom-style: solid;
border-bottom-width: 2px;
border-bottom-color: #D3D3D3;
border-left-style: none;
border-left-width: 1px;
border-left-color: #D3D3D3;
border-right-style: none;
border-right-width: 1px;
border-right-color: #D3D3D3;
vertical-align: middle;
}
#bfycptfpit .gt_empty_group_heading {
padding: 0.5px;
color: #333333;
background-color: #FFFFFF;
font-size: 100%;
font-weight: initial;
border-top-style: solid;
border-top-width: 2px;
border-top-color: #D3D3D3;
border-bottom-style: solid;
border-bottom-width: 2px;
border-bottom-color: #D3D3D3;
vertical-align: middle;
}
#bfycptfpit .gt_striped {
background-color: rgba(128, 128, 128, 0.05);
}
#bfycptfpit .gt_from_md > :first-child {
margin-top: 0;
}
#bfycptfpit .gt_from_md > :last-child {
margin-bottom: 0;
}
#bfycptfpit .gt_row {
padding-top: 8px;
padding-bottom: 8px;
padding-left: 5px;
padding-right: 5px;
margin: 10px;
border-top-style: solid;
border-top-width: 1px;
border-top-color: #D3D3D3;
border-left-style: none;
border-left-width: 1px;
border-left-color: #D3D3D3;
border-right-style: none;
border-right-width: 1px;
border-right-color: #D3D3D3;
vertical-align: middle;
overflow-x: hidden;
}
#bfycptfpit .gt_stub {
color: #333333;
background-color: #FFFFFF;
font-size: 100%;
font-weight: initial;
text-transform: inherit;
border-right-style: solid;
border-right-width: 2px;
border-right-color: #D3D3D3;
padding-left: 12px;
}
#bfycptfpit .gt_summary_row {
color: #333333;
background-color: #FFFFFF;
text-transform: inherit;
padding-top: 8px;
padding-bottom: 8px;
padding-left: 5px;
padding-right: 5px;
}
#bfycptfpit .gt_first_summary_row {
padding-top: 8px;
padding-bottom: 8px;
padding-left: 5px;
padding-right: 5px;
border-top-style: solid;
border-top-width: 2px;
border-top-color: #D3D3D3;
}
#bfycptfpit .gt_grand_summary_row {
color: #333333;
background-color: #FFFFFF;
text-transform: inherit;
padding-top: 8px;
padding-bottom: 8px;
padding-left: 5px;
padding-right: 5px;
}
#bfycptfpit .gt_first_grand_summary_row {
padding-top: 8px;
padding-bottom: 8px;
padding-left: 5px;
padding-right: 5px;
border-top-style: double;
border-top-width: 6px;
border-top-color: #D3D3D3;
}
#bfycptfpit .gt_table_body {
border-top-style: solid;
border-top-width: 2px;
border-top-color: #D3D3D3;
border-bottom-style: solid;
border-bottom-width: 2px;
border-bottom-color: #D3D3D3;
}
#bfycptfpit .gt_footnotes {
color: #333333;
background-color: #FFFFFF;
border-bottom-style: none;
border-bottom-width: 2px;
border-bottom-color: #D3D3D3;
border-left-style: none;
border-left-width: 2px;
border-left-color: #D3D3D3;
border-right-style: none;
border-right-width: 2px;
border-right-color: #D3D3D3;
}
#bfycptfpit .gt_footnote {
margin: 0px;
font-size: 90%;
padding: 4px;
}
#bfycptfpit .gt_sourcenotes {
color: #333333;
background-color: #FFFFFF;
border-bottom-style: none;
border-bottom-width: 2px;
border-bottom-color: #D3D3D3;
border-left-style: none;
border-left-width: 2px;
border-left-color: #D3D3D3;
border-right-style: none;
border-right-width: 2px;
border-right-color: #D3D3D3;
}
#bfycptfpit .gt_sourcenote {
font-size: 90%;
padding: 4px;
}
#bfycptfpit .gt_left {
text-align: left;
}
#bfycptfpit .gt_center {
text-align: center;
}
#bfycptfpit .gt_right {
text-align: right;
font-variant-numeric: tabular-nums;
}
#bfycptfpit .gt_font_normal {
font-weight: normal;
}
#bfycptfpit .gt_font_bold {
font-weight: bold;
}
#bfycptfpit .gt_font_italic {
font-style: italic;
}
#bfycptfpit .gt_super {
font-size: 65%;
}
#bfycptfpit .gt_footnote_marks {
font-style: italic;
font-size: 65%;
}
</style>
<div id="bfycptfpit" style="overflow-x:auto;overflow-y:auto;width:auto;height:auto;"><table class="gt_table">
<thead class="gt_header">
<tr>
<th colspan="5" class="gt_heading gt_title gt_font_normal" style>Summary of Five Named Graphs</th>
</tr>
<tr>
<th colspan="5" class="gt_heading gt_subtitle gt_font_normal gt_bottom_border" style></th>
</tr>
</thead>
<thead class="gt_col_headings">
<tr>
<th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1"> </th>
<th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1">Named graph</th>
<th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1">Shows</th>
<th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1">Geometric object</th>
<th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1">Notes</th>
</tr>
</thead>
<tbody class="gt_table_body">
<tr>
<td class="gt_row gt_right">1</td>
<td class="gt_row gt_left">Scatterplot</td>
<td class="gt_row gt_left">Relationship between 2 numerical variables</td>
<td class="gt_row gt_left">geom_point()</td>
<td class="gt_row gt_left">NA</td>
</tr>
<tr>
<td class="gt_row gt_right">2</td>
<td class="gt_row gt_left">Linegraph</td>
<td class="gt_row gt_left">Relationship between 2 numerical variables</td>
<td class="gt_row gt_left">geom_line()</td>
<td class="gt_row gt_left">Used when there is a sequential order to x-variable, e.g., time</td>
</tr>
<tr>
<td class="gt_row gt_right">3</td>
<td class="gt_row gt_left">Histogram</td>
<td class="gt_row gt_left">Distribution of 1 numerical variable</td>
<td class="gt_row gt_left">geom_histogram()</td>
<td class="gt_row gt_left">Facetted histograms show the distribution of 1 numerical variable split by the values of another variable</td>
</tr>
<tr>
<td class="gt_row gt_right">4</td>
<td class="gt_row gt_left">Boxplot</td>
<td class="gt_row gt_left">Distribution of 1 numerical variable split by the values of another variable</td>
<td class="gt_row gt_left">geom_boxplot()</td>
<td class="gt_row gt_left">NA</td>
</tr>
<tr>
<td class="gt_row gt_right">5</td>
<td class="gt_row gt_left">Barplot</td>
<td class="gt_row gt_left">Distribution of 1 categorical variable</td>
<td class="gt_row gt_left">geom_bar() when counts are not pre-counted, geom_col() when counts are pre-counted</td>
<td class="gt_row gt_left">Stacked, side-by-side, and faceted barplots show the joint distribution of 2 categorical variables</td>
</tr>
</tbody>
</table></div>
</div>
</div>
<div id="scatterplots" class="section level1">
<h1><span class="header-section-number">Chapter 8</span> Scatterplots</h1>
<!-- ::: {.fullwidth} -->
<p><em>Scatterplots</em>, also called <em>bivariate plots</em>, allow you to visualize the <em>relationship</em> between two numerical variables. Specifically, we will visualize the relationship between the following two numerical variables in the <code>flights</code> data frame included in the <code>nycflights13</code> package:</p>
<ol style="list-style-type: decimal">
<li><code>dep_delay</code>: departure delay on the horizontal “x” axis and</li>
<li><code>arr_delay</code>: arrival delay on the vertical “y” axis</li>
</ol>
<p>for Alaska Airlines flights leaving NYC in 2013. This requires paring down the data from all 336,776 flights that left NYC in 2013, to only the 714 <em>Alaska Airlines</em> flights that left NYC in 2013. We do this so our scatterplot will involve a manageable 714 points, and not an overwhelmingly large number like 336,776. To achieve this, we’ll take the <code>flights</code> data frame, filter the rows so that only the 714 rows corresponding to Alaska Airlines flights are kept, and save this in a new data frame called <code>alaska_flights</code> using the <code><-</code> <em>assignment</em> operator:</p>
<div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1"></a>alaska_flights <-<span class="st"> </span>flights <span class="op">%>%</span><span class="st"> </span></span>
<span id="cb15-2"><a href="#cb15-2"></a><span class="st"> </span><span class="kw">filter</span>(carrier <span class="op">==</span><span class="st"> "AS"</span>)</span></code></pre></div>
<p>For now, we suggest you don’t worry if you don’t fully understand this code. We’ll see later that this code uses the <strong>dplyr</strong> package for data wrangling to achieve our goal: it takes the <code>flights</code> data frame and <code>filter</code>s it to only return the rows where <code>carrier</code> is equal to <code>"AS"</code>, Alaska Airlines’ carrier code. Testing for equality is specified with <code>==</code> and not <code>=</code>. Convince yourself that this code achieves what it is supposed to by exploring the resulting data frame by running <code>View(alaska_flights)</code>. You’ll see that it has 714 rows, consisting of only 714 Alaska Airlines flights.
<!-- ::: --></p>
</div>
<div id="scatterplots-via-geom_point" class="section level2">
<h2><span class="header-section-number">8.1</span> Scatterplots via <code>geom_point</code></h2>
<!-- ::: {.fullwidth} -->
<p>Let’s now go over the code that will create the desired scatterplot and break it down piece-by-piece.</p>
<div class="sourceCode" id="cb16"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1"></a><span class="kw">ggplot</span>(<span class="dt">data =</span> alaska_flights, <span class="dt">mapping =</span> <span class="kw">aes</span>(<span class="dt">x =</span> dep_delay, <span class="dt">y =</span> arr_delay)) <span class="op">+</span><span class="st"> </span></span>
<span id="cb16-2"><a href="#cb16-2"></a><span class="st"> </span><span class="kw">geom_point</span>()</span></code></pre></div>
<p>Within the <code>ggplot()</code> function, we specify two of the plot’s components as arguments (i.e., inputs):</p>
<ol style="list-style-type: decimal">
<li>The <code>data</code> as the <code>alaska_flights</code> data frame via <code>data = alaska_flights</code>.</li>
<li>The <code>aes</code>thetic <code>mapping</code> by setting <code>mapping = aes(x = dep_delay, y = arr_delay)</code>. Specifically, the variable <code>dep_delay</code> maps to the <code>x</code> position aesthetic, while the variable <code>arr_delay</code> maps to the <code>y</code> position.</li>
</ol>
<p>We then add a layer to the <code>ggplot()</code> function call using the <code>+</code> sign. The added layer in question specifies the third component: the <code>geom</code>etric object. In this case, the geometric object is set to be points by specifying <code>geom_point()</code>. After running these two lines of code in your console, you’ll notice two outputs: a warning message and the following graphic shown.</p>
<pre><code>## Warning: Removed 5 rows containing missing values (geom_point).</code></pre>
<p><img src="01-visualization_files/figure-html/unnamed-chunk-28-1.png" width="672" style="display: block; margin: auto;" /></p>
<p>Let’s first unpack the graphic. Observe that a <em>positive relationship</em> exists between <code>dep_delay</code> and <code>arr_delay</code>: as departure delays increase, arrival delays tend to also increase. Observe also the large mass of points clustered near (0, 0), the point indicating flights that neither departed nor arrived late.</p>
<p>Let’s turn our attention to the warning message. R is alerting us to the fact that five rows were ignored due to them being missing. For these 5 rows, either the value for <code>dep_delay</code> or <code>arr_delay</code> or both were missing (recorded in R as <code>NA</code>), and thus these rows were ignored in our plot.</p>
<p>Before we continue, let’s make a few more observations about this code that created the scatterplot. Note that the <code>+</code> sign comes at the end of lines, and not at the beginning. You’ll get an error in R if you put it at the beginning of a line. When adding layers to a plot, you are encouraged to start a new line after the <code>+</code> (by pressing the Return/Enter button on your keyboard) so that the code for each layer is on a new line. As we add more and more layers to plots, you’ll see this will greatly improve the legibility of your code.</p>
<p>To stress the importance of adding the layer specifying the <code>geom</code>etric object, consider this figure where no layers are added. Because the <code>geom</code>etric object was not specified, we have a blank plot which is not very useful!</p>
<div class="sourceCode" id="cb18"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1"></a><span class="kw">ggplot</span>(<span class="dt">data =</span> alaska_flights, <span class="dt">mapping =</span> <span class="kw">aes</span>(<span class="dt">x =</span> dep_delay, <span class="dt">y =</span> arr_delay))</span></code></pre></div>
<p><img src="01-visualization_files/figure-html/unnamed-chunk-29-1.png" width="672" style="display: block; margin: auto;" />
<!-- ::: --></p>
</div>
<div id="overplotting" class="section level2">
<h2><span class="header-section-number">8.2</span> Overplotting</h2>
<!-- ::: {.fullwidth} -->
<p>The large mass of points near (0, 0) in the scatterplot we just plotted can cause some confusion since it is hard to tell the true number of points that are plotted. This is the result of a phenomenon called <em>overplotting</em>. As one may guess, this corresponds to points being plotted on top of each other over and over again. When overplotting occurs, it is difficult to know the number of points being plotted. There are two methods to address the issue of overplotting. Either by</p>
<ol style="list-style-type: decimal">
<li>Adjusting the transparency of the points or</li>
<li>Adding a little random “jitter”, or random “nudges”, to each of the points.</li>
</ol>
<p><strong>Method 1: Changing the transparency</strong></p>
<p>The first way of addressing overplotting is to change the transparency/opacity of the points by setting the <code>alpha</code> argument in <code>geom_point()</code>. We can change the <code>alpha</code> argument to be any value between <code>0</code> and <code>1</code>, where <code>0</code> sets the points to be 100% transparent and <code>1</code> sets the points to be 100% opaque. By default, <code>alpha</code> is set to <code>1</code>. In other words, if we don’t explicitly set an <code>alpha</code> value, R will use <code>alpha = 1</code>.</p>
<p>Note how the following code is identical to the code that created the scatterplot with overplotting, but with <code>alpha = 0.2</code> added to the <code>geom_point()</code> function:</p>
<div class="sourceCode" id="cb19"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1"></a><span class="kw">ggplot</span>(<span class="dt">data =</span> alaska_flights, <span class="dt">mapping =</span> <span class="kw">aes</span>(<span class="dt">x =</span> dep_delay, <span class="dt">y =</span> arr_delay)) <span class="op">+</span><span class="st"> </span></span>
<span id="cb19-2"><a href="#cb19-2"></a><span class="st"> </span><span class="kw">geom_point</span>(<span class="dt">alpha =</span> <span class="fl">0.2</span>)</span></code></pre></div>
<p><img src="01-visualization_files/figure-html/unnamed-chunk-30-1.png" width="672" style="display: block; margin: auto;" /></p>
<p>The key feature to note in this plot is that the transparency of the points is cumulative: areas with a high-degree of overplotting are darker, whereas areas with a lower degree are less dark. Note furthermore that there is no <code>aes()</code> surrounding <code>alpha = 0.2</code>. This is because we are not mapping a variable to an aesthetic attribute, but rather merely changing the default setting of <code>alpha</code>. In fact, you’ll receive an error if you try to change the second line to read <code>geom_point(aes(alpha = 0.2))</code>.</p>
<p><strong>Method 2: Jittering the points</strong></p>
<p>The second way of addressing overplotting is by <em>jittering</em> all the points. This means giving each point a small “nudge” in a random direction. You can think of “jittering” as shaking the points around a bit on the plot. Let’s illustrate using a simple example first. Say we have a data frame with 4 identical rows of x and y values: (0,0), (0,0), (0,0), and (0,0). We present both the regular scatterplot of these 4 points (on the left) and its jittered counterpart (on the right).</p>
<p><img src="01-visualization_files/figure-html/unnamed-chunk-31-1.png" width="672" style="display: block; margin: auto;" /></p>
<p>In the left-hand regular scatterplot, observe that the 4 points are superimposed on top of each other. While we know there are 4 values being plotted, this fact might not be apparent to others. In the right-hand jittered scatterplot, it is now plainly evident that this plot involves four points since each point is given a random “nudge.”</p>
<p>Keep in mind, however, that jittering is strictly a visualization tool; even after creating a jittered scatterplot, the original values saved in the data frame remain unchanged.</p>
<p>To create a jittered scatterplot, instead of using <code>geom_point()</code>, we use <code>geom_jitter()</code>. Observe how the following code is very similar to the code that created the scatterplot with overplotting, but with <code>geom_point()</code> replaced with <code>geom_jitter()</code>.</p>
<div class="sourceCode" id="cb20"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1"></a><span class="kw">ggplot</span>(<span class="dt">data =</span> alaska_flights, <span class="dt">mapping =</span> <span class="kw">aes</span>(<span class="dt">x =</span> dep_delay, <span class="dt">y =</span> arr_delay)) <span class="op">+</span><span class="st"> </span></span>
<span id="cb20-2"><a href="#cb20-2"></a><span class="st"> </span><span class="kw">geom_jitter</span>(<span class="dt">width =</span> <span class="dv">30</span>, <span class="dt">height =</span> <span class="dv">30</span>)</span></code></pre></div>
<p><img src="01-visualization_files/figure-html/unnamed-chunk-32-1.png" width="672" style="display: block; margin: auto;" /></p>
<p>In order to specify how much jitter to add, we adjusted the <code>width</code> and <code>height</code> arguments to <code>geom_jitter()</code>. This corresponds to how hard you’d like to shake the plot in horizontal x-axis units and vertical y-axis units, respectively. In this case, both axes are in minutes. How much jitter should we add using the <code>width</code> and <code>height</code> arguments? On the one hand, it is important to add just enough jitter to break any overlap in points, but on the other hand, not so much that we completely alter the original pattern in points.</p>
<p>As can be seen in the resulting plot, in this case jittering doesn’t really provide much new insight. In this particular case, it can be argued that changing the transparency of the points by setting <code>alpha</code> proved more effective. When would it be better to use a jittered scatterplot? When would it be better to alter the points’ transparency? There is no single right answer that applies to all situations. You need to make a subjective choice and own that choice. At the very least when confronted with overplotting, however, we suggest you make both types of plots and see which one better emphasizes the point you are trying to make.
<!-- ::: --></p>
</div>
<!-- </div> -->
<div id="linegraphs" class="section level1">
<h1><span class="header-section-number">Chapter 9</span> Linegraphs</h1>
<!-- ::: {.fullwidth} -->
<p>Linegraphs show the relationship between two numerical variables when the variable on the x-axis, also called the <em>explanatory</em> variable, is of a sequential nature. In other words, there is an inherent ordering to the variable.</p>
<p>The most common examples of linegraphs have some notion of time on the x-axis: hours, days, weeks, years, etc. Since time is sequential, we connect consecutive observations of the variable on the y-axis with a line. Linegraphs that have some notion of time on the x-axis are also called <em>time series</em> plots. Let’s illustrate linegraphs using another dataset in the <code>nycflights13</code> package: the <code>weather</code> data frame.</p>
<p>Let’s explore the <code>weather</code> data frame by running <code>View(weather)</code> and <code>glimpse(weather)</code>. Furthermore let’s read the associated help file by running <code>?weather</code> to bring up the help file.</p>
<p>Observe that there is a variable called <code>temp</code> of hourly temperature recordings in Fahrenheit at weather stations near all three major airports in New York City: Newark (<code>origin</code> code <code>EWR</code>), John F. Kennedy International (<code>JFK</code>), and LaGuardia (<code>LGA</code>). However, instead of considering hourly temperatures for all days in 2013 for all three airports, for simplicity let’s only consider hourly temperatures at Newark airport for the first 15 days in January.</p>
<p>Recall in section on scatterplots, we used the <code>filter()</code> function to only choose the subset of rows of <code>flights</code> corresponding to Alaska Airlines flights. We similarly use <code>filter()</code> here, but by using the <code>&</code> operator we only choose the subset of rows of <code>weather</code> where the <code>origin</code> is <code>"EWR"</code>, the <code>month</code> is January, <strong>and</strong> the <code>day</code> is between <code>1</code> and <code>15</code>. Recall we performed a similar task in section on scatterplots when creating the <code>alaska_flights</code> data frame of only Alaska Airlines flights, a topic we’ll explore more in the next chapter on data wrangling.</p>
<div class="sourceCode" id="cb21"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb21-1"><a href="#cb21-1"></a>early_january_weather <-<span class="st"> </span>weather <span class="op">%>%</span><span class="st"> </span></span>
<span id="cb21-2"><a href="#cb21-2"></a><span class="st"> </span><span class="kw">filter</span>(origin <span class="op">==</span><span class="st"> "EWR"</span> <span class="op">&</span><span class="st"> </span>month <span class="op">==</span><span class="st"> </span><span class="dv">1</span> <span class="op">&</span><span class="st"> </span>day <span class="op"><=</span><span class="st"> </span><span class="dv">15</span>)</span></code></pre></div>
<!-- ::: -->
</div>
<div id="linegraphs-via-geom_line" class="section level2">
<h2><span class="header-section-number">9.1</span> Linegraphs via <code>geom_line</code></h2>
<!-- ::: {.fullwidth} -->
<p>Let’s create a time series plot of the hourly temperatures saved in the <code>early_january_weather</code> data frame by using <code>geom_line()</code> to create a linegraph, instead of using <code>geom_point()</code> like we used previously to create scatterplots:</p>
<div class="sourceCode" id="cb22"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb22-1"><a href="#cb22-1"></a><span class="kw">ggplot</span>(<span class="dt">data =</span> early_january_weather, </span>
<span id="cb22-2"><a href="#cb22-2"></a> <span class="dt">mapping =</span> <span class="kw">aes</span>(<span class="dt">x =</span> time_hour, <span class="dt">y =</span> temp)) <span class="op">+</span></span>