Skip to content

Instantly share code, notes, and snippets.

@samclifford
Created April 9, 2018 14:31
Show Gist options
  • Save samclifford/b851ff5d29822c985f45b82aaecd8d9f to your computer and use it in GitHub Desktop.
Save samclifford/b851ff5d29822c985f45b82aaecd8d9f to your computer and use it in GitHub Desktop.
Tidy Tuesday NFL challenge
library(tidyverse)
library(hrbrthemes)
# download https://github.com/rfordatascience/tidytuesday/blob/master/data/tidy_tuesday_week2.xlsx
football <- read_xlsx("data/tidy_tuesday_week2.xlsx")
# get the top 16 paid players in each position for each year
to_plot <- football %>%
mutate(Team = 1:nrow(.)) %>%
gather(position, salary, -c(year, Team)) %>%
group_by(position, year) %>%
arrange(desc(salary)) %>%
mutate(rank = 1:n()) %>%
filter(rank <= 16) %>%
ungroup %>%
mutate(salary = salary/1e6) %>% # convert to millions
mutate(position_other =
fct_other(position, # we only care about running backs and the highest paid, QBs
keep=c("Quarterback",
"Running Back")))
to_plot %>%
filter(position_other == "Other") %>% # plot every other position at rear of image
ggplot(data=., aes(x= year + (9-rank)/16, # like a dodge
y=salary,
color = position_other,
group = interaction(year, position))) +
geom_path(size=1, alpha=0.075, color="black") +
theme_ipsum_rc() +
ylim(c(0,40)) + # set limits to be pretty-ish
scale_x_continuous(breaks = unique(to_plot$year)) +
ylab("Salary ($m)") +
xlab("Year") +
geom_path(data = filter(to_plot,
position_other != "Other"),
size=1, alpha=0.75) +
scale_color_manual(values=c("purple", "red"),
name="Position") +
theme(legend.position = "bottom",
panel.grid.minor.x = element_blank()) +
ggtitle("Top 16 annual salaries per position (2011-2018)",
subtitle = "Data: http://www.spotrac.com/rankings/\nGraphic: @samclifford")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment