-
Notifications
You must be signed in to change notification settings - Fork 23
/
01-eda_hot_dogs.html
789 lines (710 loc) · 32 KB
/
01-eda_hot_dogs.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
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />
<meta name="author" content="Alison Hill" />
<title>Lab 01: Nathan’s Hot-Dog Eating Contest</title>
<script src="site_libs/jquery-1.11.3/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="site_libs/bootstrap-3.3.5/css/flatly.min.css" rel="stylesheet" />
<script src="site_libs/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/respond.min.js"></script>
<script src="site_libs/jqueryui-1.11.4/jquery-ui.min.js"></script>
<link href="site_libs/tocify-1.9.1/jquery.tocify.css" rel="stylesheet" />
<script src="site_libs/tocify-1.9.1/jquery.tocify.js"></script>
<script src="site_libs/navigation-1.1/tabsets.js"></script>
<script src="site_libs/navigation-1.1/codefolding.js"></script>
<link href="site_libs/highlightjs-9.12.0/default.css" rel="stylesheet" />
<script src="site_libs/highlightjs-9.12.0/highlight.js"></script>
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
pre:not([class]) {
background-color: white;
}
</style>
<script type="text/javascript">
if (window.hljs) {
hljs.configure({languages: []});
hljs.initHighlightingOnLoad();
if (document.readyState && document.readyState === "complete") {
window.setTimeout(function() { hljs.initHighlighting(); }, 0);
}
}
</script>
<style type="text/css">
h1 {
font-size: 34px;
}
h1.title {
font-size: 38px;
}
h2 {
font-size: 30px;
}
h3 {
font-size: 24px;
}
h4 {
font-size: 18px;
}
h5 {
font-size: 16px;
}
h6 {
font-size: 12px;
}
.table th:not([align]) {
text-align: left;
}
</style>
</head>
<body>
<style type = "text/css">
.main-container {
max-width: 940px;
margin-left: auto;
margin-right: auto;
}
code {
color: inherit;
background-color: rgba(0, 0, 0, 0.04);
}
img {
max-width:100%;
height: auto;
}
.tabbed-pane {
padding-top: 12px;
}
button.code-folding-btn:focus {
outline: none;
}
</style>
<style type="text/css">
/* padding for bootstrap navbar */
body {
padding-top: 60px;
padding-bottom: 40px;
}
/* offset scroll position for anchor links (for fixed navbar) */
.section h1 {
padding-top: 65px;
margin-top: -65px;
}
.section h2 {
padding-top: 65px;
margin-top: -65px;
}
.section h3 {
padding-top: 65px;
margin-top: -65px;
}
.section h4 {
padding-top: 65px;
margin-top: -65px;
}
.section h5 {
padding-top: 65px;
margin-top: -65px;
}
.section h6 {
padding-top: 65px;
margin-top: -65px;
}
</style>
<script>
// manage active state of menu based on current page
$(document).ready(function () {
// active menu anchor
href = window.location.pathname
href = href.substr(href.lastIndexOf('/') + 1)
if (href === "")
href = "index.html";
var menuAnchor = $('a[href="' + href + '"]');
// mark it active
menuAnchor.parent().addClass('active');
// if it's got a parent navbar menu mark it active as well
menuAnchor.closest('li.dropdown').addClass('active');
});
</script>
<div class="container-fluid main-container">
<!-- tabsets -->
<script>
$(document).ready(function () {
window.buildTabsets("TOC");
});
</script>
<!-- code folding -->
<style type="text/css">
.code-folding-btn { margin-bottom: 4px; }
</style>
<script>
$(document).ready(function () {
window.initializeCodeFolding("hide" === "show");
});
</script>
<script>
$(document).ready(function () {
// move toc-ignore selectors from section div to header
$('div.section.toc-ignore')
.removeClass('toc-ignore')
.children('h1,h2,h3,h4,h5').addClass('toc-ignore');
// establish options
var options = {
selectors: "h1,h2",
theme: "bootstrap3",
context: '.toc-content',
hashGenerator: function (text) {
return text.replace(/[.\\/?&!#<>]/g, '').replace(/\s/g, '_').toLowerCase();
},
ignoreSelector: ".toc-ignore",
scrollTo: 0
};
options.showAndHide = true;
options.smoothScroll = true;
// tocify
var toc = $("#TOC").tocify(options).data("toc-tocify");
});
</script>
<style type="text/css">
#TOC {
margin: 25px 0px 20px 0px;
}
@media (max-width: 768px) {
#TOC {
position: relative;
width: 100%;
}
}
.toc-content {
padding-left: 30px;
padding-right: 40px;
}
div.main-container {
max-width: 1200px;
}
div.tocify {
width: 20%;
max-width: 260px;
max-height: 85%;
}
@media (min-width: 768px) and (max-width: 991px) {
div.tocify {
width: 25%;
}
}
@media (max-width: 767px) {
div.tocify {
width: 100%;
max-width: none;
}
}
.tocify ul, .tocify li {
line-height: 20px;
}
.tocify-subheader .tocify-item {
font-size: 0.90em;
padding-left: 25px;
text-indent: 0;
}
.tocify .list-group-item {
border-radius: 0px;
}
</style>
<!-- setup 3col/9col grid for toc_float and main content -->
<div class="row-fluid">
<div class="col-xs-12 col-sm-4 col-md-3">
<div id="TOC" class="tocify">
</div>
</div>
<div class="toc-content col-xs-12 col-sm-8 col-md-9">
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html">CS631 Labs</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>
<a href="slides.html">Slides & Reading</a>
</li>
<li>
<a href="data-vis-resources.html">Resources</a>
</li>
<li>
<a href="https://sakai.ohsu.edu/portal/site/CS-631-1-32176-Sp18">Sakai</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>
<a href="about.html">About</a>
</li>
</ul>
</div><!--/.nav-collapse -->
</div><!--/.container -->
</div><!--/.navbar -->
<div class="fluid-row" id="header">
<div class="btn-group pull-right">
<button type="button" class="btn btn-default btn-xs dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><span>Code</span> <span class="caret"></span></button>
<ul class="dropdown-menu" style="min-width: 50px;">
<li><a id="rmd-show-all-code" href="#">Show All Code</a></li>
<li><a id="rmd-hide-all-code" href="#">Hide All Code</a></li>
</ul>
</div>
<h1 class="title toc-ignore">Lab 01: Nathan’s Hot-Dog Eating Contest</h1>
<h3 class="subtitle"><em>CS631</em></h3>
<h4 class="author"><em>Alison Hill</em></h4>
</div>
<div id="goals-for-lab-01" class="section level1">
<h1><span class="header-section-number">1</span> Goals for Lab 01</h1>
<ul>
<li>Get your feet wet!</li>
<li>Innoculate you against <code>ggplot2</code> errors- we all get them!</li>
<li>Get exposed to the <em>range</em> of things you can do, before we go <strong>deeep</strong>…</li>
<li>Develop your <em>own</em> <strong>personal</strong> preferences for data visualizations!
<ul>
<li>Do you like or hate gridlines?</li>
<li>What fonts do you find pleasant to read?</li>
<li>What kinds of colors do you like?</li>
<li>Are you team <code>theme_gray</code> or <code>theme_bw</code> (or <code>theme_minimal</code>)?</li>
</ul></li>
</ul>
<p>These are important questions, and I want you to develop (well-informed) opinions on these matters! <img src="images/theme-team-tweets.png" /></p>
</div>
<div id="nathans-hot-dog-eating-contest" class="section level1">
<h1><span class="header-section-number">2</span> Nathan’s Hot Dog Eating Contest</h1>
<div class="figure">
<img src="https://i0.wp.com/flowingdata.com/wp-content/uploads/2009/06/hot-dogs1.gif?zoom=2&fit=900%2C423" />
</div>
<p>This includes a reconstruction of <a href="http://flowingdata.com/2009/07/02/whos-going-to-win-nathans-hot-dog-eating-contest/hot-dogs-2/">Nathan Yau’s hot dog contest example</a>, as interpreted by Jackie Wirz, ported into R and <code>ggplot2</code> by Steven Bedrick for a workshop for the <a href="https://ohsulibrary-datascienceinstitute.github.io">OHSU Data Science Institute</a>, and finally adapted by Alison Hill for all you intrepid Data-Viz-onauts!</p>
<p>First, we load our packages:</p>
<pre class="r"><code>library(tidyverse)
library(extrafont)
library(here)</code></pre>
</div>
<div id="read-in-and-wrangle-data" class="section level1">
<h1><span class="header-section-number">3</span> Read in and wrangle data</h1>
<p>Next, we load some data. You can use the following chunk to load it in from a link:</p>
<pre class="r"><code>hot_dogs <- read_csv("http://bit.ly/cs631-hotdog",
col_types = cols(
gender = col_factor(levels = NULL)
))</code></pre>
<p>Or you can save the file at the link to a local CSV file. I did this and saved my file in a folder called <code>data</code>, then built up the file path to the CSV using <code>here</code>:</p>
<pre class="r"><code>hot_dogs <- read_csv(here::here("data", "hot_dog_contest.csv"),
col_types = cols(
gender = col_factor(levels = NULL)
))</code></pre>
<p>Either way you do it, check it out once read in and make sure it looks like this!</p>
<pre class="r"><code>glimpse(hot_dogs)</code></pre>
<pre><code>Observations: 49
Variables: 4
$ year <dbl> 2017, 2017, 2016, 2016, 2015, 2015, 2014, 2014, 2013...
$ gender <fct> male, female, male, female, male, female, male, fema...
$ name <chr> "Joey Chestnut", "Miki Sudo", "Joey Chestnut", "Miki...
$ num_eaten <dbl> 72.000, 41.000, 70.000, 38.000, 62.000, 38.000, 61.0...</code></pre>
<pre class="r"><code>hot_dogs</code></pre>
<pre><code># A tibble: 49 x 4
year gender name num_eaten
<dbl> <fct> <chr> <dbl>
1 2017. male Joey Chestnut 72.0
2 2017. female Miki Sudo 41.0
3 2016. male Joey Chestnut 70.0
4 2016. female Miki Sudo 38.0
5 2015. male Matthew Stonie 62.0
6 2015. female Miki Sudo 38.0
7 2014. male Joey Chestnut 61.0
8 2014. female Miki Sudo 34.0
9 2013. male Joey Chestnut 69.0
10 2013. female Sonya Thomas 36.8
# ... with 39 more rows</code></pre>
<p>We’ll be wanting to somehow include information about whether a given year was before or after the incorporation of the competitive eating league, so let’s add an indicator field to the data using <code>mutate()</code>. Also, the data’s a little sketchy pre-1981 and for our purposes today we’ll be focusing on males only, so let’s do some <code>filter</code>ing too:</p>
<pre class="r"><code>hot_dogs <- hot_dogs %>%
mutate(post_ifoce = year >= 1997) %>%
filter(year >= 1981 & gender == 'male')
hot_dogs</code></pre>
<pre><code># A tibble: 37 x 5
year gender name num_eaten post_ifoce
<dbl> <fct> <chr> <dbl> <lgl>
1 2017. male Joey Chestnut 72. TRUE
2 2016. male Joey Chestnut 70. TRUE
3 2015. male Matthew Stonie 62. TRUE
4 2014. male Joey Chestnut 61. TRUE
5 2013. male Joey Chestnut 69. TRUE
6 2012. male Joey Chestnut 68. TRUE
7 2011. male Joey Chestnut 62. TRUE
8 2010. male Joey Chestnut 54. TRUE
9 2009. male Joey Chestnut 68. TRUE
10 2008. male Joey Chestnut 59. TRUE
# ... with 27 more rows</code></pre>
</div>
<div id="plot-the-data" class="section level1">
<h1><span class="header-section-number">4</span> Plot The Data</h1>
<p>Now let’s try making a first crack at a sketchy plot:</p>
<pre class="r"><code>ggplot(hot_dogs, aes(x = year, y = num_eaten)) +
geom_col()</code></pre>
<p><img src="01-eda_hot_dogs_files/figure-html/unnamed-chunk-6-1.png" width="672" /></p>
<p>Note that our data is already in “counted” form, so we’re using <code>geom_col()</code> instead of <code>geom_bar()</code>.</p>
</div>
<div id="add-axis-labels-and-title" class="section level1">
<h1><span class="header-section-number">5</span> Add Axis Labels And Title</h1>
<pre class="r"><code>ggplot(hot_dogs, aes(x = year, y = num_eaten)) +
geom_col() +
labs(x = "Year", y = "Hot Dogs and Buns Consumed") +
ggtitle("Nathan's Hot Dog Eating Contest Results, 1981-2017")</code></pre>
<p><img src="01-eda_hot_dogs_files/figure-html/unnamed-chunk-7-1.png" width="672" /></p>
</div>
<div id="play-with-colors" class="section level1">
<h1><span class="header-section-number">6</span> Play With Colors</h1>
<div class="panel panel-success">
<div class="panel-heading">
Challenge #1:
</div>
<div class="panel-body">
<p>Make 3 versions of the last plot we just made:</p>
<ul>
<li><strong>In the first,</strong> make all the columns outlined in “white”.</li>
<li><strong>In the second,</strong> make all the columns outlined in “white” and filled in “navyblue”.</li>
<li><strong>In the third,</strong> make all the columns outlined in “white” and filled in according to whether or not <code>post_ifoce</code> is TRUE or FALSE (use default colors for now).
</div></li>
</ul>
</div>
<pre class="r"><code>ggplot(hot_dogs, aes(x = year, y = num_eaten)) +
geom_col(colour = "white") +
labs(x = "Year", y = "Hot Dogs and Buns Consumed") +
ggtitle("Nathan's Hot Dog Eating Contest Results, 1981-2017")</code></pre>
<p><img src="01-eda_hot_dogs_files/figure-html/unnamed-chunk-8-1.png" width="672" /></p>
<pre class="r"><code>ggplot(hot_dogs, aes(x = year, y = num_eaten)) +
geom_col(colour = "white", fill = "navyblue") +
labs(x = "Year", y = "Hot Dogs and Buns Consumed") +
ggtitle("Nathan's Hot Dog Eating Contest Results, 1981-2017")</code></pre>
<p><img src="01-eda_hot_dogs_files/figure-html/unnamed-chunk-9-1.png" width="672" /></p>
<pre class="r"><code>ggplot(hot_dogs, aes(x = year, y = num_eaten)) +
geom_col(aes(fill = post_ifoce), colour = "white") +
labs(x = "Year", y = "Hot Dogs and Buns Consumed") +
ggtitle("Nathan's Hot Dog Eating Contest Results, 1981-2017")</code></pre>
<p><img src="01-eda_hot_dogs_files/figure-html/unnamed-chunk-10-1.png" width="672" /></p>
<div class="panel panel-success">
<div class="panel-heading">
Challenge #2:
</div>
<div class="panel-body">
<p>What if you want to change the legend in the last plot you made? Use google to figure out how to do the following:</p>
<ul>
<li>Delete the legend title</li>
<li>Make the legend text either “Post-IFOCE” or “Pre-IFOCE”.
</div></li>
</ul>
</div>
<pre class="r"><code>ggplot(hot_dogs, aes(x = year, y = num_eaten)) +
geom_col(aes(fill = post_ifoce), colour = "white") +
labs(x = "Year", y = "Hot Dogs and Buns Consumed") +
ggtitle("Nathan's Hot Dog Eating Contest Results, 1981-2017") +
scale_fill_discrete(name = "",
labels=c("Pre-IFOCE", "Post-IFOCE"))</code></pre>
<p><img src="01-eda_hot_dogs_files/figure-html/unnamed-chunk-11-1.png" width="672" /></p>
</div>
<div id="change-the-dataset" class="section level1">
<h1><span class="header-section-number">7</span> Change The Dataset</h1>
<p>Now, let’s change the question a little bit. This looks at the <em>creation</em> of the IFOCE. What about the <em>affiliation</em> of the contestants? We’ll need some different data for this. Through the <em>Magic Of Data Science™</em>, we have dug that information up and put it into an expanded version of our CSV file available at <a href="http://bit.ly/cs631-hotdog-affiliated" class="uri">http://bit.ly/cs631-hotdog-affiliated</a>.</p>
<div class="panel panel-success">
<div class="panel-heading">
Challenge #3:
</div>
<div class="panel-body">
<p>Let’s work with this new dataset! Do the following:</p>
<ul>
<li>Read in the “hot_dog_contest_with_affiliation.csv” data file, using <code>col_types</code> to read in <code>affiliated</code> and <code>gender</code> as factors.</li>
<li>Within a <code>mutate</code>, create a new variable called <code>post_ifoce</code> that is TRUE if <code>year</code> is greater than or equal to 1997.</li>
<li>Also <code>filter</code> the new data for only years 1981 and after, and only for male competitors.
</div></li>
</ul>
</div>
<pre class="r"><code>hdm_affil <- read_csv("http://bit.ly/cs631-hotdog-affiliated",
col_types = cols(
affiliated = col_factor(levels = NULL),
gender = col_factor(levels = NULL)
)) %>%
mutate(post_ifoce = year >= 1997) %>%
filter(year >= 1981 & gender == "male") </code></pre>
<pre class="r"><code>hdm_affil <- read_csv(here::here("data", "hot_dog_contest_with_affiliation.csv"),
col_types = cols(
affiliated = col_factor(levels = NULL),
gender = col_factor(levels = NULL)
)) %>%
mutate(post_ifoce = year >= 1997) %>%
filter(year >= 1981 & gender == "male")
glimpse(hdm_affil)</code></pre>
<pre><code>Observations: 37
Variables: 6
$ year <dbl> 2017, 2016, 2015, 2014, 2013, 2012, 2011, 2010, 200...
$ gender <fct> male, male, male, male, male, male, male, male, mal...
$ name <chr> "Joey Chestnut", "Joey Chestnut", "Matthew Stonie",...
$ num_eaten <dbl> 72.000, 70.000, 62.000, 61.000, 69.000, 68.000, 62....
$ affiliated <fct> current, current, current, current, current, curren...
$ post_ifoce <lgl> TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRU...</code></pre>
<div class="panel panel-success">
<div class="panel-heading">
Challenge #4:
</div>
<div class="panel-body">
<p>Let’s do some basic EDA with this new dataset! Do the following:</p>
<ul>
<li>Use <code>dplyr::distinct</code> to figure out how many unique values there are of <code>affiliated</code>.</li>
<li>Use <code>dplyr::count</code> to count the number of rows for each unique value of <code>affiliated</code>; use <code>?count</code> to figure out how to sort the counts in descending order.
</div></li>
</ul>
</div>
<pre class="r"><code>hdm_affil %>%
distinct(affiliated)</code></pre>
<pre><code># A tibble: 3 x 1
affiliated
<fct>
1 current
2 former
3 not affiliated</code></pre>
<pre class="r"><code>hdm_affil %>%
count(affiliated, sort = TRUE)</code></pre>
<pre><code># A tibble: 3 x 2
affiliated n
<fct> <int>
1 not affiliated 20
2 current 11
3 former 6</code></pre>
<p>Now let’s plot this new data, and fill the columns according to our new <code>affiliated</code> column.</p>
<pre class="r"><code>ggplot(hdm_affil, aes(x = year, y = num_eaten)) +
geom_col(aes(fill = affiliated)) +
labs(x = "Year", y = "Hot Dogs and Buns Consumed") +
ggtitle("Nathan's Hot Dog Eating Contest Results, 1981-2017")</code></pre>
<p><img src="01-eda_hot_dogs_files/figure-html/unnamed-chunk-15-1.png" width="672" /></p>
<div class="panel panel-success">
<div class="panel-heading">
Challenge #5:
</div>
<div class="panel-body">
<p>Do the following updates to the last plot we just made:</p>
<ul>
<li>Update the colors using hex colors: <code>c('#E9602B','#2277A0','#CCB683')</code>.</li>
<li>Change the legend title to “IFOCE-affiliation”.</li>
<li>Save this plot object as “affil_plot”.
</div></li>
</ul>
</div>
<pre class="r"><code>affil_plot <- ggplot(hdm_affil, aes(x = year, y = num_eaten)) +
geom_col(aes(fill = affiliated)) +
labs(x = "Year", y = "Hot Dogs and Buns Consumed") +
ggtitle("Nathan's Hot Dog Eating Contest Results, 1981-2017") +
scale_fill_manual(values = c('#E9602B','#2277A0','#CCB683'),
name = "IFOCE-affiliation")
affil_plot</code></pre>
<p><img src="01-eda_hot_dogs_files/figure-html/unnamed-chunk-16-1.png" width="672" /></p>
</div>
<div id="play-with-scales-coordinates" class="section level1">
<h1><span class="header-section-number">8</span> Play With Scales & Coordinates</h1>
<p>The spacing’s a little funky down near the origin of the plot. The <a href="http://ggplot2.tidyverse.org/reference/scale_continuous.html">documentation</a> tells us that the defaults are <code>c(0.05, 0)</code> for continuous variables. The first number is multiplicative and the second is additive.</p>
<p>The default was that 1.8 ((2017-1981)*.05+0) was added to the right and left sides of the x-axis as padding, so the effective default limits were <code>c(1979, 2019)</code>.</p>
<p>Let’s tighten that up with the <code>expand</code> property for the <code>scale_y_continuous</code> (we’ll also change the breaks for y-axis tick marks here) and <code>scale_x_continuous</code> settings:</p>
<pre class="r"><code>affil_plot <- affil_plot +
scale_y_continuous(expand = c(0, 0),
breaks = seq(0, 70, 10)) +
scale_x_continuous(expand = c(0, 0))
affil_plot</code></pre>
<p><img src="01-eda_hot_dogs_files/figure-html/unnamed-chunk-17-1.png" width="672" /></p>
<p>But now the plot looks like it is wearing tight pants.</p>
<div class="figure">
<img src="https://media.giphy.com/media/xT1XH07no2wZSq4mnm/giphy.gif" />
</div>
<p>Let’s loosen things up a bit by updating the plot coordinates.</p>
<div class="panel panel-success">
<div class="panel-heading">
Challenge #6:
</div>
<div class="panel-body">
<p>Use <code>coord_cartesian</code> to:</p>
<ul>
<li>Set the x-axis range to 1980-2018</li>
<li>Set the y-axis range to 0-80
</div></li>
</ul>
</div>
<p>Using <code>coord_cartesian</code> is the preferred layer here because “setting limits on the coordinate system will zoom the plot (like you’re looking at it with a magnifying glass), and will not change the underlying data like setting <code>limits</code> on a scale will.”</p>
<div class="panel panel-info">
<div class="panel-heading">
Lesson:
</div>
<div class="panel-body">
Don’t change <code>limits</code> unless you really know what you are doing! Most of the time, you want to change the coordinates instead.
</div>
</div>
<pre class="r"><code>affil_plot <- affil_plot +
coord_cartesian(xlim = c(1980, 2018), ylim = c(0, 80))
affil_plot</code></pre>
<p><img src="01-eda_hot_dogs_files/figure-html/unnamed-chunk-18-1.png" width="672" /></p>
</div>
<div id="play-with-theme-settings" class="section level1">
<h1><span class="header-section-number">9</span> Play With Theme Settings</h1>
<p>Let’s change some key theme settings:</p>
<pre class="r"><code>affil_plot +
theme(plot.title = element_text(hjust = 0.5)) +
theme(axis.text = element_text(size = 12)) +
theme(panel.background = element_blank()) +
theme(axis.line.x = element_line(color = "gray80", size = 0.5)) +
theme(axis.ticks = element_line(color = "gray80", size = 0.5))</code></pre>
<p><img src="01-eda_hot_dogs_files/figure-html/unnamed-chunk-19-1.png" width="672" /></p>
<div class="panel panel-info">
<div class="panel-heading">
Lesson:
</div>
<div class="panel-body">
You can change <em>almost anything</em> that your heart desires to change!
</div>
</div>
<p>By default, plot titles in <code>ggplot2</code> are left-aligned. For <code>hjust</code>:</p>
<ul>
<li><code>0</code> == left</li>
<li><code>0.5</code> == centered</li>
<li><code>1</code> == right</li>
</ul>
<p>We could also save all these as a custom theme. We are not fans of the default font, so we are also going to change this. To do this, you need to install the (<code>extrafont</code> package)[<a href="https://github.com/wch/extrafont" class="uri">https://github.com/wch/extrafont</a>] and follow its setup instructions before doing this next step.</p>
<pre class="r"><code>hot_diggity <- theme(plot.title = element_text(hjust = 0.5),
axis.text = element_text(size = 12),
panel.background = element_blank(),
axis.line.x = element_line(color = "gray80", size = 0.5),
axis.ticks = element_line(color = "gray80", size = 0.5),
text = element_text(family = "Lato") # need extrafont for this
)</code></pre>
<pre class="r"><code>affil_plot + hot_diggity </code></pre>
<p><img src="01-eda_hot_dogs_files/figure-html/unnamed-chunk-21-1.png" width="672" /></p>
<p>We could also use someone else’s theme:</p>
<pre class="r"><code>library(ggthemes)
affil_plot + theme_fivethirtyeight(base_family = "Lato")</code></pre>
<p><img src="01-eda_hot_dogs_files/figure-html/unnamed-chunk-22-1.png" width="672" /></p>
<pre class="r"><code>affil_plot + theme_tufte(base_family = "Palatino")</code></pre>
<p><img src="01-eda_hot_dogs_files/figure-html/unnamed-chunk-22-2.png" width="672" /></p>
<p>The final thing we have to mess with is the x-axis ticks and labels. We’ll do this in two steps, then override our previous layer <code>scale_x_continuous</code>.</p>
<pre class="r"><code>years_to_label <- seq(from = 1981, to = 2017, by = 4)
years_to_label</code></pre>
<pre><code> [1] 1981 1985 1989 1993 1997 2001 2005 2009 2013 2017</code></pre>
<pre class="r"><code>hd_years <- hdm_affil %>%
distinct(year) %>%
mutate(year_lab = ifelse(year %in% years_to_label, year, ""))</code></pre>
<pre class="r"><code>affil_plot +
hot_diggity +
scale_x_continuous(expand = c(0, 0),
breaks = hd_years$year,
labels = hd_years$year_lab)</code></pre>
<pre><code>Scale for 'x' is already present. Adding another scale for 'x', which
will replace the existing scale.</code></pre>
<p><img src="01-eda_hot_dogs_files/figure-html/unnamed-chunk-24-1.png" width="672" /></p>
</div>
<div id="final-final-final-version" class="section level1">
<h1><span class="header-section-number">10</span> Final (final, final) version</h1>
<p>Don’t name your files “final” :)</p>
<div class="figure">
<img src="http://www.phdcomics.com/comics/archive/phd101212s.gif" />
</div>
<p>All together in one chunk, here is our final (for now) plot! I’m also adding some additional elements here to show you options:</p>
<pre class="r"><code>nathan_plot <- ggplot(hdm_affil, aes(x = year, y = num_eaten)) +
geom_col(aes(fill = affiliated)) +
labs(x = "Year", y = "Hot Dogs and Buns Consumed") +
ggtitle("Nathan's Hot Dog Eating Contest Results, 1981-2017") +
scale_fill_manual(values = c('#E9602B','#2277A0','#CCB683'),
name = "IFOCE-affiliation") +
hot_diggity +
scale_y_continuous(expand = c(0, 0),
breaks = seq(0, 70, 10)) +
scale_x_continuous(expand = c(0, 0),
breaks = hd_years$year,
labels = hd_years$year_lab) +
coord_cartesian(xlim = c(1980, 2018), ylim = c(0, 80))
nathan_plot</code></pre>
<p><img src="01-eda_hot_dogs_files/figure-html/unnamed-chunk-25-1.png" width="672" /></p>
<p>Adding some plot annotations rather than having a fill legend:</p>
<pre class="r"><code>nathan_ann <- nathan_plot +
guides(fill = FALSE) +
coord_cartesian(xlim = c(1980, 2019), ylim = c(0, 85)) +
annotate('segment', x=1980.75, xend=2000.25, y= 30, yend=30, size=0.5, color="#CCB683") +
annotate('segment', x=1980.75, xend=1980.75, y= 30, yend=28, size=0.5, color="#CCB683") +
annotate('segment', x=2000.25, xend=2000.25, y= 30, yend=28, size=0.5, color="#CCB683") +
annotate('segment', x=1990, xend=1990, y= 33, yend=30, size=0.5, color="#CCB683") +
annotate('text', x=1990, y=36, label="No MLE/IFOCE Affiliation", color="#CCB683", family="Lato", hjust=0.5, size = 3) +
annotate('segment', x=2000.75, xend=2006.25, y= 58, yend=58, size=0.5, color="#2277A0") +
annotate('segment', x=2000.75, xend=2000.75, y= 58, yend=56, size=0.5, color="#2277A0") +
annotate('segment', x=2006.25, xend=2006.25, y= 58, yend=56, size=0.5, color="#2277A0") +
annotate('segment', x=2003.5, xend=2003.5, y= 61, yend=58, size=0.5, color="#2277A0") +
annotate('text', x=2003.5, y=65, label="MLE/IFOCE\nFormer Member", color="#2277A0", family="Lato", hjust=0.5, size = 3) +
annotate('segment', x=2006.75, xend=2017.25, y= 76, yend=76, size=0.5, color="#E9602B") +
annotate('segment', x=2006.75, xend=2006.75, y= 76, yend=74, size=0.5, color="#E9602B") +
annotate('segment', x=2017.25, xend=2017.25, y= 76, yend=74, size=0.5, color="#E9602B") +
annotate('segment', x=2012, xend=2012, y= 79, yend=76, size=0.5, color="#E9602B") +
annotate('text', x=2012, y=82, label="MLE/IFOCE Current Member", color="#E9602B", family="Lato", hjust=0.5, size = 3) </code></pre>
<pre><code>Coordinate system already present. Adding new coordinate system, which will replace the existing one.</code></pre>
<pre class="r"><code>nathan_ann</code></pre>
<p><img src="01-eda_hot_dogs_files/figure-html/unnamed-chunk-26-1.png" width="672" /></p>
<p>Finally, adding in another layer of data from female contestants:</p>
<pre class="r"><code>hdm_females <- read_csv(here::here("data", "hot_dog_contest_with_affiliation.csv"),
col_types = cols(
affiliated = col_factor(levels = NULL),
gender = col_factor(levels = NULL)
)) %>%
mutate(post_ifoce = year >= 1997) %>%
filter(year >= 1981 & gender == "female")
glimpse(hdm_females)</code></pre>
<pre><code>Observations: 7
Variables: 6
$ year <dbl> 2017, 2016, 2015, 2014, 2013, 2012, 2011
$ gender <fct> female, female, female, female, female, female, female
$ name <chr> "Miki Sudo", "Miki Sudo", "Miki Sudo", "Miki Sudo",...
$ num_eaten <dbl> 41.00, 38.00, 38.00, 34.00, 36.75, 45.00, 40.00
$ affiliated <fct> current, current, current, current, current, curren...
$ post_ifoce <lgl> TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE</code></pre>
<pre class="r"><code>nathan_w_females <- nathan_ann +
# add in the female data, and manually set a fill color
geom_col(data = hdm_females,
width = 0.75,
fill = "#F68A39")
nathan_w_females</code></pre>
<p><img src="01-eda_hot_dogs_files/figure-html/unnamed-chunk-28-1.png" width="672" /></p>
<p>And adding a final caption:</p>
<pre class="r"><code>caption <- paste(strwrap("* From 2011 on, separate Men's and Women's prizes have been awarded. All female champions to date have been MLE/IFOCE-affiliated.", 70), collapse="\n")
nathan_w_females +
# now an asterisk to set off the female scores, and a caption
annotate('text', x = 2018.5, y = 39, label="*", family = "Lato", size = 8) +
labs(caption = caption) +
theme(plot.caption = element_text(family = "Lato", size=8, hjust=0, margin=margin(t=15)))</code></pre>
<p><img src="01-eda_hot_dogs_files/figure-html/unnamed-chunk-29-1.png" width="672" /></p>
</div>
<p>
<a rel="license" href="http://creativecommons.org/licenses/by-nc/4.0/">
<img alt="Creative Commons License" style="border-width:0" src="by-nc.png" height="400" width="65"/></a>
</p>
</div>
</div>
</div>
<script>
// add bootstrap table styles to pandoc tables
function bootstrapStylePandocTables() {
$('tr.header').parent('thead').parent('table').addClass('table table-condensed');
}
$(document).ready(function () {
bootstrapStylePandocTables();
});
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>