AFCE_point_diff <-
pbp |>
# Keeping just regular season games:
filter(season_type == 'REG') |>
# Keeping the specified columns
dplyr::select(game_id, season, game_date, home_team,
away_team, home_score, away_score) |>
# Keeping each distinct row, dropping any duplicates
distinct() |>
# Moving the home_team and away_team into one column
pivot_longer(
cols = home_team:away_team,
names_to = 'location',
values_to = 'team'
) |>
# Dropping the '_team' from location and finding the score_diff for each team
mutate(
location = str_remove(location, '_team'),
point_diff = if_else(location == 'home', home_score - away_score, away_score - home_score)
) |>
# Keeping just the AFCE teams
filter(team %in% c('NE', 'MIA', 'NYJ', 'BUF')) |>
# Calculating the total point differential per team by year
summarize(
.by = c(season, team),
point_diff = sum(point_diff)
) |>
arrange(season, team) |>
# Adding the teams logo and color to the data:
left_join(
y = teams_colors_logos,
by = c('team' = 'team_abbr')
)
AFCE_point_diff
## # A tibble: 40 × 17
## season team point_diff team_name team_id team_nick team_conf team_division
## <int> <chr> <int> <chr> <chr> <chr> <chr> <chr>
## 1 2015 BUF 20 Buffalo Bi… 0610 Bills AFC AFC East
## 2 2015 MIA -79 Miami Dolp… 2700 Dolphins AFC AFC East
## 3 2015 NE 150 New Englan… 3200 Patriots AFC AFC East
## 4 2015 NYJ 73 New York J… 3430 Jets AFC AFC East
## 5 2016 BUF 21 Buffalo Bi… 0610 Bills AFC AFC East
## 6 2016 MIA -17 Miami Dolp… 2700 Dolphins AFC AFC East
## 7 2016 NE 191 New Englan… 3200 Patriots AFC AFC East
## 8 2016 NYJ -134 New York J… 3430 Jets AFC AFC East
## 9 2017 BUF -57 Buffalo Bi… 0610 Bills AFC AFC East
## 10 2017 MIA -112 Miami Dolp… 2700 Dolphins AFC AFC East
## # ℹ 30 more rows
## # ℹ 9 more variables: team_color <chr>, team_color2 <chr>, team_color3 <chr>,
## # team_color4 <chr>, team_logo_wikipedia <chr>, team_logo_espn <chr>,
## # team_wordmark <chr>, team_conference_logo <chr>, team_league_logo <chr>
Now we can make the line graph:
ggplot(
data = AFCE_point_diff,
mapping = aes(
x = factor(season),
y = point_diff
)
) +
geom_line(
mapping = aes(
#linetype = if_else(team %in% c('NE'), 2, 1) |> factor(),
color = if_else(team %in% c('NE'), team_color2, team_color),
group = team
),
linewidth = 1
) +
# geom_text(
# data = AFCE_point_diff |> filter(season == max(season)),
# mapping = aes(label = team),
# nudge_x = 0.3
# ) +
ggimage::geom_image(
data = AFCE_point_diff |> filter(season == max(season)),
mapping = aes(image = team_logo_wikipedia),
nudge_x = 0.3,
size = 0.1
) +
geom_hline(
yintercept = 0,
linetype = 'dashed',
color = 'grey70'
) +
labs(
x = 'Season',
y = 'Points Scored - Points Allowed'
) +
theme_bw() +
theme(legend.position = 'none') +
# Using the colors provided instead of the default group colors
scale_color_identity() +
scale_x_discrete(
expand = c(0, 0, 0.075, 0)
)