data <- read_excel("../00_data/MyData.xlsx")
data
## # A tibble: 638 × 15
## team team_…¹ year wins loss point…² point…³ point…⁴ margi…⁵ stren…⁶
## <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Miami Dolphi… 2000 11 5 323 226 97 6.1 1
## 2 Indianapol… Colts 2000 10 6 429 326 103 6.4 1.5
## 3 New York Jets 2000 9 7 321 321 0 0 3.5
## 4 Buffalo Bills 2000 8 8 315 350 -35 -2.2 2.2
## 5 New England Patrio… 2000 5 11 276 338 -62 -3.9 1.4
## 6 Tennessee Titans 2000 13 3 346 191 155 9.7 -1.3
## 7 Baltimore Ravens 2000 12 4 333 165 168 10.5 -2.5
## 8 Pittsburgh Steele… 2000 9 7 321 255 66 4.1 -0.2
## 9 Jacksonvil… Jaguars 2000 7 9 367 327 40 2.5 -1.4
## 10 Cincinnati Bengals 2000 4 12 185 359 -174 -10.9 0.4
## # … with 628 more rows, 5 more variables: simple_rating <dbl>,
## # offensive_ranking <dbl>, defensive_ranking <dbl>, playoffs <chr>,
## # sb_winner <chr>, and abbreviated variable names ¹team_name, ²points_for,
## # ³points_against, ⁴points_differential, ⁵margin_of_victory,
## # ⁶strength_of_schedule
Make two bar charts here - one before ordering another after
# Transform data: calculate average wins by team
wins_by_team <- data %>%
group_by(team) %>%
summarise(
avg_wins = mean(wins, na.rm = TRUE)
)
wins_by_team
## # A tibble: 32 × 2
## team avg_wins
## <chr> <dbl>
## 1 Arizona 6.85
## 2 Atlanta 8.2
## 3 Baltimore 9.5
## 4 Buffalo 6.85
## 5 Carolina 7.85
## 6 Chicago 7.85
## 7 Cincinnati 7.25
## 8 Cleveland 4.95
## 9 Dallas 8.4
## 10 Denver 9.1
## # … with 22 more rows
# Plot
wins_by_team %>%
ggplot(aes(x = team, y = avg_wins)) +
geom_point()
wins_by_team %>%
ggplot(aes(x = avg_wins, y = fct_reorder(.f = team, .x = avg_wins))) +
geom_point() +
# Labeling
labs(y = NULL, x = "Mean Wins by Team")
Show examples of three functions:
data %>%
mutate(playoffs = fct_recode(playoffs,
"Not SB Contender" = "No Playoffs",
"SB Contender" = "Playoffs"
)) %>%
count(playoffs)
## # A tibble: 2 × 2
## playoffs n
## <fct> <int>
## 1 Not SB Contender 398
## 2 SB Contender 240
data %>%
mutate(team_name = fct_collapse(team_name,
AFCNorth = c("Browns", "Ravens", "Steelers", "Bengals"),
AFCSouth = c("Colts", "Jaguars", "Texans", "Titans"),
AFCEast = c("Bills", "Dolphins", "Patriots", "Jets"),
AFCWest = c("Chiefs", "Raiders", "Chargers", "Broncos"),
NFCNorth = c("Vikings", "Lions", "Packers", "Bears"),
NFCSouth = c("Buccaneers", "Panthers", "Saints", "Falcons"),
NFCEast = c("Cowboys", "Eagles", "Giants", "Redskins"),
NFCWest = c("49ers", "Cardinals", "Rams", "Seahawks")
)) %>%
count(team_name)
## # A tibble: 8 × 2
## team_name n
## <fct> <int>
## 1 NFCWest 80
## 2 NFCNorth 80
## 3 AFCNorth 80
## 4 AFCEast 80
## 5 AFCWest 80
## 6 NFCSouth 80
## 7 AFCSouth 78
## 8 NFCEast 80
data %>%
mutate(sb_winner = fct_lump(sb_winner, n = 10)) %>%
count(sb_winner, sort = TRUE) %>%
print(n = Inf)
## # A tibble: 2 × 2
## sb_winner n
## <fct> <int>
## 1 No Superbowl 618
## 2 Won Superbowl 20
No need to do anything here.