-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path20_02_04_TidyTuesday_Football.Rmd
67 lines (53 loc) · 2.01 KB
/
20_02_04_TidyTuesday_Football.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
---
title: 'TidyTuesday: Football attendance by state'
author: "Kyla McConnell"
date: "2/4/2020"
output:
pdf_document: default
html_document: default
---
```{r setup, include=FALSE}
library(tidyverse)
library(maps)
library(sf)
library(rayshader)
library(av)
attendance <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-02-04/attendance.csv')
team_loc <- read_csv("https://raw.githubusercontent.com/ali-ce/datasets/master/NFL/NFL%20Teams.csv") #from Github user ali-ce
states <- st_as_sf(map("state", plot = FALSE, fill = TRUE)) #US state data
```
# Football attendance by state
###Join football team names to the state they are located in
```{r}
df <- attendance %>%
unite(team_full, team, team_name, sep=" ") %>%
left_join(team_loc, by=c("team_full"= "Team")) %>%
select("team_full", "year", "total", "home", "away", "week", "weekly_attendance", "Location") %>%
separate("Location", c("City", "State"), sep=", ") %>%
mutate(State = tolower(State))
```
###Filter to only the year 2019, add state boundary geoms
```{r}
df_state_2019 <- df %>%
filter(year == 2019) %>%
select(State, weekly_attendance) %>%
group_by(State) %>%
summarize(total_attend = sum(weekly_attendance, na.rm = TRUE)) %>%
full_join(states, by=c("State" = "ID"))
df_state_2019$total_attend <- replace_na(df_state_2019$total_attend, 0) #add 0s to states with no team
```
###Plot (gpplot)
```{r}
mtplot <- ggplot(data = df_state_2019$geom) +
geom_sf(aes(fill=df_state_2019$total_attend))+
scale_fill_continuous(low = "#ffd23d", high = "#d01727")+
theme(legend.position = "none", panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_blank(), panel.background=element_blank())+
ggtitle("Football attendance in 2019")
mtplot
```
###Create Rayshader 3D plot
```{r}
plot_gg(mtplot)
#render_movie(mtplot, clear = TRUE)
```
Wishlist: rotating graph as gif, more informative labels/legend, display number of teams per state