-
Notifications
You must be signed in to change notification settings - Fork 1
/
US_TrackandField_WeiHuaHsu.Rmd
1077 lines (923 loc) · 49.3 KB
/
US_TrackandField_WeiHuaHsu.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: |
| U.S. High School Track & Field
| and Cross-Country Data Analysis
subtitle: "Data Science 2019 Fall Final Project"
author: "Wei-Hua Hsu (Wafer)"
date: "`r Sys.Date()`"
output:
pdf_document: default
header-includes:
- \newcommand{\bcenter}{\begin{center}}
- \newcommand{\ecenter}{\end{center}}
geometry: margin = 0.5in
params:
solutions: yes
fontsize: 12pt
urlcolor: blue
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,
fig.height = 4.8,
fig.width = 7.8,
fig.align = "center")
# -- General --
library(tidyverse)
library(purrr) # map function
library(ggplot2) # visualization
library(ggthemes) # set theme
theme_set(theme_bw())
library(ggpubr) # arrange plots side-by-side
# -- Data Driven --
library(xml2) # read html
library(rvest) # read html table
library(readxl) # read excel file
library(lubridate) # parse date
# -- Other --
library(usmap) # visualize the US map
```
\newpage
### TO DO LIST
- single match for athletes, less oxgen
- training in CO/arizona, adapt the body, getting more efficient (blood thick/more oxgen)
- 10 years
- shiny.app <8000/ <6000 filter income (EFC table/median)
- recruiting process
- predictive modeling (postal code)
- 1) value 2) good
-- separate things: 1) predictive model 2008-2018, 2) economy
- year at the left (shinyapp)
## Introduction
In the United States, sports is an important part of American culture. American football is the most popular sport to watch, but running, jogging, and trail running is the most popular exercise people practice daily. Between the U.S. high schools, there are thousands of Track & Field and Cross Country competitions held within colleges and high schools every year. For those students who are dedicated to sports, the performance list is crucial since they would go to college depending on their performance records. However, in the view of colleges, they prefer to recruit new students who are from a middle class or lower income family. In this way the college can save budgets by paying their athletes through the sponsorship from the U.S. government. Under this premise, this study aims to find out which states tend to have more elite athletes. In addition to personal training and genetic strength, many studies indicate the environmental factors can have a significant effect on athlete’s performance. Therefore, I plan to explore the relationship between player performance and climate factors (rainfall, temperature, sun hours, etc.) among states, and then apply the possible results to statewide family household income.
## Data Collecting
This project will include four main aspects of data:
1. The **information of the U.S.**: 50 states and District of Columbia, with state's latitude and longitude.
2. The performance list of **top 500 high school athletes**: the top 500 U.S. high school athletes in the sports of Track & Field and Cross Country (XC) in 2018. The datasets are separated by gender, there are three different events 800m, 1600m, 3200m for indoor/outdoor Track & Field (ITF and OTF), and the XC comes with 5k performance list. The athlete's hometown and the time he/she finished the race are included.
| Indoor Track & Field | Outdoor Track & Field | Cross Country |
|:---------------------|:----------------------|:--------------|
| boys_800m | boys_800m | boys_XC_5k |
| girls_800m | boys_800m | girls_XC_5k |
| boys_1600m | boys_1600m | |
| girls_1600m | girls_1600m | |
| boys_1600m | boys_3200m | |
| girls_1600m | girls_3200m | |
3. **U.S. Climate data**: all statewide climate data are classified by year, including temperature, rainfall inches, humidity, sunshine hours, wind speed, and elevations.
| Climate Data |
|:---------------------------------|
| average *temperature* |
| average *precipitation* |
| morning and afternoon *humidity* |
| annual *sunshine hours* |
| average *wind speed* |
| highest/lowest *elevations* |
4. **U.S. Social Economy data**: median household income by state (2013-2017).
## Study Questions
**1.** What does the statewide player performance looks like? Are there some states tend to have better performance?
**2.** Does climate matter? How does the climate factors (temperature, rainfall inches, humidity, sunshine hours, wind speed, elevations) relate to elite athletes in the U.S.?
**3.** How wealthy are those outstanding athletes? Is there any lower-income state that tends to have a better performance of athletes?
## Overview of results
1. The indoor/outdoor Track & Field list shows different relationship between athletes with different states.
2. People from northeastern, west coast (WA, CA), and southern (TX, FL) of the United States engage more in sports, and the western inland states of Colorado and Utah are second popular with sports.
2. Climate and natural conditions do affect players' performance in a certain aspect.
3. Female athletes are more spread out between states than male athletes.
4. Natural phenomenons such as the population from each state need to be taken into consideration.
5. The Cross Country results are evenly distributed.
## Data import, cleaning, and tidying
### 1. State information in the U.S.
- I found a data file with each state's information. But I do not need "medals" info and this data miss West Virginia and South Dakota, I tidy and add more info on it.
```{r Import_state info}
# import
state_info <- read_csv("./data/state_info.csv")
# tidy
state_info <- state_info %>%
select(-medals) %>%
bind_rows(list(state = c("WV", "SD"),
location = c("West Virginia", "South Dakota"),
lat = c(39.000000, 44.500000),
lon = c(-80.500000, -100.000000)))
# dataset - state_info
```
```{r, include=FALSE}
head(state_info, 10)
```
- To draw a U.S. map, I found an useful package "usmap", this dataset has a more detailed position information, and it helps me with graphing the map.
```{r Import_usmap}
library(usmap)
# tidy & save
usmap <- map_data("state") %>%
select(1, 2, 3, 5)
# dataset - usmap
```
```{r, include=FALSE}
head(usmap, 10)
```
### 2. The performance list of top 500 high school athletes
- **Function**: Since the format of high school data are very similar and those are embedded in an excel file. I write 2 functions to tidy the data, one is for tiding the data, the other is to convert the time format to seconds. Then applying `map()` function on the data.
```{r Fun1_tidy High School}
## function1 - tidy High School data
tidy_hs <- function(x) {
x %>%
slice(which(row_number() %% 2 == 0)) %>%
mutate(state = str_extract(`ATHLETE/TEAM`, "(^(?i)[a-z][a-z])")) %>%
separate(`ATHLETE/TEAM`, into = c("Empty", "Team"), sep = "(^(?i)[a-z][a-z])") %>%
select(8, 5) %>%
mutate(state = str_to_upper(.$state)) %>%
# column bind all the second row of data
cbind(
x %>%
slice(which(row_number() %% 2 == 1)) %>%
select(-3) %>%
set_names(c("Rank", "Time", "Athlete", "Grade", "Meet/Place")) %>%
mutate(Place = str_extract(`Meet/Place`, "(\\d[a-z][a-z])$")) %>%
mutate(`Meet/Place` = str_replace(`Meet/Place`, "(\\d[a-z][a-z])$", ""))
) %>%
set_names(c("state", "Team", "Rank", "Time",
"Athlete", "Grade", "Meet", "Place")) %>%
select(3, 5, 4, 1, 2, 6, 7, 8) %>%
# join the state info
left_join(state_info, by = "state")
}
```
```{r Fun2_convert to second}
## function2 - convert time to second
count_sec <- function(x) {
x <- x %>%
separate(Time, into = c("minute", "second"), sep = ":")
# set the time variable as numeric
x$minute <- as.numeric(x$minute)
x$second <- as.numeric(x$second)
# calculate the time variable
x <- x %>%
mutate(Time = (minute * 60) + second) %>%
select(1, 2, 13, 5, 10:12, 6:9)
return(x)
}
```
- **Data import**: There are three data files for top 500 high school athletes: 1) indoor Track & Field (*hs_indoor18*); 2) outdoor Track & Field (*hs_outdoor18*); 3) Cross Country (*hs_xc18*). These are high school ranking results with national meeting events in the year of 2018.
*Since the codes are basically the same, the code is shown on .pdf file only includes `hs_indoor18`*
```{r Import_HS_ITF}
hs_indoor18 <- "./data/HS_indoor18.xlsx"
# read excel
hs_indoor18 <- hs_indoor18 %>%
excel_sheets() %>%
purrr::set_names() %>%
map(read_excel, path = hs_indoor18)
# tidy & convert the time format
hs_indoor18 <- map(hs_indoor18, tidy_hs)
hs_indoor18 <- map(hs_indoor18, count_sec)
# not sure why but this variable couldn't change to numeric automatically
hs_indoor18$boys_1600m$Rank <- as.numeric(hs_indoor18$boys_1600m$Rank)
```
```{r Import_HS_OTF, echo=FALSE}
hs_outdoor18 <- "./data/HS_outdoor18.xlsx"
# read excel
hs_outdoor18 <- hs_outdoor18 %>%
excel_sheets() %>%
purrr::set_names() %>%
map(read_excel, path = hs_outdoor18)
# tidy & convert the time format
hs_outdoor18 <- map(hs_outdoor18, tidy_hs)
hs_outdoor18 <- map(hs_outdoor18, count_sec)
```
```{r Import_HS_XC, echo=FALSE}
hs_xc18 <- "./data/HS_XC18.xlsx"
# read excel
hs_xc18 <- hs_xc18 %>%
excel_sheets() %>%
purrr::set_names() %>%
map(read_excel, path = hs_xc18)
# tidy & convert the time format
hs_xc18 <- map(hs_xc18, tidy_hs)
hs_xc18 <- map(hs_xc18, count_sec)
```
- Take a glance at the high school performance list, the format of the rest of datasets are basically the same.
```{r}
head(hs_indoor18$boys_800m, 10)
head(hs_outdoor18$girls_800m, 10)
```
### 3. The U.S. Climate data
- For climate data, I do a lot of web scraping as follow.
- Average annual temperature by state
```{r Import_temperature}
# webscraping
temp_url <- read_html("https://www.currentresults.com/Weather/US/average-annual-state-temperatures.php")
temperature <- html_table(temp_url, fill = T)
# tidy & join state_info
temperature <- rbind(temperature[[1]], temperature[[2]], temperature[[3]]) %>%
set_names(c("location", "avg_F", "avg_C", "Rank")) %>%
left_join(state_info, by = "location")
# dataset - temperature
```
```{r, include=FALSE}
head(temperature, 10)
```
- Average annual precipitation by state
```{r Import_rainfall}
# webscraping
rain_url <- read_html("https://www.currentresults.com/Weather/US/average-annual-state-precipitation.php")
rainfall <- html_table(rain_url, fill = T)
# tidy & join state_info
rainfall <- rbind(rainfall[[1]], rainfall[[2]], rainfall[[3]]) %>%
set_names(c("location", "Inches", "Millimeters", "Rank")) %>%
left_join(state_info, by = "location")
# dataset - rainfall
```
```{r, include=FALSE}
head(rainfall, 10)
```
- Average annual morning and afternoon humidity (%) by states: Since the row with "Connecticut" and "Massachusetts" contain an unreadable UTF-8 signs, I duplicate the info and delete the former one.
```{r Import_humidity}
# webscraping
humid_url <-
read_html("https://www.currentresults.com/Weather/US/annual-average-humidity-by-state.php")
humidity <- html_table(humid_url, fill = T)
# tidy & join state_info
humidity <- rbind(humidity[[1]], humidity[[2]], humidity[[3]]) %>%
set_names(c("location", "place", "morning", "afternoon")) %>%
# fix the imput in the row with "location = Connecticut"
filter(place != "Hartford" & place != "Boston") %>%
bind_rows(list(location = c("Connecticut", "Massachusetts"),
place = c("Hartford", "Boston"),
morning = c(79, 75),
afternoon = c(52, 59))
) %>%
# join state_info
left_join(state_info, by = "location")
# dataset - humidity
```
```{r, include=FALSE}
head(humidity, 10)
```
- Average annual sunshine hours by states
```{r Import_sun hours, warning=FALSE}
# webscraping
sun_url <-
read_html("https://www.currentresults.com/Weather/US/average-annual-state-sunshine.php")
sunshine <- html_table(sun_url, fill = T)
# tidy & join state_info
sunshine <- rbind(sunshine[[1]], sunshine[[2]], sunshine[[3]]) %>%
mutate(location = State) %>%
select(6, everything(), -1) %>%
mutate(`% Sun` = as.integer(`% Sun`)) %>%
mutate(`Total Hours` = as.integer(`Total Hours`)) %>%
left_join(state_info, by = "location")
# dataset - sunshine
```
```{r, include=FALSE}
head(sunshine, 10)
```
- Average Wind Speed by states (with the U.S. Population data): Because the `comma` and the `dash` are read as a "character", it needs more code to change the type of continuous variable.
```{r Import_wind speed/population}
# webscraping
windsp_url <-
read_html("http://www.usa.com/rank/us--average-wind-speed--state-rank.htm")
windspeed <- html_table(windsp_url, fill = T)[[2]]
# tidy & join state_info
windspeed <- windspeed %>%
set_names(c("Rank", "avg_WindSpeed", "location / Population")) %>%
slice(2:nrow(windspeed)) %>%
separate("location / Population", into = c("location", "Population"), sep = " / ") %>%
mutate(Rank = str_replace_all(.$Rank, "\\D", "")) %>%
mutate(avg_WindSpeed = str_extract(.$avg_WindSpeed, "\\d\\d.\\d\\d")) %>%
mutate(Population = str_replace_all(.$Population, "\\D", "")) %>%
mutate(avg_WindSpeed = as.numeric(avg_WindSpeed)) %>%
mutate(Population = as.numeric(Population)) %>%
mutate(Rank = as.numeric(Rank)) %>%
left_join(state_info, by = c("location"))
# dataset - windspeed
```
```{r, include=FALSE}
head(windspeed, 10)
```
- Elevations by states: Because the `comma` and the `dash` are read as a "character", it needs more code to change the type of continuous variable.
```{r Import_elevations}
# webscraping
elev_url <-
read_html("https://www.infoplease.com/world/united-states-geography/highest-lowest-and-mean-elevations-united-states")
elevation <- html_table(elev_url, fill = T)[[1]]
# tidy & join state_info
elevation <- elevation %>%
set_names(c("location", "avg_Elevation",
"Highest Point", "Highest Elevation",
"Lowest Point", "Lowest Elevation")) %>%
mutate(avg_Elevation = str_replace_all(.$avg_Elevation, ",", "")) %>%
mutate(avg_Elevation = as.numeric(avg_Elevation)) %>%
mutate(`Highest Elevation` = str_replace_all(.$`Highest Elevation`, ",", "")) %>%
mutate(`Highest Elevation` = as.numeric(`Highest Elevation`)) %>%
mutate(`Lowest Elevation` = str_replace_all(.$`Lowest Elevation`, "Sea level", "0")) %>%
mutate(`Lowest Elevation` = str_replace_all(.$`Lowest Elevation`, ",", "")) %>%
mutate(`Lowest Elevation` = str_replace_all(.$`Lowest Elevation`, "\\D", "-")) %>%
mutate(`Lowest Elevation` = as.numeric(`Lowest Elevation`)) %>%
mutate(location = str_replace(.$location, "D.C.", "District of Columbia")) %>%
left_join(state_info, by = c("location")) %>%
na.omit()
# dataset - elevation
```
```{r, include=FALSE}
head(elevation, 10)
```
\newpage
### 4. The U.S. Social Economy data
- Median Household Income by State (2013-2017)
```{r Import_income, message=FALSE, warning=FALSE}
# read .csv file
median_income <- read_csv("./data/Median_Income.csv")
# tidy
median_income <- median_income %>%
set_names(c("location", "Income", "Margin of Error")) %>%
slice(3:nrow(median_income)-1) %>%
mutate(Income = str_replace_all(.$Income, "\\D", "")) %>%
mutate(Income = as.numeric(Income)) %>%
mutate(`Margin of Error` = str_replace_all(.$`Margin of Error`, "\\D", "")) %>%
mutate(`Margin of Error` = as.numeric(`Margin of Error`)) %>%
left_join(state_info, by = "location")
# dataset - median_income
```
```{r, include=FALSE}
head(median_income, 10)
```
### Question 1
`What does the statewide player performance looks like? Are there some states tend to have better performance?`
- Write a function to get the subset of performance data
```{r Fun3_subset data}
## function3 - subset data with the frequency of state
sub_data <- function(x) {
x %>%
# group by state
group_by(state) %>%
# aggregate the amount of athletes
count() %>%
left_join(x, by = "state") %>%
group_by(state) %>%
# keep the highest ranking of athletes by each state
filter(Rank == min(Rank))
}
sub_otf <- map(hs_outdoor18, sub_data)
sub_itf <- map(hs_indoor18, sub_data)
sub_xc <- map(hs_xc18, sub_data)
# the format of sub_otf / sub_itf / sub_xc are basically the same
# take a glance at one of each
```
```{r, include=FALSE}
head(sub_otf$boys_1600m, 10)
```
- Visualize the amount of athletes from each state, the bigger the "count circle" is, the greater number of athletes' hometowns are. And the color gradient records the best ranking a state's athlete got. The results are separate by different event and gender.
- Indoor Track & Field
```{r Visualization_Q1_ITF, warning=FALSE}
# size = frequency by states
# rank = the best ranking athlete from the state
# ----- Indoor TF 800m ----- #
ggplot(sub_itf$boys_800m, aes(x = lon, y = lat, color = Rank)) +
geom_polygon(data = usmap, aes(x = long, y = lat, group = group),
color = "white", fill = "grey92") +
# label states
ggrepel::geom_label_repel(aes(label = state), data = sub_itf$boys_800m,
size = 3, label.size = 0, segment.color = "orange") +
# point size & ranking
geom_point(aes(size = n, color = Rank, alpha = 0.8)) +
scale_color_continuous("Ranking", low = "#0099FF", high = "red") +
scale_size_continuous("Count", range = c(1, 12)) +
labs(title = "Indoor TF boys (800m) - ranking & frequency") -> itf_boys_800m
ggplot(sub_itf$girls_800m, aes(x = lon, y = lat, color = Rank)) +
geom_polygon(data = usmap, aes(x = long, y = lat, group = group),
color = "white", fill = "grey92") +
# label states
ggrepel::geom_label_repel(aes(label = state), data = sub_itf$girls_800m,
size = 3, label.size = 0, segment.color = "orange") +
# point size & ranking
geom_point(aes(size = n, color = Rank, alpha = 0.8)) +
scale_color_continuous("Ranking", low = "#FFCC00", high = "#3300FF") +
scale_size_continuous("Count", range = c(1, 12)) +
labs(title = "Indoor TF girls (800m) - ranking & frequency") -> itf_girls_800m
```
```{r Visualization_Q1_ITF continued, echo=FALSE}
# ----- Indoor TF 1600m ----- #
ggplot(sub_itf$boys_1600m, aes(x = lon, y = lat, color = Rank)) +
geom_polygon(data = usmap, aes(x = long, y = lat, group = group),
color = "white", fill = "grey92") +
# label states
ggrepel::geom_label_repel(aes(label = state), data = sub_itf$boys_1600m,
size = 3, label.size = 0, segment.color = "orange") +
# point size & ranking
geom_point(aes(size = n, color = Rank, alpha = 0.8)) +
scale_color_continuous("Ranking", low = "#0099FF", high = "red") +
scale_size_continuous("Count", range = c(1, 12)) +
labs(title = "Indoor TF boys (1600m) - ranking & frequency") -> itf_boys_1600m
ggplot(sub_itf$girls_1600m, aes(x = lon, y = lat, color = Rank)) +
geom_polygon(data = usmap, aes(x = long, y = lat, group = group),
color = "white", fill = "grey92") +
# label states
ggrepel::geom_label_repel(aes(label = state), data = sub_itf$girls_1600m,
size = 3, label.size = 0, segment.color = "orange") +
# point size & ranking
geom_point(aes(size = n, color = Rank, alpha = 0.8)) +
scale_color_continuous("Ranking", low = "#FFCC00", high = "#3300FF") +
scale_size_continuous("Count", range = c(1, 12)) +
labs(title = "Indoor TF girls (1600m) - ranking & frequency") -> itf_girls_1600m
# ----- Indoor TF 3200m ----- #
ggplot(sub_itf$boys_3200m, aes(x = lon, y = lat, color = Rank)) +
geom_polygon(data = usmap, aes(x = long, y = lat, group = group),
color = "white", fill = "grey92") +
# label states
ggrepel::geom_label_repel(aes(label = state), data = sub_itf$boys_3200m,
size = 3, label.size = 0, segment.color = "orange") +
# point size & ranking
geom_point(aes(size = n, color = Rank, alpha = 0.8)) +
scale_color_continuous("Ranking", low = "#0099FF", high = "red") +
scale_size_continuous("Count", range = c(1, 12)) +
labs(title = "Indoor TF boys (3200m) - ranking & frequency") -> itf_boys_3200m
ggplot(sub_itf$girls_3200m, aes(x = lon, y = lat, color = Rank)) +
geom_polygon(data = usmap, aes(x = long, y = lat, group = group),
color = "white", fill = "grey92") +
# label states
ggrepel::geom_label_repel(aes(label = state), data = sub_itf$girls_3200m,
size = 3, label.size = 0, segment.color = "orange") +
# point size & ranking
geom_point(aes(size = n, color = Rank, alpha = 0.8)) +
scale_color_continuous("Ranking", low = "#FFCC00", high = "#3300FF") +
scale_size_continuous("Count", range = c(1, 12)) +
labs(title = "Indoor TF girls (3200m) - ranking & frequency") -> itf_girls_3200m
```
As we can see from the indoor results, the higher ranking athletes mostly from northeastern region.
```{r Q1_ITF, echo=FALSE, warning=FALSE, fig.height = 5.2, fig.width = 16, fig.align = "center"}
ggarrange(itf_boys_800m, itf_girls_800m)
ggarrange(itf_boys_1600m, itf_girls_1600m)
ggarrange(itf_boys_3200m, itf_girls_3200m)
```
*Since the codes are basically the same, I am hiding the rest of 1600m/3200m codes on the .pdf file.*
- Outdoor Track & Field
The outdoor results are clearly more spread out, but we can see the west coast states and the southern region states did a better job than the athletes who are from northeastern states.
*The codes are hidden on .pdf file*
```{r Visualization_Q1_OTF, echo=FALSE}
# ----- Outdoor TF 800m ----- #
ggplot(sub_otf$boys_800m, aes(x = lon, y = lat, color = Rank)) +
geom_polygon(data = usmap, aes(x = long, y = lat, group = group),
color = "white", fill = "grey92") +
# label states
ggrepel::geom_label_repel(aes(label = state), data = sub_otf$boys_800m,
size = 3, label.size = 0, segment.color = "orange") +
# point size & ranking
geom_point(aes(size = n, color = Rank, alpha = 0.8)) +
scale_color_continuous("Ranking", low = "#0099FF", high = "red") +
scale_size_continuous("Count", range = c(1, 12)) +
labs(title = "Outdoor TF boys (800m) - ranking & frequency") -> otf_boys_800m
ggplot(sub_otf$girls_800m, aes(x = lon, y = lat, color = Rank)) +
geom_polygon(data = usmap, aes(x = long, y = lat, group = group),
color = "white", fill = "grey92") +
# label states
ggrepel::geom_label_repel(aes(label = state), data = sub_otf$girls_800m,
size = 3, label.size = 0, segment.color = "orange") +
# point size & ranking
geom_point(aes(size = n, color = Rank, alpha = 0.8)) +
scale_color_continuous("Ranking", low = "#FFCC00", high = "#3300FF") +
scale_size_continuous("Count", range = c(1, 12)) +
labs(title = "Outdoor TF girls (800m) - ranking & frequency") -> otf_girls_800m
# ----- Outdoor TF 1600m ----- #
ggplot(sub_otf$boys_1600m, aes(x = lon, y = lat, color = Rank)) +
geom_polygon(data = usmap, aes(x = long, y = lat, group = group),
color = "white", fill = "grey92") +
# label states
ggrepel::geom_label_repel(aes(label = state), data = sub_otf$boys_1600m,
size = 3, label.size = 0, segment.color = "orange") +
# point size & ranking
geom_point(aes(size = n, color = Rank, alpha = 0.8)) +
scale_color_continuous("Ranking", low = "#0099FF", high = "red") +
scale_size_continuous("Count", range = c(1, 12)) +
labs(title = "Outdoor TF boys (1600m) - ranking & frequency") -> otf_boys_1600m
ggplot(sub_otf$girls_1600m, aes(x = lon, y = lat, color = Rank)) +
geom_polygon(data = usmap, aes(x = long, y = lat, group = group),
color = "white", fill = "grey92") +
# label states
ggrepel::geom_label_repel(aes(label = state), data = sub_otf$girls_1600m,
size = 3, label.size = 0, segment.color = "orange") +
# point size & ranking
geom_point(aes(size = n, color = Rank, alpha = 0.8)) +
scale_color_continuous("Ranking", low = "#FFCC00", high = "#3300FF") +
scale_size_continuous("Count", range = c(1, 12)) +
labs(title = "Outdoor TF girls (1600m) - ranking & frequency") -> otf_girls_1600m
# ----- Outdoor TF 3200m ----- #
ggplot(sub_otf$boys_3200m, aes(x = lon, y = lat, color = Rank)) +
geom_polygon(data = usmap, aes(x = long, y = lat, group = group),
color = "white", fill = "grey92") +
# label states
ggrepel::geom_label_repel(aes(label = state), data = sub_otf$boys_3200m,
size = 3, label.size = 0, segment.color = "orange") +
# point size & ranking
geom_point(aes(size = n, color = Rank, alpha = 0.8)) +
scale_color_continuous("Ranking", low = "#0099FF", high = "red") +
scale_size_continuous("Count", range = c(1, 12)) +
labs(title = "Outdoor TF boys (3200m) - ranking & frequency") -> otf_boys_3200m
ggplot(sub_otf$girls_3200m, aes(x = lon, y = lat, color = Rank)) +
geom_polygon(data = usmap, aes(x = long, y = lat, group = group),
color = "white", fill = "grey92") +
# label states
ggrepel::geom_label_repel(aes(label = state), data = sub_otf$girls_3200m,
size = 3, label.size = 0, segment.color = "orange") +
# point size & ranking
geom_point(aes(size = n, color = Rank, alpha = 0.8)) +
scale_color_continuous("Ranking", low = "#FFCC00", high = "#3300FF") +
scale_size_continuous("Count", range = c(1, 12)) +
labs(title = "Outdoor TF girls (3200m) - ranking & frequency") -> otf_girls_3200m
```
```{r Q1_OTF, echo=FALSE, fig.height = 5.2, fig.width = 16, fig.align = "center"}
ggarrange(otf_boys_800m, otf_girls_800m)
ggarrange(otf_boys_1600m, otf_girls_1600m)
ggarrange(otf_boys_3200m, otf_girls_3200m)
```
- Cross Country
For the cross country data, I wouldn't not make a conclusion through this plot. Because the outstanding athletes are scatter around. It needs more research (pull up more years data, take a deeper look at their performance of time spending, etc.) to get a possible insight.
*The codes are hidden on .pdf file*
```{r Visualization_Q1_XC, echo=FALSE}
ggplot(sub_xc$boys_XC_5k, aes(x = lon, y = lat, color = Rank)) +
geom_polygon(data = usmap, aes(x = long, y = lat, group = group),
color = "white", fill = "grey92") +
# label states
ggrepel::geom_label_repel(aes(label = state), data = sub_xc$boys_XC_5k,
size = 3, label.size = 0, segment.color = "orange") +
# point size & ranking
geom_point(aes(size = n, color = Rank, alpha = 0.8)) +
scale_color_continuous("Ranking", low = "#009900", high = "red") +
scale_size_continuous("Count", range = c(1, 12)) +
labs(title = "Boys Cross Country - ranking & frequency") -> xc_boys
ggplot(sub_xc$girls_XC_5k, aes(x = lon, y = lat, color = Rank)) +
geom_polygon(data = usmap, aes(x = long, y = lat, group = group),
color = "white", fill = "grey92") +
# label states
ggrepel::geom_label_repel(aes(label = state), data = sub_xc$girls_XC_5k,
size = 3, label.size = 0, segment.color = "orange") +
# point size & ranking
geom_point(aes(size = n, color = Rank, alpha = 0.8)) +
scale_color_continuous("Ranking", low = "#009900", high = "red") +
scale_size_continuous("Count", range = c(1, 12)) +
labs(title = "Girls Cross Country - ranking & frequency") -> xc_girls
```
```{r Q1_XC, echo=FALSE, warning=FALSE, fig.height = 5.2, fig.width = 16, fig.align = "center"}
ggarrange(xc_boys, xc_girls)
```
- As we can see a similar statewide pattern between events in indoor/outdoor track & field data. I respectively combine the list of all indoor and outdoor datasets, and count the frequency by state (will use this data in Q2 and Q3).
```{r Summary_Q1 indoor}
# combine the list of datasets by indoor/outdoor
bind_rows(hs_indoor18) %>%
group_by(state) %>%
count() %>%
arrange(desc(n)) -> hs_in18_freq
plot_usmap(data = hs_in18_freq, values = "n", color = "white", labels = TRUE) +
scale_fill_continuous(low = "#33CCFF", high = "#FF3300",
name = "# of athletes", label = scales::comma) +
theme(legend.position = "right",
panel.background = element_rect(colour = "Black")) +
ggtitle("The distribution of Indoor Track & Field athletes") -> ITF_dist
```
*The outdoor codes are hidden on .pdf file*
```{r Summary_Q1 outdoor, echo=FALSE}
bind_rows(hs_outdoor18) %>%
group_by(state) %>%
count() %>%
arrange(desc(n)) -> hs_out18_freq
plot_usmap(data = hs_out18_freq, values = "n", color = "white", labels = TRUE) +
scale_fill_continuous(low = "#33CCFF", high = "#FF3300",
name = "# of athletes", label = scales::comma) +
theme(legend.position = "right",
panel.background = element_rect(colour = "Black")) +
ggtitle("The distribution of Outdoor Track & Field athletes") -> OTF_dist
```
\newpage
### Question 2
`Does climate matter? How does the climate factors (temperature, rainfall inches, humidity, sunshine hours, wind speed, elevations) relate to elite athletes in the U.S.?`
```{r join hs data_Q2}
hs_freq <- hs_in18_freq %>%
left_join(hs_out18_freq, by = "state") %>%
set_names(c("state", "indoor_n", "outdoor_n"))
```
- Average Temperature: rank 1 means the warmest.
It's cooler in the northern place, but the athletes' performances are not divided by north and south but east and west. I would suggest the average temperature is not significant on players average performance, but I believe the temperature will be crucial for each local competition.
```{r Visualization_Q2_temp indoor}
# join data
temperature_hs <- temperature %>%
left_join(hs_freq, by = "state")
# visualization
ggplot(temperature_hs, aes(x = lon, y = lat, color = Rank)) +
geom_polygon(data = usmap, aes(x = long, y = lat, group = group),
color = "white", fill = "grey92") +
# label
ggrepel::geom_label_repel(aes(label = state, fontface = "italic"),
data = temperature_hs, size = 3,
label.size = 0, segment.color = "orange") +
# point size & ranking
geom_point(aes(size = indoor_n, color = Rank, alpha = 0.9)) +
scale_color_continuous("Ranking", low = "#009900", high = "red") +
scale_size_continuous(range = c(1, 12)) +
ggtitle("Indoor athletes distribution v.s. Average temperature") -> temp_ITF
```
```{r Visualization_Q2_temp outdoor, echo=FALSE}
ggplot(temperature_hs, aes(x = lon, y = lat, color = Rank)) +
geom_polygon(data = usmap, aes(x = long, y = lat, group = group),
color = "white", fill = "grey92") +
# label
ggrepel::geom_label_repel(aes(label = state, fontface = "italic"),
data = temperature_hs, size = 3,
label.size = 0, segment.color = "orange") +
# point size & ranking
geom_point(aes(size = outdoor_n, color = Rank, alpha = 0.9)) +
scale_color_continuous("Ranking", low = "#009900", high = "red") +
scale_size_continuous(range = c(1, 12)) +
ggtitle("Outdoor athletes distribution v.s. Average temperature") -> temp_OTF
```
```{r Q2_temp, warning=FALSE, fig.height = 5.2, fig.width = 16, fig.align = "center"}
ggarrange(temp_ITF, temp_OTF)
```
- Average Rainfall: rank 1 means more rain.
The average rainfall is significant to the indoor/outdoor result, the eastern states tend to perform better at the indoor activities.
```{r Visualization_Q2_rain indoor}
# join data
rainfall_hs <- rainfall %>%
left_join(hs_freq, by = "state")
# visualization
ggplot(rainfall_hs, aes(x = lon, y = lat, color = Rank)) +
geom_polygon(data = usmap, aes(x = long, y = lat, group = group),
color = "white", fill = "grey92") +
# label
ggrepel::geom_label_repel(aes(label = state, fontface = "italic"),
data = rainfall_hs, size = 3,
label.size = 0, segment.color = "orange") +
# point size & ranking
geom_point(aes(size = indoor_n, color = Rank, alpha = 0.9)) +
scale_color_continuous(low = "#009900", high = "red") +
scale_size_continuous(range = c(1, 12)) +
ggtitle("Indoor athletes distribution v.s. Average rainfall") -> rain_ITF
```
```{r Visualization_Q2_rain outdoor, echo=FALSE}
ggplot(rainfall_hs, aes(x = lon, y = lat, color = Rank)) +
geom_polygon(data = usmap, aes(x = long, y = lat, group = group),
color = "white", fill = "grey92") +
# label
ggrepel::geom_label_repel(aes(label = state, fontface = "italic"),
data = rainfall_hs, size = 3,
label.size = 0, segment.color = "orange") +
# point size & ranking
geom_point(aes(size = outdoor_n, color = Rank, alpha = 0.9)) +
scale_color_continuous(low = "#009900", high = "red") +
scale_size_continuous(range = c(1, 12)) +
ggtitle("Outdoor athletes distribution v.s. Average rainfall") -> rain_OTF
```
```{r Q2_rain, warning=FALSE, fig.height = 5.2, fig.width = 16, fig.align = "center"}
ggarrange(rain_ITF, rain_OTF)
```
- Humidity: the humidity(%) in the morning usually comes with a higher humidity.
The humidity is partially important as well.
```{r Visualization_Q2_humid morning indoor}
# join data
humidity_hs <- humidity %>%
left_join(hs_freq, by = "state")
# --------- Morning ---------
ggplot(humidity_hs, aes(x = lon, y = lat, color = morning)) +
geom_polygon(data = usmap, aes(x = long, y = lat, group = group),
color = "white", fill = "grey92") +
# label
ggrepel::geom_label_repel(aes(label = state, fontface = "italic"),
data = humidity_hs, size = 3,
label.size = 0, segment.color = "orange") +
# point size & ranking
geom_point(aes(size = indoor_n, color = morning, alpha = 0.9)) +
scale_color_continuous(low = "#009900", high = "red") +
scale_size_continuous(range = c(1, 12)) +
ggtitle("Indoor athletes distribution v.s. morning humidity") -> morn_humid_ITF
```
```{r Visualization_Q2_humid morning outdoor, echo=FALSE}
ggplot(humidity_hs, aes(x = lon, y = lat, color = morning)) +
geom_polygon(data = usmap, aes(x = long, y = lat, group = group),
color = "white", fill = "grey92") +
# label
ggrepel::geom_label_repel(aes(label = state, fontface = "italic"),
data = humidity_hs, size = 3,
label.size = 0, segment.color = "orange") +
# point size & ranking
geom_point(aes(size = outdoor_n, color = morning, alpha = 0.9)) +
scale_color_continuous(low = "#009900", high = "red") +
scale_size_continuous(range = c(1, 12)) +
ggtitle("Outdoor athletes distribution v.s. morning humidity") -> morn_humid_OTF
```
```{r Visualization_Q2_humid afternoon indoor, echo=FALSE}
ggplot(humidity_hs, aes(x = lon, y = lat, color = afternoon)) +
geom_polygon(data = usmap, aes(x = long, y = lat, group = group),
color = "white", fill = "grey92") +
# label
ggrepel::geom_label_repel(aes(label = state, fontface = "italic"),
data = humidity_hs, size = 3,
label.size = 0, segment.color = "orange") +
# point size & ranking
geom_point(aes(size = indoor_n, color = afternoon, alpha = 0.9)) +
scale_color_continuous(low = "#009900", high = "red") +
scale_size_continuous(range = c(1, 12)) +
ggtitle("Indoor athletes distribution v.s. afternoon humidity") -> aft_humid_ITF
```
```{r Visualization_Q2_humid afternoon outdoor, echo=FALSE}
ggplot(humidity_hs, aes(x = lon, y = lat, color = afternoon)) +
geom_polygon(data = usmap, aes(x = long, y = lat, group = group),
color = "white", fill = "grey92") +
# label
ggrepel::geom_label_repel(aes(label = state, fontface = "italic"),
data = humidity_hs, size = 3,
label.size = 0, segment.color = "orange") +
# point size & ranking
geom_point(aes(size = outdoor_n, color = afternoon, alpha = 0.9)) +
scale_color_continuous(low = "#009900", high = "red") +
scale_size_continuous(range = c(1, 12)) +
ggtitle("Outdoor athletes distribution v.s. afternoon humidity") -> aft_humid_OTF
```
- Showing all plots (morning/afternoon indoor/outdoor)
```{r Q2_humid, warning=FALSE, fig.height = 5.2, fig.width = 16, fig.align = "center"}
ggarrange(morn_humid_ITF, morn_humid_OTF)
ggarrange(aft_humid_ITF, aft_humid_OTF)
```
- Sunshine hours
The more sunshine hours the state has, the more better outdoor athletes they would have.
```{r Visualization_Q2_sun indoor}
# join data
sunshine_hs <- sunshine %>%
left_join(hs_freq, by = "state")
# visualization
ggplot(sunshine_hs, aes(x = lon, y = lat, color = `% Sun`)) +
geom_polygon(data = usmap, aes(x = long, y = lat, group = group),
color = "white", fill = "grey92") +
# label
ggrepel::geom_label_repel(aes(label = state, fontface = "italic"),
data = sunshine_hs, size = 3,
label.size = 0, segment.color = "orange") +
# point size & ranking
geom_point(aes(size = indoor_n, color = `% Sun`, alpha = 0.9)) +
scale_color_continuous(low = "#009900", high = "red") +
scale_size_continuous(range = c(1, 12)) +
ggtitle("Indoor athletes distribution v.s. percentage of sun") -> sun_ITF
```
```{r Visualization_Q2_sun outdoor, echo=FALSE}
ggplot(sunshine_hs, aes(x = lon, y = lat, color = `% Sun`)) +
geom_polygon(data = usmap, aes(x = long, y = lat, group = group),
color = "white", fill = "grey92") +
# label
ggrepel::geom_label_repel(aes(label = state, fontface = "italic"),
data = sunshine_hs, size = 3,
label.size = 0, segment.color = "orange") +
# point size & ranking
geom_point(aes(size = outdoor_n, color = `% Sun`, alpha = 0.9)) +
scale_color_continuous(low = "#009900", high = "red") +
scale_size_continuous(range = c(1, 12)) +
ggtitle("Outdoor athletes distribution v.s. percentage of sun") -> sun_OTF
```
```{r Q2_sun, warning=FALSE, fig.height = 5.2, fig.width = 16, fig.align = "center"}
ggarrange(sun_ITF, sun_OTF)
```
- Windspeed: rank 1 suggests a stronger wind speed.
The stronger wind speed by inland north America suggests less well performanced athletes.
```{r Visualization_Q2_windsp}
# join data
windspeed_hs <- windspeed %>%
left_join(hs_freq, by = "state")
# visualization
ggplot(windspeed_hs, aes(x = lon, y = lat, color = Rank)) +
geom_polygon(data = usmap, aes(x = long, y = lat, group = group),
color = "white", fill = "grey92") +
# label
ggrepel::geom_label_repel(aes(label = state, fontface = "italic"),
data = windspeed_hs, size = 3,
label.size = 0, segment.color = "orange") +
# point size & ranking
geom_point(aes(size = indoor_n, color = Rank, alpha = 0.9)) +
scale_color_continuous(low = "#009900", high = "red") +
scale_size_continuous(range = c(1, 12)) +
ggtitle("Indoor athletes distribution v.s. Wind speed") -> windsp_ITF
```
```{r Visualization_Q2_windsp outdoor, echo=FALSE}
ggplot(windspeed_hs, aes(x = lon, y = lat, color = Rank)) +
geom_polygon(data = usmap, aes(x = long, y = lat, group = group),
color = "white", fill = "grey92") +
# label
ggrepel::geom_label_repel(aes(label = state, fontface = "italic"),
data = windspeed_hs, size = 3,
label.size = 0, segment.color = "orange") +
# point size & ranking
geom_point(aes(size = outdoor_n, color = Rank, alpha = 0.9)) +
scale_color_continuous(low = "#009900", high = "red") +
scale_size_continuous(range = c(1, 12)) +
ggtitle("Outdoor athletes distribution v.s. Wind speed") -> windsp_OTF
```
```{r Q2_windsp, warning=FALSE, fig.height = 5.2, fig.width = 16, fig.align = "center"}
ggarrange(windsp_ITF, windsp_OTF)
```
- Elevation
According to the upper two plots, the western region in the North America tends to have a higher highest elevation than in the eastern. I found the indoor list is unrelated to the elevation. However, the last plot with average elevation versus outdoor performance list, the middle west side has higher average elevation, then the west coast is in the second place, the lowest average elevation is in the east region. But the performance list gives a converse result, people in the west (higher elevation) generally have a better performance than the east region (lower elevation) for the outdoor competitions.
```{r Visualization_Q2_elevation}
# join data
elevation_hs <- elevation %>%
left_join(hs_freq, by = "state")
# visualization
ggplot(elevation_hs, aes(x = lon, y = lat, color = `Highest Elevation`)) +
geom_polygon(data = usmap, aes(x = long, y = lat, group = group),
color = "white", fill = "grey92") +
# label
ggrepel::geom_label_repel(aes(label = state, fontface = "italic"),
data = elevation_hs, size = 3,
label.size = 0, segment.color = "orange") +
# point size & ranking
geom_point(aes(size = indoor_n, color = `Highest Elevation`, alpha = 0.9)) +
scale_color_continuous(low = "#009900", high = "red") +
scale_size_continuous(range = c(1, 12)) +
ggtitle("Indoor athletes distribution v.s. the peak") -> elev_peak_ITF
```
```{r Visualization_Q2_elevation outdoor, echo=FALSE}
ggplot(elevation_hs, aes(x = lon, y = lat, color = `Highest Elevation`)) +
geom_polygon(data = usmap, aes(x = long, y = lat, group = group),
color = "white", fill = "grey92") +
# label
ggrepel::geom_label_repel(aes(label = state, fontface = "italic"),
data = elevation_hs, size = 3,
label.size = 0, segment.color = "orange") +
# point size & ranking
geom_point(aes(size = outdoor_n, color = `Highest Elevation`, alpha = 0.9)) +
scale_color_continuous(low = "#009900", high = "red") +
scale_size_continuous(range = c(1, 12)) +
ggtitle("Outdoor athletes distribution v.s. the peak") -> elev_peak_OTF
ggplot(elevation_hs, aes(x = lon, y = lat, color = `Lowest Elevation`)) +
geom_polygon(data = usmap, aes(x = long, y = lat, group = group),
color = "white", fill = "grey92") +
# label
ggrepel::geom_label_repel(aes(label = state, fontface = "italic"),
data = elevation_hs, size = 3,
label.size = 0, segment.color = "orange") +
# point size & ranking
geom_point(aes(size = outdoor_n, color = `Lowest Elevation`, alpha = 0.9)) +
scale_color_continuous(low = "#009900", high = "red") +
scale_size_continuous(range = c(1, 12)) +
ggtitle("Outdoor athletes distribution v.s. the deep") -> elev_deep_OTF
ggplot(elevation_hs, aes(x = lon, y = lat, color = `avg_Elevation`)) +
geom_polygon(data = usmap, aes(x = long, y = lat, group = group),
color = "white", fill = "grey92") +
# label
ggrepel::geom_label_repel(aes(label = state, fontface = "italic"),
data = elevation_hs, size = 3,
label.size = 0, segment.color = "orange") +
# point size & ranking
geom_point(aes(size = outdoor_n, color = `avg_Elevation`, alpha = 0.9)) +
scale_color_continuous(low = "#009900", high = "red") +
scale_size_continuous(range = c(1, 12)) +
ggtitle("Outdoor athletes distribution v.s. the average Elevation") -> elev_avg_OTF
```
```{r Q2_elevation, warning=FALSE, fig.height = 5.2, fig.width = 16, fig.align = "center"}
ggarrange(elev_peak_ITF, elev_peak_OTF)
ggarrange(elev_deep_OTF, elev_avg_OTF)
```
\newpage