-
Notifications
You must be signed in to change notification settings - Fork 229
/
exploratory-data-analysis.html
818 lines (770 loc) · 80 KB
/
exploratory-data-analysis.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
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Exercise Solutions and Notes for R for Data Science</title>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<meta name="description" content="Exercise Solutions and Notes for “R for Data Science”">
<meta name="generator" content="bookdown 0.3.6 and GitBook 2.6.7">
<meta property="og:title" content="Exercise Solutions and Notes for R for Data Science" />
<meta property="og:type" content="book" />
<meta name="github-repo" content="jrnold/e4qf" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="Exercise Solutions and Notes for R for Data Science" />
<meta name="author" content="Jeffrey B. Arnold">
<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="data-transformation.html">
<link rel="next" href="tibbles.html">
<script src="libs/jquery-2.2.3/jquery.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.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" />
<script src="libs/htmlwidgets-0.8/htmlwidgets.js"></script>
<link href="libs/str_view-0.1.0/str_view.css" rel="stylesheet" />
<script src="libs/str_view-binding-1.1.0/str_view.js"></script>
<style type="text/css">
div.sourceCode { overflow-x: auto; }
table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
margin: 0; padding: 0; vertical-align: baseline; border: none; }
table.sourceCode { width: 100%; line-height: 100%; }
td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
td.sourceCode { padding-left: 5px; }
code > span.kw { color: #007020; font-weight: bold; } /* Keyword */
code > span.dt { color: #902000; } /* DataType */
code > span.dv { color: #40a070; } /* DecVal */
code > span.bn { color: #40a070; } /* BaseN */
code > span.fl { color: #40a070; } /* Float */
code > span.ch { color: #4070a0; } /* Char */
code > span.st { color: #4070a0; } /* String */
code > span.co { color: #60a0b0; font-style: italic; } /* Comment */
code > span.ot { color: #007020; } /* Other */
code > span.al { color: #ff0000; font-weight: bold; } /* Alert */
code > span.fu { color: #06287e; } /* Function */
code > span.er { color: #ff0000; font-weight: bold; } /* Error */
code > span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
code > span.cn { color: #880000; } /* Constant */
code > span.sc { color: #4070a0; } /* SpecialChar */
code > span.vs { color: #4070a0; } /* VerbatimString */
code > span.ss { color: #bb6688; } /* SpecialString */
code > span.im { } /* Import */
code > span.va { color: #19177c; } /* Variable */
code > span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code > span.op { color: #666666; } /* Operator */
code > span.bu { } /* BuiltIn */
code > span.ex { } /* Extension */
code > span.pp { color: #bc7a00; } /* Preprocessor */
code > span.at { color: #7d9029; } /* Attribute */
code > span.do { color: #ba2121; font-style: italic; } /* Documentation */
code > span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code > span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code > span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
</style>
<link rel="stylesheet" href="r4ds.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><strong><a href="./">R for Data Science</a></strong></li>
<li class="divider"></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i>Welcome</a></li>
<li class="part"><span><b>I Explore</b></span></li>
<li class="chapter" data-level="1" data-path="explore-intro.html"><a href="explore-intro.html"><i class="fa fa-check"></i><b>1</b> Introduction</a></li>
<li class="chapter" data-level="2" data-path="visualize.html"><a href="visualize.html"><i class="fa fa-check"></i><b>2</b> Visualize</a><ul>
<li class="chapter" data-level="2.1" data-path="visualize.html"><a href="visualize.html#introduction"><i class="fa fa-check"></i><b>2.1</b> Introduction</a><ul>
<li class="chapter" data-level="2.1.1" data-path="visualize.html"><a href="visualize.html#prerequisites"><i class="fa fa-check"></i><b>2.1.1</b> Prerequisites</a></li>
<li class="chapter" data-level="2.1.2" data-path="visualize.html"><a href="visualize.html#first-steps"><i class="fa fa-check"></i><b>2.1.2</b> First Steps</a></li>
<li class="chapter" data-level="2.1.3" data-path="visualize.html"><a href="visualize.html#aesthetic-mappings"><i class="fa fa-check"></i><b>2.1.3</b> Aesthetic mappings</a></li>
<li class="chapter" data-level="2.1.4" data-path="visualize.html"><a href="visualize.html#facets"><i class="fa fa-check"></i><b>2.1.4</b> Facets</a></li>
<li class="chapter" data-level="2.1.5" data-path="visualize.html"><a href="visualize.html#geometric-objects"><i class="fa fa-check"></i><b>2.1.5</b> Geometric Objects</a></li>
<li class="chapter" data-level="2.1.6" data-path="visualize.html"><a href="visualize.html#statistical-transformations"><i class="fa fa-check"></i><b>2.1.6</b> Statistical Transformations</a></li>
</ul></li>
<li class="chapter" data-level="2.2" data-path="visualize.html"><a href="visualize.html#position-adjustments"><i class="fa fa-check"></i><b>2.2</b> Position Adjustments</a></li>
<li class="chapter" data-level="2.3" data-path="visualize.html"><a href="visualize.html#coordinate-systems"><i class="fa fa-check"></i><b>2.3</b> Coordinate Systems</a><ul>
<li class="chapter" data-level="2.3.1" data-path="visualize.html"><a href="visualize.html#exercises-3"><i class="fa fa-check"></i><b>2.3.1</b> Exercises</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="3" data-path="workflow-basics.html"><a href="workflow-basics.html"><i class="fa fa-check"></i><b>3</b> Workflow Basics</a><ul>
<li class="chapter" data-level="3.1" data-path="workflow-basics.html"><a href="workflow-basics.html#practice"><i class="fa fa-check"></i><b>3.1</b> Practice</a><ul>
<li class="chapter" data-level="3.1.1" data-path="workflow-basics.html"><a href="workflow-basics.html#exercises-4"><i class="fa fa-check"></i><b>3.1.1</b> Exercises</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="4" data-path="data-transformation.html"><a href="data-transformation.html"><i class="fa fa-check"></i><b>4</b> Data Transformation</a><ul>
<li class="chapter" data-level="4.1" data-path="data-transformation.html"><a href="data-transformation.html#prerequisites-1"><i class="fa fa-check"></i><b>4.1</b> Prerequisites</a></li>
<li class="chapter" data-level="4.2" data-path="data-transformation.html"><a href="data-transformation.html#filter"><i class="fa fa-check"></i><b>4.2</b> Filter</a></li>
<li class="chapter" data-level="4.3" data-path="data-transformation.html"><a href="data-transformation.html#exercises-5"><i class="fa fa-check"></i><b>4.3</b> Exercises</a></li>
<li class="chapter" data-level="4.4" data-path="data-transformation.html"><a href="data-transformation.html#arrange"><i class="fa fa-check"></i><b>4.4</b> Arrange</a><ul>
<li class="chapter" data-level="4.4.1" data-path="data-transformation.html"><a href="data-transformation.html#exercises-6"><i class="fa fa-check"></i><b>4.4.1</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="4.5" data-path="data-transformation.html"><a href="data-transformation.html#mutate"><i class="fa fa-check"></i><b>4.5</b> Mutate</a><ul>
<li class="chapter" data-level="4.5.1" data-path="data-transformation.html"><a href="data-transformation.html#exercises-7"><i class="fa fa-check"></i><b>4.5.1</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="4.6" data-path="data-transformation.html"><a href="data-transformation.html#grouped-summaries-with-summarise"><i class="fa fa-check"></i><b>4.6</b> Grouped summaries with <code>summarise()</code></a><ul>
<li class="chapter" data-level="4.6.1" data-path="data-transformation.html"><a href="data-transformation.html#exercises-8"><i class="fa fa-check"></i><b>4.6.1</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="4.7" data-path="data-transformation.html"><a href="data-transformation.html#grouped-mutates-and-filters"><i class="fa fa-check"></i><b>4.7</b> Grouped mutates and filters</a><ul>
<li class="chapter" data-level="4.7.1" data-path="data-transformation.html"><a href="data-transformation.html#exercises-9"><i class="fa fa-check"></i><b>4.7.1</b> Exercises</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="5" data-path="exploratory-data-analysis.html"><a href="exploratory-data-analysis.html"><i class="fa fa-check"></i><b>5</b> Exploratory Data Analysis</a><ul>
<li class="chapter" data-level="5.1" data-path="exploratory-data-analysis.html"><a href="exploratory-data-analysis.html#introduction-1"><i class="fa fa-check"></i><b>5.1</b> Introduction</a><ul>
<li class="chapter" data-level="5.1.1" data-path="exploratory-data-analysis.html"><a href="exploratory-data-analysis.html#questions"><i class="fa fa-check"></i><b>5.1.1</b> Questions</a></li>
<li class="chapter" data-level="5.1.2" data-path="exploratory-data-analysis.html"><a href="exploratory-data-analysis.html#variation"><i class="fa fa-check"></i><b>5.1.2</b> Variation</a></li>
</ul></li>
<li class="chapter" data-level="5.2" data-path="exploratory-data-analysis.html"><a href="exploratory-data-analysis.html#missing-values"><i class="fa fa-check"></i><b>5.2</b> Missing Values</a><ul>
<li class="chapter" data-level="5.2.1" data-path="exploratory-data-analysis.html"><a href="exploratory-data-analysis.html#exercises-11"><i class="fa fa-check"></i><b>5.2.1</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="5.3" data-path="exploratory-data-analysis.html"><a href="exploratory-data-analysis.html#covariation"><i class="fa fa-check"></i><b>5.3</b> Covariation</a><ul>
<li class="chapter" data-level="5.3.1" data-path="exploratory-data-analysis.html"><a href="exploratory-data-analysis.html#a-categorical-and-continuous-variable"><i class="fa fa-check"></i><b>5.3.1</b> A categorical and continuous variable</a></li>
<li class="chapter" data-level="5.3.2" data-path="exploratory-data-analysis.html"><a href="exploratory-data-analysis.html#two-categorical-variables"><i class="fa fa-check"></i><b>5.3.2</b> Two categorical variables</a></li>
<li class="chapter" data-level="5.3.3" data-path="exploratory-data-analysis.html"><a href="exploratory-data-analysis.html#two-continuous-variables"><i class="fa fa-check"></i><b>5.3.3</b> Two continuous variables</a></li>
</ul></li>
</ul></li>
<li class="part"><span><b>II Wrangle</b></span></li>
<li class="chapter" data-level="6" data-path="tibbles.html"><a href="tibbles.html"><i class="fa fa-check"></i><b>6</b> Tibbles</a><ul>
<li class="chapter" data-level="6.1" data-path="tibbles.html"><a href="tibbles.html#prerquisites"><i class="fa fa-check"></i><b>6.1</b> Prerquisites</a></li>
<li class="chapter" data-level="6.2" data-path="tibbles.html"><a href="tibbles.html#creating-tibbles"><i class="fa fa-check"></i><b>6.2</b> Creating Tibbles</a></li>
<li class="chapter" data-level="6.3" data-path="tibbles.html"><a href="tibbles.html#tibbles-vs.data.frame"><i class="fa fa-check"></i><b>6.3</b> Tibbles vs. data.frame</a></li>
<li class="chapter" data-level="6.4" data-path="tibbles.html"><a href="tibbles.html#subsetting"><i class="fa fa-check"></i><b>6.4</b> Subsetting</a></li>
<li class="chapter" data-level="6.5" data-path="tibbles.html"><a href="tibbles.html#interacting-with-older-code"><i class="fa fa-check"></i><b>6.5</b> Interacting with older code</a></li>
<li class="chapter" data-level="6.6" data-path="tibbles.html"><a href="tibbles.html#exercises-12"><i class="fa fa-check"></i><b>6.6</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="7" data-path="data-import.html"><a href="data-import.html"><i class="fa fa-check"></i><b>7</b> Data Import</a><ul>
<li class="chapter" data-level="7.1" data-path="data-import.html"><a href="data-import.html#introduction-2"><i class="fa fa-check"></i><b>7.1</b> Introduction</a></li>
<li class="chapter" data-level="7.2" data-path="data-import.html"><a href="data-import.html#getting-started"><i class="fa fa-check"></i><b>7.2</b> Getting started</a><ul>
<li class="chapter" data-level="7.2.1" data-path="data-import.html"><a href="data-import.html#exercises-13"><i class="fa fa-check"></i><b>7.2.1</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="7.3" data-path="data-import.html"><a href="data-import.html#parsing-a-vector"><i class="fa fa-check"></i><b>7.3</b> Parsing a vector</a><ul>
<li class="chapter" data-level="7.3.1" data-path="data-import.html"><a href="data-import.html#exercises-14"><i class="fa fa-check"></i><b>7.3.1</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="7.4" data-path="data-import.html"><a href="data-import.html#other-types-of-data"><i class="fa fa-check"></i><b>7.4</b> Other Types of Data</a></li>
</ul></li>
<li class="chapter" data-level="8" data-path="tidy-data.html"><a href="tidy-data.html"><i class="fa fa-check"></i><b>8</b> Tidy Data</a><ul>
<li class="chapter" data-level="8.1" data-path="tidy-data.html"><a href="tidy-data.html#introduction-3"><i class="fa fa-check"></i><b>8.1</b> Introduction</a></li>
<li class="chapter" data-level="8.2" data-path="tidy-data.html"><a href="tidy-data.html#tidy-data-1"><i class="fa fa-check"></i><b>8.2</b> Tidy Data</a><ul>
<li class="chapter" data-level="8.2.1" data-path="tidy-data.html"><a href="tidy-data.html#exercises-15"><i class="fa fa-check"></i><b>8.2.1</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="8.3" data-path="tidy-data.html"><a href="tidy-data.html#spreading-and-gathering"><i class="fa fa-check"></i><b>8.3</b> Spreading and Gathering</a><ul>
<li class="chapter" data-level="8.3.1" data-path="tidy-data.html"><a href="tidy-data.html#exercises-16"><i class="fa fa-check"></i><b>8.3.1</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="8.4" data-path="tidy-data.html"><a href="tidy-data.html#separating-and-uniting"><i class="fa fa-check"></i><b>8.4</b> Separating and Uniting</a><ul>
<li class="chapter" data-level="8.4.1" data-path="tidy-data.html"><a href="tidy-data.html#exercises-17"><i class="fa fa-check"></i><b>8.4.1</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="8.5" data-path="tidy-data.html"><a href="tidy-data.html#missing-values-1"><i class="fa fa-check"></i><b>8.5</b> Missing Values</a><ul>
<li class="chapter" data-level="8.5.1" data-path="tidy-data.html"><a href="tidy-data.html#exercises-18"><i class="fa fa-check"></i><b>8.5.1</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="8.6" data-path="tidy-data.html"><a href="tidy-data.html#case-study"><i class="fa fa-check"></i><b>8.6</b> Case Study</a><ul>
<li class="chapter" data-level="8.6.1" data-path="tidy-data.html"><a href="tidy-data.html#exercises-19"><i class="fa fa-check"></i><b>8.6.1</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="8.7" data-path="tidy-data.html"><a href="tidy-data.html#non-tidy-data"><i class="fa fa-check"></i><b>8.7</b> Non-Tidy Data</a></li>
</ul></li>
<li class="chapter" data-level="9" data-path="relational-data.html"><a href="relational-data.html"><i class="fa fa-check"></i><b>9</b> Relational Data</a><ul>
<li class="chapter" data-level="9.1" data-path="relational-data.html"><a href="relational-data.html#prerequisites-2"><i class="fa fa-check"></i><b>9.1</b> Prerequisites</a></li>
<li class="chapter" data-level="9.2" data-path="relational-data.html"><a href="relational-data.html#nycflights13"><i class="fa fa-check"></i><b>9.2</b> nycflights13</a><ul>
<li class="chapter" data-level="9.2.1" data-path="relational-data.html"><a href="relational-data.html#exercises-20"><i class="fa fa-check"></i><b>9.2.1</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="9.3" data-path="relational-data.html"><a href="relational-data.html#keys"><i class="fa fa-check"></i><b>9.3</b> Keys</a></li>
<li class="chapter" data-level="9.4" data-path="relational-data.html"><a href="relational-data.html#mutating-joins"><i class="fa fa-check"></i><b>9.4</b> Mutating Joins</a><ul>
<li class="chapter" data-level="9.4.1" data-path="relational-data.html"><a href="relational-data.html#exercises-21"><i class="fa fa-check"></i><b>9.4.1</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="9.5" data-path="relational-data.html"><a href="relational-data.html#filtering-joins"><i class="fa fa-check"></i><b>9.5</b> Filtering Joins</a><ul>
<li class="chapter" data-level="9.5.1" data-path="relational-data.html"><a href="relational-data.html#exercises-22"><i class="fa fa-check"></i><b>9.5.1</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="9.6" data-path="relational-data.html"><a href="relational-data.html#set-operations"><i class="fa fa-check"></i><b>9.6</b> Set operations</a></li>
</ul></li>
<li class="chapter" data-level="10" data-path="strings.html"><a href="strings.html"><i class="fa fa-check"></i><b>10</b> Strings</a><ul>
<li class="chapter" data-level="10.1" data-path="strings.html"><a href="strings.html#introduction-4"><i class="fa fa-check"></i><b>10.1</b> Introduction</a></li>
<li class="chapter" data-level="10.2" data-path="strings.html"><a href="strings.html#string-basics"><i class="fa fa-check"></i><b>10.2</b> String Basics</a><ul>
<li class="chapter" data-level="10.2.1" data-path="strings.html"><a href="strings.html#exercises-23"><i class="fa fa-check"></i><b>10.2.1</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="10.3" data-path="strings.html"><a href="strings.html#matching-patterns-and-regular-expressions"><i class="fa fa-check"></i><b>10.3</b> Matching Patterns and Regular Expressions</a><ul>
<li class="chapter" data-level="10.3.1" data-path="strings.html"><a href="strings.html#exercises-24"><i class="fa fa-check"></i><b>10.3.1</b> Exercises</a></li>
<li class="chapter" data-level="10.3.2" data-path="strings.html"><a href="strings.html#repitition"><i class="fa fa-check"></i><b>10.3.2</b> Repitition</a></li>
<li class="chapter" data-level="10.3.3" data-path="strings.html"><a href="strings.html#grouping-and-backreferences"><i class="fa fa-check"></i><b>10.3.3</b> Grouping and backreferences</a></li>
</ul></li>
<li class="chapter" data-level="10.4" data-path="strings.html"><a href="strings.html#tools"><i class="fa fa-check"></i><b>10.4</b> Tools</a><ul>
<li class="chapter" data-level="10.4.1" data-path="strings.html"><a href="strings.html#detect-matches"><i class="fa fa-check"></i><b>10.4.1</b> Detect matches</a></li>
<li class="chapter" data-level="10.4.2" data-path="strings.html"><a href="strings.html#exercises-29"><i class="fa fa-check"></i><b>10.4.2</b> Exercises</a></li>
<li class="chapter" data-level="10.4.3" data-path="strings.html"><a href="strings.html#extract-matches"><i class="fa fa-check"></i><b>10.4.3</b> Extract Matches</a></li>
<li class="chapter" data-level="10.4.4" data-path="strings.html"><a href="strings.html#grouped-matches"><i class="fa fa-check"></i><b>10.4.4</b> Grouped Matches</a></li>
<li class="chapter" data-level="10.4.5" data-path="strings.html"><a href="strings.html#splitting"><i class="fa fa-check"></i><b>10.4.5</b> Splitting</a></li>
</ul></li>
<li class="chapter" data-level="10.5" data-path="strings.html"><a href="strings.html#other-types-of-patterns"><i class="fa fa-check"></i><b>10.5</b> Other types of patterns</a><ul>
<li class="chapter" data-level="10.5.1" data-path="strings.html"><a href="strings.html#exercises-33"><i class="fa fa-check"></i><b>10.5.1</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="10.6" data-path="strings.html"><a href="strings.html#stringi"><i class="fa fa-check"></i><b>10.6</b> stringi</a><ul>
<li class="chapter" data-level="10.6.1" data-path="strings.html"><a href="strings.html#exercises-34"><i class="fa fa-check"></i><b>10.6.1</b> Exercises</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="11" data-path="factors.html"><a href="factors.html"><i class="fa fa-check"></i><b>11</b> Factors</a><ul>
<li class="chapter" data-level="11.1" data-path="factors.html"><a href="factors.html#introduction-5"><i class="fa fa-check"></i><b>11.1</b> Introduction</a></li>
<li class="chapter" data-level="11.2" data-path="factors.html"><a href="factors.html#creating-factors"><i class="fa fa-check"></i><b>11.2</b> Creating Factors</a></li>
<li class="chapter" data-level="11.3" data-path="factors.html"><a href="factors.html#general-social-survey"><i class="fa fa-check"></i><b>11.3</b> General Social Survey</a><ul>
<li class="chapter" data-level="11.3.1" data-path="factors.html"><a href="factors.html#exercises-35"><i class="fa fa-check"></i><b>11.3.1</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="11.4" data-path="factors.html"><a href="factors.html#modifying-factor-order"><i class="fa fa-check"></i><b>11.4</b> Modifying factor order</a><ul>
<li class="chapter" data-level="11.4.1" data-path="factors.html"><a href="factors.html#exercises-36"><i class="fa fa-check"></i><b>11.4.1</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="11.5" data-path="factors.html"><a href="factors.html#modifying-factor-levels"><i class="fa fa-check"></i><b>11.5</b> Modifying factor levels</a><ul>
<li class="chapter" data-level="11.5.1" data-path="factors.html"><a href="factors.html#exercises-37"><i class="fa fa-check"></i><b>11.5.1</b> Exercises</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="12" data-path="dates-and-times.html"><a href="dates-and-times.html"><i class="fa fa-check"></i><b>12</b> Dates and Times</a><ul>
<li class="chapter" data-level="12.1" data-path="dates-and-times.html"><a href="dates-and-times.html#prerequisite"><i class="fa fa-check"></i><b>12.1</b> Prerequisite</a></li>
<li class="chapter" data-level="12.2" data-path="dates-and-times.html"><a href="dates-and-times.html#creating-datetimes"><i class="fa fa-check"></i><b>12.2</b> Creating date/times</a><ul>
<li class="chapter" data-level="12.2.1" data-path="dates-and-times.html"><a href="dates-and-times.html#exercises-38"><i class="fa fa-check"></i><b>12.2.1</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="12.3" data-path="dates-and-times.html"><a href="dates-and-times.html#date-time-components"><i class="fa fa-check"></i><b>12.3</b> Date-Time Components</a><ul>
<li class="chapter" data-level="12.3.1" data-path="dates-and-times.html"><a href="dates-and-times.html#exercises-39"><i class="fa fa-check"></i><b>12.3.1</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="12.4" data-path="dates-and-times.html"><a href="dates-and-times.html#time-spans"><i class="fa fa-check"></i><b>12.4</b> Time Spans</a><ul>
<li class="chapter" data-level="12.4.1" data-path="dates-and-times.html"><a href="dates-and-times.html#durations"><i class="fa fa-check"></i><b>12.4.1</b> Durations</a></li>
<li class="chapter" data-level="12.4.2" data-path="dates-and-times.html"><a href="dates-and-times.html#periods"><i class="fa fa-check"></i><b>12.4.2</b> Periods</a></li>
<li class="chapter" data-level="12.4.3" data-path="dates-and-times.html"><a href="dates-and-times.html#intervals"><i class="fa fa-check"></i><b>12.4.3</b> Intervals</a></li>
<li class="chapter" data-level="12.4.4" data-path="dates-and-times.html"><a href="dates-and-times.html#exercises-40"><i class="fa fa-check"></i><b>12.4.4</b> Exercises</a></li>
<li class="chapter" data-level="12.4.5" data-path="dates-and-times.html"><a href="dates-and-times.html#time-zones"><i class="fa fa-check"></i><b>12.4.5</b> Time Zones</a></li>
</ul></li>
</ul></li>
<li class="part"><span><b>III Program</b></span></li>
<li class="chapter" data-level="13" data-path="program-intro.html"><a href="program-intro.html"><i class="fa fa-check"></i><b>13</b> Introduction</a></li>
<li class="chapter" data-level="14" data-path="pipes.html"><a href="pipes.html"><i class="fa fa-check"></i><b>14</b> Pipes</a></li>
<li class="chapter" data-level="15" data-path="vectors.html"><a href="vectors.html"><i class="fa fa-check"></i><b>15</b> Vectors</a><ul>
<li class="chapter" data-level="15.1" data-path="vectors.html"><a href="vectors.html#introduction-6"><i class="fa fa-check"></i><b>15.1</b> Introduction</a></li>
<li class="chapter" data-level="15.2" data-path="vectors.html"><a href="vectors.html#important-types-of-atomic-vector"><i class="fa fa-check"></i><b>15.2</b> Important types of Atomic Vector</a><ul>
<li class="chapter" data-level="15.2.1" data-path="vectors.html"><a href="vectors.html#exercises-41"><i class="fa fa-check"></i><b>15.2.1</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="15.3" data-path="vectors.html"><a href="vectors.html#using-atomic-vectors"><i class="fa fa-check"></i><b>15.3</b> Using atomic vectors</a></li>
<li class="chapter" data-level="15.4" data-path="vectors.html"><a href="vectors.html#recursive-vectors-lists"><i class="fa fa-check"></i><b>15.4</b> Recursive Vectors (lists)</a><ul>
<li class="chapter" data-level="15.4.1" data-path="vectors.html"><a href="vectors.html#exercises-42"><i class="fa fa-check"></i><b>15.4.1</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="15.5" data-path="vectors.html"><a href="vectors.html#augmented-vectors"><i class="fa fa-check"></i><b>15.5</b> Augmented Vectors</a><ul>
<li class="chapter" data-level="15.5.1" data-path="vectors.html"><a href="vectors.html#exercises-43"><i class="fa fa-check"></i><b>15.5.1</b> Exercises</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="16" data-path="iteration.html"><a href="iteration.html"><i class="fa fa-check"></i><b>16</b> Iteration</a><ul>
<li class="chapter" data-level="16.1" data-path="iteration.html"><a href="iteration.html#introduction-7"><i class="fa fa-check"></i><b>16.1</b> Introduction</a></li>
<li class="chapter" data-level="16.2" data-path="iteration.html"><a href="iteration.html#for-loops"><i class="fa fa-check"></i><b>16.2</b> For Loops</a><ul>
<li class="chapter" data-level="16.2.1" data-path="iteration.html"><a href="iteration.html#exercises-44"><i class="fa fa-check"></i><b>16.2.1</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="16.3" data-path="iteration.html"><a href="iteration.html#for-loop-variations"><i class="fa fa-check"></i><b>16.3</b> For loop variations</a><ul>
<li class="chapter" data-level="16.3.1" data-path="iteration.html"><a href="iteration.html#section"><i class="fa fa-check"></i><b>16.3.1</b> </a></li>
</ul></li>
<li class="chapter" data-level="16.4" data-path="iteration.html"><a href="iteration.html#for-loops-vs.functionals"><i class="fa fa-check"></i><b>16.4</b> For loops vs. functionals</a><ul>
<li class="chapter" data-level="16.4.1" data-path="iteration.html"><a href="iteration.html#exercises-45"><i class="fa fa-check"></i><b>16.4.1</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="16.5" data-path="iteration.html"><a href="iteration.html#the-map-functions"><i class="fa fa-check"></i><b>16.5</b> The map functions</a><ul>
<li class="chapter" data-level="16.5.1" data-path="iteration.html"><a href="iteration.html#shortcuts"><i class="fa fa-check"></i><b>16.5.1</b> Shortcuts</a></li>
<li class="chapter" data-level="16.5.2" data-path="iteration.html"><a href="iteration.html#exercises-46"><i class="fa fa-check"></i><b>16.5.2</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="16.6" data-path="iteration.html"><a href="iteration.html#dealing-with-failure"><i class="fa fa-check"></i><b>16.6</b> Dealing with Failure</a></li>
<li class="chapter" data-level="16.7" data-path="iteration.html"><a href="iteration.html#mapping-over-multiple-arguments"><i class="fa fa-check"></i><b>16.7</b> Mapping over multiple arguments</a></li>
<li class="chapter" data-level="16.8" data-path="iteration.html"><a href="iteration.html#walk"><i class="fa fa-check"></i><b>16.8</b> Walk</a></li>
<li class="chapter" data-level="16.9" data-path="iteration.html"><a href="iteration.html#other-patterns-of-for-loops"><i class="fa fa-check"></i><b>16.9</b> Other patterns of for loops</a><ul>
<li class="chapter" data-level="16.9.1" data-path="iteration.html"><a href="iteration.html#exercises-47"><i class="fa fa-check"></i><b>16.9.1</b> Exercises</a></li>
</ul></li>
</ul></li>
<li class="part"><span><b>IV Model</b></span></li>
<li class="chapter" data-level="17" data-path="model-intro.html"><a href="model-intro.html"><i class="fa fa-check"></i><b>17</b> Introduction</a></li>
<li class="chapter" data-level="18" data-path="model-basics.html"><a href="model-basics.html"><i class="fa fa-check"></i><b>18</b> Model Basics</a><ul>
<li class="chapter" data-level="18.1" data-path="model-basics.html"><a href="model-basics.html#prerequisites-3"><i class="fa fa-check"></i><b>18.1</b> Prerequisites</a></li>
<li class="chapter" data-level="18.2" data-path="model-basics.html"><a href="model-basics.html#a-simple-model"><i class="fa fa-check"></i><b>18.2</b> A simple model</a><ul>
<li class="chapter" data-level="18.2.1" data-path="model-basics.html"><a href="model-basics.html#exercises-48"><i class="fa fa-check"></i><b>18.2.1</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="18.3" data-path="model-basics.html"><a href="model-basics.html#visualizing-models"><i class="fa fa-check"></i><b>18.3</b> Visualizing Models</a><ul>
<li class="chapter" data-level="18.3.1" data-path="model-basics.html"><a href="model-basics.html#exercises-49"><i class="fa fa-check"></i><b>18.3.1</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="18.4" data-path="model-basics.html"><a href="model-basics.html#formulas-and-model-families"><i class="fa fa-check"></i><b>18.4</b> Formulas and Model Families</a><ul>
<li class="chapter" data-level="18.4.1" data-path="model-basics.html"><a href="model-basics.html#categorical-variables"><i class="fa fa-check"></i><b>18.4.1</b> Categorical Variables</a></li>
<li class="chapter" data-level="18.4.2" data-path="model-basics.html"><a href="model-basics.html#exercises-50"><i class="fa fa-check"></i><b>18.4.2</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="18.5" data-path="model-basics.html"><a href="model-basics.html#missing-values-2"><i class="fa fa-check"></i><b>18.5</b> Missing values</a></li>
<li class="chapter" data-level="18.6" data-path="model-basics.html"><a href="model-basics.html#other-model-families"><i class="fa fa-check"></i><b>18.6</b> Other model families</a></li>
</ul></li>
<li class="part"><span><b>V Communicate</b></span></li>
<li class="chapter" data-level="19" data-path="communicate-intro.html"><a href="communicate-intro.html"><i class="fa fa-check"></i><b>19</b> Introduction</a></li>
<li class="chapter" data-level="20" data-path="r-markdown.html"><a href="r-markdown.html"><i class="fa fa-check"></i><b>20</b> R Markdown</a><ul>
<li class="chapter" data-level="20.1" data-path="r-markdown.html"><a href="r-markdown.html#r-markdown-basics"><i class="fa fa-check"></i><b>20.1</b> R Markdown Basics</a><ul>
<li class="chapter" data-level="20.1.1" data-path="r-markdown.html"><a href="r-markdown.html#exercise"><i class="fa fa-check"></i><b>20.1.1</b> Exercise</a></li>
</ul></li>
<li class="chapter" data-level="20.2" data-path="r-markdown.html"><a href="r-markdown.html#text-formatting-with-r-markdown"><i class="fa fa-check"></i><b>20.2</b> Text formatting with R Markdown</a></li>
</ul></li>
<li class="chapter" data-level="21" data-path="r-markdown-formats.html"><a href="r-markdown-formats.html"><i class="fa fa-check"></i><b>21</b> R Markdown Formats</a></li>
<li class="chapter" data-level="22" data-path="r-markdown-workflow.html"><a href="r-markdown-workflow.html"><i class="fa fa-check"></i><b>22</b> R Markdown Workflow</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="./">Exercise Solutions and Notes for “R for Data Science”</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<section class="normal" id="section-">
<div id="exploratory-data-analysis" class="section level1">
<h1><span class="header-section-number">5</span> Exploratory Data Analysis</h1>
<div id="introduction-1" class="section level2">
<h2><span class="header-section-number">5.1</span> Introduction</h2>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(<span class="st">"tidyverse"</span>)
<span class="kw">library</span>(<span class="st">"viridis"</span>)
<span class="kw">library</span>(<span class="st">"forcats"</span>)</code></pre></div>
<p>This will also use data from <strong>nycflights13</strong>,</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(<span class="st">"nycflights13"</span>)</code></pre></div>
<div id="questions" class="section level3">
<h3><span class="header-section-number">5.1.1</span> Questions</h3>
</div>
<div id="variation" class="section level3">
<h3><span class="header-section-number">5.1.2</span> Variation</h3>
<div id="exercises-10" class="section level4">
<h4><span class="header-section-number">5.1.2.1</span> Exercises</h4>
<!-- 7.3.4 Exercises -->
<p><em>1. Explore the distribution of each of the x, y, and z variables in diamonds. What do you learn? Think about a diamond and how you might decide which dimension is the length, width, and depth.</em></p>
<p>In order to make it eaiser to plot them, I’ll reshape the dataset so that I can use the variables as facets.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">diamonds %>%
<span class="st"> </span><span class="kw">mutate</span>(<span class="dt">id =</span> <span class="kw">row_number</span>()) %>%
<span class="st"> </span><span class="kw">select</span>(x, y, z, id) %>%
<span class="st"> </span><span class="kw">gather</span>(variable, value, -id) %>%
<span class="st"> </span><span class="kw">ggplot</span>(<span class="kw">aes</span>(<span class="dt">x =</span> value)) +
<span class="st"> </span><span class="kw">geom_density</span>() +
<span class="st"> </span><span class="kw">geom_rug</span>() +
<span class="st"> </span><span class="kw">facet_grid</span>(variable ~<span class="st"> </span>.)</code></pre></div>
<p><img src="EDA_files/figure-html/unnamed-chunk-4-1.png" width="70%" style="display: block; margin: auto;" /></p>
<p>There several noticeable features of thedistributions</p>
<ol style="list-style-type: decimal">
<li>They are right skewed, with most diamonds small, but a few very large ones.</li>
<li>There is an outlier in <code>y</code>, and <code>z</code> (see the rug)</li>
<li>All three distributions have a bimodality (perhaps due to some sort of threshhold)</li>
</ol>
<p>According to the documentation for <code>diamonds</code>: <code>x</code> is length, <code>y</code> is width, and <code>z</code> is depth. I don’t know if I would have figured that out before; maybe if there was data on the type of cuts.</p>
<p><em>2. Explore the distribution of price. Do you discover anything unusual or surprising? (Hint: Carefully think about the binwidth and make sure you try a wide range of values.)</em></p>
<ul>
<li>The price data is spikey, but I can’t tell what that corresponds to, as the following plots don’t show much difference in the distributions in the last one and last two digits.</li>
<li>There are no diamonds with a price of 1500</li>
<li>There’s a bulge in the distribution around 7500.</li>
</ul>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">ggplot</span>(<span class="kw">filter</span>(diamonds, price <<span class="st"> </span><span class="dv">2500</span>), <span class="kw">aes</span>(<span class="dt">x =</span> price)) +
<span class="st"> </span><span class="kw">geom_histogram</span>(<span class="dt">binwidth =</span> <span class="dv">10</span>, <span class="dt">center =</span> <span class="dv">0</span>)</code></pre></div>
<p><img src="EDA_files/figure-html/unnamed-chunk-5-1.png" width="70%" style="display: block; margin: auto;" /></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">ggplot</span>(<span class="kw">filter</span>(diamonds), <span class="kw">aes</span>(<span class="dt">x =</span> price)) +
<span class="st"> </span><span class="kw">geom_histogram</span>(<span class="dt">binwidth =</span> <span class="dv">100</span>, <span class="dt">center =</span> <span class="dv">0</span>)</code></pre></div>
<p><img src="EDA_files/figure-html/unnamed-chunk-6-1.png" width="70%" style="display: block; margin: auto;" /></p>
<p>Distribution of last digit</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">diamonds %>%
<span class="st"> </span><span class="kw">mutate</span>(<span class="dt">ending =</span> price %%<span class="st"> </span><span class="dv">10</span>) %>%
<span class="st"> </span><span class="kw">ggplot</span>(<span class="kw">aes</span>(<span class="dt">x =</span> ending)) +
<span class="st"> </span><span class="kw">geom_histogram</span>(<span class="dt">binwidth =</span> <span class="dv">1</span>, <span class="dt">center =</span> <span class="dv">0</span>) +
<span class="st"> </span><span class="kw">geom_bar</span>()</code></pre></div>
<p><img src="EDA_files/figure-html/unnamed-chunk-7-1.png" width="70%" style="display: block; margin: auto;" /></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">diamonds %>%
<span class="st"> </span><span class="kw">mutate</span>(<span class="dt">ending =</span> price %%<span class="st"> </span><span class="dv">100</span>) %>%
<span class="st"> </span><span class="kw">ggplot</span>(<span class="kw">aes</span>(<span class="dt">x =</span> ending)) +
<span class="st"> </span><span class="kw">geom_histogram</span>(<span class="dt">binwidth =</span> <span class="dv">1</span>) +
<span class="st"> </span><span class="kw">geom_bar</span>()</code></pre></div>
<p><img src="EDA_files/figure-html/unnamed-chunk-8-1.png" width="70%" style="display: block; margin: auto;" /></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">diamonds %>%
<span class="st"> </span><span class="kw">mutate</span>(<span class="dt">ending =</span> price %%<span class="st"> </span><span class="dv">1000</span>) %>%
<span class="st"> </span><span class="kw">filter</span>(ending >=<span class="st"> </span><span class="dv">500</span>, ending <=<span class="st"> </span><span class="dv">800</span>) %>%
<span class="st"> </span><span class="kw">ggplot</span>(<span class="kw">aes</span>(<span class="dt">x =</span> ending)) +
<span class="st"> </span><span class="kw">geom_histogram</span>(<span class="dt">binwidth =</span> <span class="dv">1</span>) +
<span class="st"> </span><span class="kw">geom_bar</span>()</code></pre></div>
<p><img src="EDA_files/figure-html/unnamed-chunk-9-1.png" width="70%" style="display: block; margin: auto;" /></p>
<p><em>3. How many diamonds are 0.99 carat? How many are 1 carat? What do you think is the cause of the difference?</em></p>
<p>There are more than 70 times as many 1 carat diamonds as 0.99 carat diamond.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">diamonds %>%
<span class="st"> </span><span class="kw">filter</span>(carat >=<span class="st"> </span><span class="fl">0.99</span>, carat <=<span class="st"> </span><span class="dv">1</span>) %>%
<span class="st"> </span><span class="kw">count</span>(carat)
<span class="co">#> # A tibble: 2 × 2</span>
<span class="co">#> carat n</span>
<span class="co">#> <dbl> <int></span>
<span class="co">#> 1 0.99 23</span>
<span class="co">#> 2 1.00 1558</span></code></pre></div>
<p>I don’t know exactly the process behind how carats are measured, but some way or another some diamonds carat values are being “rounded up”, because presumably there is a premium for a 1 carat diamond vs. a 0.99 carat diamond beyond the expected increase in price due to a 0.01 carat increase.</p>
<p>To check this intuition, we’d want to look at the number of diamonds in each carat range to seem if there is an abnormally low number at 0.99 carats, and an abnormally high number at 1 carat.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">diamonds %>%
<span class="st"> </span><span class="kw">filter</span>(carat >=<span class="st"> </span><span class="fl">0.9</span>, carat <=<span class="st"> </span><span class="fl">1.1</span>) %>%
<span class="st"> </span><span class="kw">count</span>(carat) %>%
<span class="st"> </span><span class="kw">print</span>(<span class="dt">n =</span> <span class="dv">30</span>)
<span class="co">#> # A tibble: 21 × 2</span>
<span class="co">#> carat n</span>
<span class="co">#> <dbl> <int></span>
<span class="co">#> 1 0.90 1485</span>
<span class="co">#> 2 0.91 570</span>
<span class="co">#> 3 0.92 226</span>
<span class="co">#> 4 0.93 142</span>
<span class="co">#> 5 0.94 59</span>
<span class="co">#> 6 0.95 65</span>
<span class="co">#> 7 0.96 103</span>
<span class="co">#> 8 0.97 59</span>
<span class="co">#> 9 0.98 31</span>
<span class="co">#> 10 0.99 23</span>
<span class="co">#> 11 1.00 1558</span>
<span class="co">#> 12 1.01 2242</span>
<span class="co">#> 13 1.02 883</span>
<span class="co">#> 14 1.03 523</span>
<span class="co">#> 15 1.04 475</span>
<span class="co">#> 16 1.05 361</span>
<span class="co">#> 17 1.06 373</span>
<span class="co">#> 18 1.07 342</span>
<span class="co">#> 19 1.08 246</span>
<span class="co">#> 20 1.09 287</span>
<span class="co">#> 21 1.10 278</span></code></pre></div>
<p><strong>Q</strong> Can you think of other examples of similar phenoma where you might expect to see similar discontinuities in areas related to your research.</p>
<ol start="4" style="list-style-type: decimal">
<li>Compare and contrast <code>coord_cartesian()</code> vs <code>xlim()</code> or <code>ylim()</code> when zooming in on a histogram. What happens if you leave <code>binwidth</code> unset? What happens if you try and zoom so only half a bar shows?</li>
</ol>
<p><code>coord_cartesian</code> simply zooms in on the area specified by the limits. The calculation of the histogram is unaffected.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">ggplot</span>(diamonds) +<span class="st"> </span>
<span class="st"> </span><span class="kw">geom_histogram</span>(<span class="dt">mapping =</span> <span class="kw">aes</span>(<span class="dt">x =</span> price)) +
<span class="st"> </span><span class="kw">coord_cartesian</span>(<span class="dt">xlim =</span> <span class="kw">c</span>(<span class="dv">100</span>, <span class="dv">5000</span>), <span class="dt">ylim =</span> <span class="kw">c</span>(<span class="dv">0</span>, <span class="dv">3000</span>))
<span class="co">#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.</span></code></pre></div>
<p><img src="EDA_files/figure-html/unnamed-chunk-12-1.png" width="70%" style="display: block; margin: auto;" /></p>
<p>However, the <code>xlim</code> and <code>ylim</code> functions first drop any values outside the limits (the <code>ylim</code> doesn’t matter in this case), then calculates the histogram, and draws the graph with the given limits.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">ggplot</span>(diamonds) +<span class="st"> </span>
<span class="st"> </span><span class="kw">geom_histogram</span>(<span class="dt">mapping =</span> <span class="kw">aes</span>(<span class="dt">x =</span> price)) +
<span class="st"> </span><span class="kw">xlim</span>(<span class="dv">100</span>, <span class="dv">5000</span>) +
<span class="st"> </span><span class="kw">ylim</span>(<span class="dv">0</span>, <span class="dv">3000</span>)
<span class="co">#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.</span>
<span class="co">#> Warning: Removed 14714 rows containing non-finite values (stat_bin).</span>
<span class="co">#> Warning: Removed 5 rows containing missing values (geom_bar).</span></code></pre></div>
<p><img src="EDA_files/figure-html/unnamed-chunk-13-1.png" width="70%" style="display: block; margin: auto;" /></p>
</div>
</div>
</div>
<div id="missing-values" class="section level2">
<h2><span class="header-section-number">5.2</span> Missing Values</h2>
<div id="exercises-11" class="section level3">
<h3><span class="header-section-number">5.2.1</span> Exercises</h3>
<ol style="list-style-type: decimal">
<li>What happens to missing values in a histogram? What happens to missing values in a bar chart? Why is there a difference?</li>
</ol>
<p>Missing values are removed when the number of observations in each bin are calculated. See the warning message: <code>Removed 9 rows containing non-finite values (stat_bin)</code></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">diamonds2 <-<span class="st"> </span>diamonds %>%<span class="st"> </span>
<span class="st"> </span><span class="kw">mutate</span>(<span class="dt">y =</span> <span class="kw">ifelse</span>(y <<span class="st"> </span><span class="dv">3</span> |<span class="st"> </span>y ><span class="st"> </span><span class="dv">20</span>, <span class="ot">NA</span>, y))
<span class="kw">ggplot</span>(diamonds2, <span class="kw">aes</span>(<span class="dt">x =</span> y)) +
<span class="st"> </span><span class="kw">geom_histogram</span>()
<span class="co">#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.</span>
<span class="co">#> Warning: Removed 9 rows containing non-finite values (stat_bin).</span></code></pre></div>
<p><img src="EDA_files/figure-html/unnamed-chunk-14-1.png" width="70%" style="display: block; margin: auto;" /></p>
<p>In <code>geom_bar</code>, <code>NA</code> is treated as another category. This is because the <code>x</code> aesthetic in <code>geom_bar</code> should be a discrete (categorical) variable, and missing values are just another category.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">diamonds %>%
<span class="st"> </span><span class="kw">mutate</span>(<span class="dt">cut =</span> <span class="kw">if_else</span>(<span class="kw">runif</span>(<span class="kw">n</span>()) <<span class="st"> </span><span class="fl">0.1</span>, <span class="ot">NA_character_</span>, <span class="kw">as.character</span>(cut))) %>%
<span class="st"> </span><span class="kw">ggplot</span>() +
<span class="st"> </span><span class="kw">geom_bar</span>(<span class="dt">mapping =</span> <span class="kw">aes</span>(<span class="dt">x =</span> cut))</code></pre></div>
<p><img src="EDA_files/figure-html/unnamed-chunk-15-1.png" width="70%" style="display: block; margin: auto;" /></p>
<p>In a histogram, the <code>x</code> aesthetic variable needs to be numeric, and <code>stat_bin</code> groups the observations by ranges into bins. Since the numeric value of the <code>NA</code> observations is unknown, they cannot be placed in a particular bin, and are dropped.</p>
<ol start="2" style="list-style-type: decimal">
<li>What does <code>na.rm = TRUE</code> do in <code>mean()</code> and <code>sum()</code>?</li>
</ol>
<p>This option removes <code>NA</code> values from the vector prior to calculating the mean and sum.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">mean</span>(<span class="kw">c</span>(<span class="dv">0</span>, <span class="dv">1</span>, <span class="dv">2</span>, <span class="ot">NA</span>), <span class="dt">na.rm =</span> <span class="ot">TRUE</span>)
<span class="co">#> [1] 1</span>
<span class="kw">sum</span>(<span class="kw">c</span>(<span class="dv">0</span>, <span class="dv">1</span>, <span class="dv">2</span>, <span class="ot">NA</span>), <span class="dt">na.rm =</span> <span class="ot">TRUE</span>)
<span class="co">#> [1] 3</span></code></pre></div>
</div>
</div>
<div id="covariation" class="section level2">
<h2><span class="header-section-number">5.3</span> Covariation</h2>
<div id="a-categorical-and-continuous-variable" class="section level3">
<h3><span class="header-section-number">5.3.1</span> A categorical and continuous variable</h3>
<p>For a history of the boxplot see Wikckham [40 years of the boxplot] (<a href="http://vita.had.co.nz/papers/boxplots.pdf" class="uri">http://vita.had.co.nz/papers/boxplots.pdf</a>)</p>
<p>Krywinski, Martin, and Naomi Altman. 2014. “Points of Significance: Visualizing samples with box plots.” <em>Nature Methods</em> <a href="http://www.nature.com/nmeth/journal/v11/n2/full/nmeth.2813.html">URL</a></p>
<p>Where does the 1.5 x IQR come from? It’s kind of arbitrary. But in a normal distribution, the IQR is approximatley 2, and 1.5 x IQR is approx 4, so the outliers are approximately within 4 standard deviations of the median (mean).</p>
<div id="excercises" class="section level4">
<h4><span class="header-section-number">5.3.1.1</span> Excercises</h4>
<ol style="list-style-type: decimal">
<li>Use what you’ve learned to improve the visualisation of the departure times of cancelled vs. non-cancelled flights.</li>
</ol>
<p>Instead of a <code>freqplot</code> use a box-plot</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">nycflights13::flights %>%<span class="st"> </span>
<span class="st"> </span><span class="kw">mutate</span>(
<span class="dt">cancelled =</span> <span class="kw">is.na</span>(dep_time),
<span class="dt">sched_hour =</span> sched_dep_time %/%<span class="st"> </span><span class="dv">100</span>,
<span class="dt">sched_min =</span> sched_dep_time %%<span class="st"> </span><span class="dv">100</span>,
<span class="dt">sched_dep_time =</span> sched_hour +<span class="st"> </span>sched_min /<span class="st"> </span><span class="dv">60</span>
) %>%<span class="st"> </span>
<span class="st"> </span><span class="kw">ggplot</span>() +
<span class="st"> </span><span class="kw">geom_boxplot</span>(<span class="dt">mapping =</span> <span class="kw">aes</span>(<span class="dt">y =</span> sched_dep_time, <span class="dt">x =</span> cancelled))</code></pre></div>
<p><img src="EDA_files/figure-html/unnamed-chunk-17-1.png" width="70%" style="display: block; margin: auto;" /></p>
<ol start="2" style="list-style-type: decimal">
<li>What variable in the diamonds dataset is most important for predicting the price of a diamond? How is that variable correlated with cut? Why does the combination of those two relationships lead to lower quality diamonds being more expensive?</li>
</ol>
<p>I’m not exactly sure what this question is asking conditional on using only the tools introduced in the book thus far.</p>
<ol start="3" style="list-style-type: decimal">
<li>Install the <strong>ggstance</strong> package, and create a horizontal boxplot. How does this compare to using <code>coord_flip()</code>?</li>
</ol>
<p>Earlier we created a horizontal boxplot of the distribution <code>hwy</code> by <code>class</code>, using <code>geom_boxplot</code> and <code>coord_flip</code>:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">ggplot</span>(<span class="dt">data =</span> mpg) +
<span class="st"> </span><span class="kw">geom_boxplot</span>(<span class="dt">mapping =</span> <span class="kw">aes</span>(<span class="dt">x =</span> <span class="kw">reorder</span>(class, hwy, <span class="dt">FUN =</span> median), <span class="dt">y =</span> hwy)) +
<span class="st"> </span><span class="kw">coord_flip</span>()</code></pre></div>
<p><img src="EDA_files/figure-html/unnamed-chunk-18-1.png" width="70%" style="display: block; margin: auto;" /></p>
<p>In this case the output looks the same, but in the aesthetics the <code>x</code> and <code>y</code> are flipped from the previous case.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(<span class="st">"ggstance"</span>)
<span class="kw">ggplot</span>(<span class="dt">data =</span> mpg) +
<span class="st"> </span><span class="kw">geom_boxploth</span>(<span class="dt">mapping =</span> <span class="kw">aes</span>(<span class="dt">y =</span> <span class="kw">reorder</span>(class, hwy, <span class="dt">FUN =</span> median), <span class="dt">x =</span> hwy))</code></pre></div>
<p><img src="EDA_files/figure-html/unnamed-chunk-19-1.png" width="70%" style="display: block; margin: auto;" /></p>
<ol start="4" style="list-style-type: decimal">
<li>One problem with boxplots is that they were developed in an era of much smaller datasets and tend to display a prohibitively large number of “outlying values”. One approach to remedy this problem is the letter value plot. Install the <strong>lvplot</strong> package, and try using <code>geom_lv()</code> to display the distribution of price vs cut. What do you learn? How do you interpret the plots?</li>
</ol>
<p>The boxes of the letter-value plot correspond to many more quantiles. They are useful for larger datasets because</p>
<ol style="list-style-type: decimal">
<li>larger datasets can give precise estiamtes of quantiles beyond the quartiles</li>
<li>in expectation, larger datasets should have many more outliers</li>
</ol>
<p>The letter-value plot is described in:</p>
<blockquote>
<p>Heike Hofmann, Karen Kafadar, and Hadley Wickham. 2011. “Letter-value plots: Boxplots for large data” <a href="http://vita.had.co.nz/papers/letter-value-plot.pdf" class="uri">http://vita.had.co.nz/papers/letter-value-plot.pdf</a></p>
</blockquote>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(<span class="st">"lvplot"</span>)
<span class="kw">ggplot</span>(diamonds, <span class="kw">aes</span>(<span class="dt">x =</span> cut, <span class="dt">y =</span> price)) +
<span class="st"> </span><span class="kw">geom_lv</span>()</code></pre></div>
<p><img src="EDA_files/figure-html/unnamed-chunk-20-1.png" width="70%" style="display: block; margin: auto;" /></p>
<ol start="5" style="list-style-type: decimal">
<li>Compare and contrast <code>geom_violin()</code> with a facetted <code>geom_histogram()</code>, or a coloured <code>geom_freqpoly()</code>. What are the pros and cons of each method?</li>
</ol>
<p>I produce plots for these three methods below. The <code>geom_freqpoly</code> is better for look-up: meaning that given a price, it is easy to tell which <code>cut</code> has the highest density. However, the overlapping lines makes it difficult to distinguish how the overall distributions relate to each other. The <code>geom_violin</code> and facetted <code>geom_histogram</code> have similar strengths and weaknesses. It is easy to visually distinguish differences in the overall shape of the distributions (skewness, central values, variance, etc). However, since we can’t easily compare the vertical values of the distribution, its difficult to look up which category has the highest density for a given price. All of these methods depend on tuning parameters to determine the level of smoothness of the distribution.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">ggplot</span>(<span class="dt">data =</span> diamonds, <span class="dt">mapping =</span> <span class="kw">aes</span>(<span class="dt">x =</span> price, <span class="dt">y =</span> ..density..)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">geom_freqpoly</span>(<span class="dt">mapping =</span> <span class="kw">aes</span>(<span class="dt">colour =</span> cut), <span class="dt">binwidth =</span> <span class="dv">500</span>)</code></pre></div>
<p><img src="EDA_files/figure-html/unnamed-chunk-21-1.png" width="70%" style="display: block; margin: auto;" /></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">ggplot</span>(<span class="dt">data =</span> diamonds, <span class="dt">mapping =</span> <span class="kw">aes</span>(<span class="dt">x =</span> price)) +
<span class="st"> </span><span class="kw">geom_histogram</span>() +
<span class="st"> </span><span class="kw">facet_wrap</span>(~<span class="st"> </span>cut, <span class="dt">ncol =</span> <span class="dv">1</span>, <span class="dt">scales =</span> <span class="st">"free_y"</span>)
<span class="co">#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.</span></code></pre></div>
<p><img src="EDA_files/figure-html/unnamed-chunk-22-1.png" width="70%" style="display: block; margin: auto;" /></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">ggplot</span>(<span class="dt">data =</span> diamonds, <span class="dt">mapping =</span> <span class="kw">aes</span>(<span class="dt">x =</span> cut, <span class="dt">y =</span> price)) +
<span class="st"> </span><span class="kw">geom_violin</span>() +
<span class="st"> </span><span class="kw">coord_flip</span>()</code></pre></div>
<p><img src="EDA_files/figure-html/unnamed-chunk-23-1.png" width="70%" style="display: block; margin: auto;" /></p>
<p>The violin plot was first described in</p>
<blockquote>
<p>Hintze JL, Nelson RD (1998). “Violin Plots: A Box Plot-Density Trace Synergism.” The American Statistician, 52(2), 181–184</p>
</blockquote>
<ol start="6" style="list-style-type: decimal">
<li>If you have a small dataset, it’s sometimes useful to use <code>geom_jitter()</code> to see the relationship between a continuous and categorical variable. The <strong>ggbeeswarm</strong> package provides a number of methods similar to <code>geom_jitter()</code>. List them and briefly describe what each one does.</li>
</ol>
<p>There are two methods:</p>
<ul>
<li><code>geom_quasirandom</code> that produces plots that resemble something between jitter and violin. There are several different methods that determine exactly how the random location of the points is generated.</li>
<li><code>geom_beeswarm</code> creates a shape similar to a violin plot, but by offsetting the points.</li>
</ul>
<p>I’ll use the <code>mpg</code> boxplot example since these methods display individual points, they are better suited for smaller datasets.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(<span class="st">"ggbeeswarm"</span>)
<span class="kw">ggplot</span>(<span class="dt">data =</span> mpg) +
<span class="st"> </span><span class="kw">geom_quasirandom</span>(<span class="dt">mapping =</span> <span class="kw">aes</span>(<span class="dt">x =</span> <span class="kw">reorder</span>(class, hwy, <span class="dt">FUN =</span> median),
<span class="dt">y =</span> hwy))</code></pre></div>
<p><img src="EDA_files/figure-html/unnamed-chunk-24-1.png" width="70%" style="display: block; margin: auto;" /></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">ggplot</span>(<span class="dt">data =</span> mpg) +
<span class="st"> </span><span class="kw">geom_quasirandom</span>(<span class="dt">mapping =</span> <span class="kw">aes</span>(<span class="dt">x =</span> <span class="kw">reorder</span>(class, hwy, <span class="dt">FUN =</span> median),
<span class="dt">y =</span> hwy),
<span class="dt">method =</span> <span class="st">"tukey"</span>)</code></pre></div>
<p><img src="EDA_files/figure-html/unnamed-chunk-25-1.png" width="70%" style="display: block; margin: auto;" /></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">ggplot</span>(<span class="dt">data =</span> mpg) +
<span class="st"> </span><span class="kw">geom_quasirandom</span>(<span class="dt">mapping =</span> <span class="kw">aes</span>(<span class="dt">x =</span> <span class="kw">reorder</span>(class, hwy, <span class="dt">FUN =</span> median),
<span class="dt">y =</span> hwy),
<span class="dt">method =</span> <span class="st">"tukeyDense"</span>)</code></pre></div>
<p><img src="EDA_files/figure-html/unnamed-chunk-26-1.png" width="70%" style="display: block; margin: auto;" /></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">ggplot</span>(<span class="dt">data =</span> mpg) +
<span class="st"> </span><span class="kw">geom_quasirandom</span>(<span class="dt">mapping =</span> <span class="kw">aes</span>(<span class="dt">x =</span> <span class="kw">reorder</span>(class, hwy, <span class="dt">FUN =</span> median),
<span class="dt">y =</span> hwy),
<span class="dt">method =</span> <span class="st">"frowney"</span>)</code></pre></div>
<p><img src="EDA_files/figure-html/unnamed-chunk-27-1.png" width="70%" style="display: block; margin: auto;" /></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">ggplot</span>(<span class="dt">data =</span> mpg) +
<span class="st"> </span><span class="kw">geom_quasirandom</span>(<span class="dt">mapping =</span> <span class="kw">aes</span>(<span class="dt">x =</span> <span class="kw">reorder</span>(class, hwy, <span class="dt">FUN =</span> median),
<span class="dt">y =</span> hwy),
<span class="dt">method =</span> <span class="st">"smiley"</span>)</code></pre></div>
<p><img src="EDA_files/figure-html/unnamed-chunk-28-1.png" width="70%" style="display: block; margin: auto;" /></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">ggplot</span>(<span class="dt">data =</span> mpg) +
<span class="st"> </span><span class="kw">geom_beeswarm</span>(<span class="dt">mapping =</span> <span class="kw">aes</span>(<span class="dt">x =</span> <span class="kw">reorder</span>(class, hwy, <span class="dt">FUN =</span> median),
<span class="dt">y =</span> hwy))</code></pre></div>
<p><img src="EDA_files/figure-html/unnamed-chunk-29-1.png" width="70%" style="display: block; margin: auto;" /></p>
</div>
</div>
<div id="two-categorical-variables" class="section level3">
<h3><span class="header-section-number">5.3.2</span> Two categorical variables</h3>
<ol style="list-style-type: decimal">
<li>How could you rescale the count dataset above to more clearly show the distribution of cut within colour, or colour within cut?</li>
</ol>
<p>TO clearly show the distribution of <code>cut</code> within <code>color</code>, calculate a new variable <code>prop</code> which is the proportion of each cut within a <code>color</code>. This is done using a grouped mutate.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">diamonds %>%<span class="st"> </span>
<span class="st"> </span><span class="kw">count</span>(color, cut) %>%
<span class="st"> </span><span class="kw">group_by</span>(color) %>%
<span class="st"> </span><span class="kw">mutate</span>(<span class="dt">prop =</span> n /<span class="st"> </span><span class="kw">sum</span>(n)) %>%
<span class="st"> </span><span class="kw">ggplot</span>(<span class="dt">mapping =</span> <span class="kw">aes</span>(<span class="dt">x =</span> color, <span class="dt">y =</span> cut)) +
<span class="st"> </span><span class="kw">geom_tile</span>(<span class="dt">mapping =</span> <span class="kw">aes</span>(<span class="dt">fill =</span> prop)) +
<span class="st"> </span><span class="kw">scale_fill_viridis</span>(<span class="dt">limits =</span> <span class="kw">c</span>(<span class="dv">0</span>, <span class="dv">1</span>))</code></pre></div>
<p><img src="EDA_files/figure-html/unnamed-chunk-30-1.png" width="70%" style="display: block; margin: auto;" /></p>
<p>Similarly, to scale by the distribution of <code>color</code> within <code>cut</code>,</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">diamonds %>%<span class="st"> </span>
<span class="st"> </span><span class="kw">count</span>(color, cut) %>%
<span class="st"> </span><span class="kw">group_by</span>(cut) %>%
<span class="st"> </span><span class="kw">mutate</span>(<span class="dt">prop =</span> n /<span class="st"> </span><span class="kw">sum</span>(n)) %>%
<span class="st"> </span><span class="kw">ggplot</span>(<span class="dt">mapping =</span> <span class="kw">aes</span>(<span class="dt">x =</span> color, <span class="dt">y =</span> cut)) +
<span class="st"> </span><span class="kw">geom_tile</span>(<span class="dt">mapping =</span> <span class="kw">aes</span>(<span class="dt">fill =</span> prop)) +
<span class="st"> </span><span class="kw">scale_fill_viridis</span>(<span class="dt">limits =</span> <span class="kw">c</span>(<span class="dv">0</span>, <span class="dv">1</span>))</code></pre></div>
<p><img src="EDA_files/figure-html/unnamed-chunk-31-1.png" width="70%" style="display: block; margin: auto;" /></p>
<p>I add <code>limit = c(0, 1)</code> to put the color scale between (0, 1). These are the logical boundaries of proportions. This makes it possible to compare each cell to its actual value, and would improve comparisons across multiple plots. However, it ends up limiting the colors and makes it harder to compare within the dataset. However, using the default limits of the minimum and maximum values makes it easier to compare within the dataset the emphasizing relative differences, but harder to compare across datasets.</p>
<ol start="2" style="list-style-type: decimal">
<li>Use <code>geom_tile()</code> together with <strong>dplyr</strong> to explore how average flight delays vary by destination and month of year. What makes the plot difficult to read? How could you improve it?</li>
</ol>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">flights %>%
<span class="st"> </span><span class="kw">group_by</span>(month, dest) %>%
<span class="st"> </span><span class="kw">summarise</span>(<span class="dt">dep_delay =</span> <span class="kw">mean</span>(dep_delay, <span class="dt">na.rm =</span> <span class="ot">TRUE</span>)) %>%
<span class="st"> </span><span class="kw">ggplot</span>(<span class="kw">aes</span>(<span class="dt">x =</span> <span class="kw">factor</span>(month), <span class="dt">y =</span> dest, <span class="dt">fill =</span> dep_delay)) +
<span class="st"> </span><span class="kw">geom_tile</span>() +
<span class="st"> </span><span class="kw">labs</span>(<span class="dt">x =</span> <span class="st">"Month"</span>, <span class="dt">y =</span> <span class="st">"Destination"</span>, <span class="dt">fill =</span> <span class="st">"Departure Delay"</span>)</code></pre></div>
<p><img src="EDA_files/figure-html/unnamed-chunk-32-1.png" width="70%" style="display: block; margin: auto;" /></p>
<p>There are several things that could be done to improve it,</p>
<ul>
<li>sort destinations by a meaningful quanity (distance, number of flights, average delay)</li>
<li>remove missing values</li>
<li>better color scheme (viridis)</li>
</ul>
<p>How to treat missing values is difficult. In this case, missing values correspond to airports which don’t have regular flights (at least one flight each month) from NYC. These are likely smaller airports (with higher variance in their average due to fewer observations).</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(<span class="st">"viridis"</span>)
flights %>%
<span class="st"> </span><span class="kw">group_by</span>(month, dest) %>%
<span class="st"> </span><span class="kw">summarise</span>(<span class="dt">dep_delay =</span> <span class="kw">mean</span>(dep_delay, <span class="dt">na.rm =</span> <span class="ot">TRUE</span>)) %>%
<span class="st"> </span><span class="kw">group_by</span>(dest) %>%
<span class="st"> </span><span class="kw">filter</span>(<span class="kw">n</span>() ==<span class="st"> </span><span class="dv">12</span>) %>%
<span class="st"> </span><span class="kw">ungroup</span>() %>%
<span class="st"> </span><span class="kw">mutate</span>(<span class="dt">dest =</span> <span class="kw">fct_reorder</span>(dest, dep_delay)) %>%
<span class="st"> </span><span class="kw">ggplot</span>(<span class="kw">aes</span>(<span class="dt">x =</span> <span class="kw">factor</span>(month), <span class="dt">y =</span> dest, <span class="dt">fill =</span> dep_delay)) +
<span class="st"> </span><span class="kw">geom_tile</span>() +
<span class="st"> </span><span class="kw">scale_fill_viridis</span>() +
<span class="st"> </span><span class="kw">labs</span>(<span class="dt">x =</span> <span class="st">"Month"</span>, <span class="dt">y =</span> <span class="st">"Destination"</span>, <span class="dt">fill =</span> <span class="st">"Departure Delay"</span>)</code></pre></div>
<p><img src="EDA_files/figure-html/unnamed-chunk-33-1.png" width="70%" style="display: block; margin: auto;" /></p>
<ol start="3" style="list-style-type: decimal">
<li>Why is it slightly better to use <code>aes(x = color, y = cut)</code> rather than <code>aes(x = cut, y = color)</code> in the example above?</li>
</ol>
<p>It’s usually better to use the categorical variable with a larger number of categories or the longer labels on the y axis. If at all possible, labels should be horizontal because that is easier to read.</p>
<p>However, switching the order doesn’t result in overlapping labels.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">diamonds %>%<span class="st"> </span>
<span class="st"> </span><span class="kw">count</span>(color, cut) %>%<span class="st"> </span>
<span class="st"> </span><span class="kw">ggplot</span>(<span class="dt">mapping =</span> <span class="kw">aes</span>(<span class="dt">y =</span> color, <span class="dt">x =</span> cut)) +
<span class="st"> </span><span class="kw">geom_tile</span>(<span class="dt">mapping =</span> <span class="kw">aes</span>(<span class="dt">fill =</span> n))</code></pre></div>
<p><img src="EDA_files/figure-html/unnamed-chunk-34-1.png" width="70%" style="display: block; margin: auto;" /></p>
<p>Another justification, for switching the order is that the larger numbers are at the top when <code>x = color</code> and <code>y = cut</code>, and that lowers the cognitive burden of interpreting the plot.</p>
</div>
<div id="two-continuous-variables" class="section level3">
<h3><span class="header-section-number">5.3.3</span> Two continuous variables</h3>
<ol style="list-style-type: decimal">
<li>Instead of summarising the conditional distribution with a boxplot, you could use a frequency polygon. What do you need to consider when using <code>cut_width()</code> vs <code>cut_number()</code>? How does that impact a visualisation of the 2d distribution of <code>carat</code> and <code>price</code>?</li>
</ol>
<p>When using <code>cut_width</code> the number in each bin may be unequal. The distribution of <code>carat</code> is right skewed so there are few diamonds in those bins.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">ggplot</span>(<span class="dt">data =</span> diamonds,
<span class="dt">mapping =</span> <span class="kw">aes</span>(<span class="dt">x =</span> price,
<span class="dt">colour =</span> <span class="kw">cut_width</span>(carat, <span class="fl">0.3</span>))) +
<span class="st"> </span><span class="kw">geom_freqpoly</span>()
<span class="co">#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.</span></code></pre></div>
<p><img src="EDA_files/figure-html/unnamed-chunk-35-1.png" width="70%" style="display: block; margin: auto;" /> Plotting the density instead of counts will make the distributions comparable, although the bins with few observations will still be hard to interpret.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">ggplot</span>(<span class="dt">data =</span> diamonds,
<span class="dt">mapping =</span> <span class="kw">aes</span>(<span class="dt">x =</span> price,
<span class="dt">y =</span> ..density..,
<span class="dt">colour =</span> <span class="kw">cut_width</span>(carat, <span class="fl">0.3</span>))) +
<span class="st"> </span><span class="kw">geom_freqpoly</span>()
<span class="co">#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.</span></code></pre></div>
<p><img src="EDA_files/figure-html/unnamed-chunk-36-1.png" width="70%" style="display: block; margin: auto;" /> Plotting the density instead of counts will make the distributions comparable, although the bins with few observations will still be hard to interpret.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">ggplot</span>(<span class="dt">data =</span> diamonds,
<span class="dt">mapping =</span> <span class="kw">aes</span>(<span class="dt">x =</span> price,
<span class="dt">colour =</span> <span class="kw">cut_number</span>(carat, <span class="dv">10</span>))) +
<span class="st"> </span><span class="kw">geom_freqpoly</span>()
<span class="co">#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.</span></code></pre></div>
<p><img src="EDA_files/figure-html/unnamed-chunk-37-1.png" width="70%" style="display: block; margin: auto;" /> Since there are equal numbers in each bin, the plot looks the same if density is used for the y aesthetic (although the values are on a different scale).</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">ggplot</span>(<span class="dt">data =</span> diamonds,
<span class="dt">mapping =</span> <span class="kw">aes</span>(<span class="dt">x =</span> price,
<span class="dt">y =</span> ..density..,
<span class="dt">colour =</span> <span class="kw">cut_number</span>(carat, <span class="dv">10</span>))) +
<span class="st"> </span><span class="kw">geom_freqpoly</span>()
<span class="co">#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.</span></code></pre></div>
<p><img src="EDA_files/figure-html/unnamed-chunk-38-1.png" width="70%" style="display: block; margin: auto;" /></p>
<ol start="2" style="list-style-type: decimal">
<li>Visualise the distribution of <code>carat</code>, partitioned by <code>price</code>.</li>
</ol>
<p>With a boxplot, partitionining into an 10 bins with the same number of observations:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">ggplot</span>(diamonds, <span class="kw">aes</span>(<span class="dt">x =</span> <span class="kw">cut_number</span>(price, <span class="dv">10</span>), <span class="dt">y =</span> carat)) +
<span class="st"> </span><span class="kw">geom_boxplot</span>() +
<span class="st"> </span><span class="kw">coord_flip</span>() +
<span class="st"> </span><span class="kw">xlab</span>(<span class="st">"Price"</span>)</code></pre></div>
<p><img src="EDA_files/figure-html/unnamed-chunk-39-1.png" width="70%" style="display: block; margin: auto;" /> With a boxplot, partitionining into an bins of $2,000 with the width of the box determined by the number of observations. I use <code>boundary = 0</code> to ensure the first bin goes from $0–$2,000.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">ggplot</span>(diamonds, <span class="kw">aes</span>(<span class="dt">x =</span> <span class="kw">cut_width</span>(price, <span class="dv">2000</span>, <span class="dt">boundary =</span> <span class="dv">0</span>), <span class="dt">y =</span> carat)) +
<span class="st"> </span><span class="kw">geom_boxplot</span>(<span class="dt">varwidth =</span> <span class="ot">TRUE</span>) +
<span class="st"> </span><span class="kw">coord_flip</span>() +
<span class="st"> </span><span class="kw">xlab</span>(<span class="st">"Price"</span>)</code></pre></div>
<p><img src="EDA_files/figure-html/unnamed-chunk-40-1.png" width="70%" style="display: block; margin: auto;" /></p>
<ol start="3" style="list-style-type: decimal">
<li>How does the price distribution of very large diamonds compare to small diamonds. Is it as you expect, or does it surprise you?</li>
</ol>
<p>The distribution of very large diamonds is more variable. I’m not surprised, since I had a very weak prior about diamond prices. Ex post, I would reason that above a certain size other factors such as cut, clarity, color play more of a role in the price.</p>
<ol start="4" style="list-style-type: decimal">
<li>Combine two of the techniques you’ve learned to visualise the combined distribution of cut, carat, and price.</li>
</ol>
<p>There’s lots of options to try: Here’s a couple. What else did you try? What’s the best way?</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">ggplot</span>(diamonds, <span class="kw">aes</span>(<span class="dt">x =</span> carat, <span class="dt">y =</span> price)) +
<span class="st"> </span><span class="kw">geom_hex</span>() +
<span class="st"> </span><span class="kw">facet_wrap</span>(~<span class="st"> </span>cut, <span class="dt">ncol =</span> <span class="dv">1</span>) +
<span class="st"> </span><span class="kw">scale_fill_viridis</span>()
<span class="co">#> Loading required package: methods</span></code></pre></div>
<p><img src="EDA_files/figure-html/unnamed-chunk-41-1.png" width="70%" style="display: block; margin: auto;" /></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">ggplot</span>(diamonds, <span class="kw">aes</span>(<span class="dt">x =</span> <span class="kw">cut_number</span>(carat, <span class="dv">5</span>), <span class="dt">y =</span> price, <span class="dt">color =</span> cut)) +
<span class="st"> </span><span class="kw">geom_boxplot</span>()</code></pre></div>
<p><img src="EDA_files/figure-html/unnamed-chunk-42-1.png" width="70%" style="display: block; margin: auto;" /></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">ggplot</span>(diamonds, <span class="kw">aes</span>(<span class="dt">color =</span> <span class="kw">cut_number</span>(carat, <span class="dv">5</span>), <span class="dt">y =</span> price, <span class="dt">x =</span> cut)) +
<span class="st"> </span><span class="kw">geom_boxplot</span>()</code></pre></div>
<p><img src="EDA_files/figure-html/unnamed-chunk-43-1.png" width="70%" style="display: block; margin: auto;" /></p>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
</div>
<a href="data-transformation.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="tibbles.html" class="navigation navigation-next " aria-label="Next page""><i class="fa fa-angle-right"></i></a>
<script src="libs/gitbook-2.6.7/js/app.min.js"></script>
<script src="libs/gitbook-2.6.7/js/lunr.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>
require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": false,
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": "https://github.com/jrnold/r4ds/edit/master/EDA.Rmd",
"text": "Edit"
},
"download": null,
"toc": {
"collapse": "section"
}
});
});
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
if (location.protocol !== "file:" && /^https?:/.test(script.src))
script.src = script.src.replace(/^https?:/, '');
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>