As a person who closely follows the NFL, a sentiment I see often is how good a team’s draft was and how lucky they are to have their general manager (or how bad a team’s draft was and how unfortunate they are to have their general manager). I wanted to see if there were teams that were better at drafting than others, and if it is possible to consolidate my findings into unified graphics.

library(shiny)
## Warning: package 'shiny' was built under R version 4.6.2
library(bslib)
## Warning: package 'bslib' was built under R version 4.6.2
## 
## Attaching package: 'bslib'
## The following object is masked from 'package:utils':
## 
##     page
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.6.2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.2.1     ✔ readr     2.2.0
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.3     ✔ tibble    3.3.1
## ✔ lubridate 1.9.5     ✔ tidyr     1.3.2
## ✔ purrr     1.2.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(nflreadr)
## Warning: package 'nflreadr' was built under R version 4.6.2
library(plotly)
## Warning: package 'plotly' was built under R version 4.6.2
## 
## Attaching package: 'plotly'
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following object is masked from 'package:graphics':
## 
##     layout
library(DT)
## Warning: package 'DT' was built under R version 4.6.2
## 
## Attaching package: 'DT'
## 
## The following objects are masked from 'package:shiny':
## 
##     dataTableOutput, renderDataTable
# Load NFL draft data
draft <- load_draft_picks()

# Clean and prepare data
draft <- draft %>%
  mutate(
    car_av = as.numeric(car_av),
    w_av = as.numeric(w_av),
    probowls = as.numeric(probowls),
    allpro = as.numeric(allpro),
    hof = as.numeric(hof),
    season = as.integer(season),
    pick = as.numeric(pick),
    team = case_when(
      team == "PHO" ~ "ARI",
      team == "OAK" ~ "LVR",
      team == "RAI" ~ "LVR",
      team == "SDG" ~ "LAC",
      team == "STL" ~ "LAR",
      team == "RAM" ~ "LAR",
      TRUE ~ team
    )
  ) %>%
  filter(!is.na(team), !is.na(season))

teams <- sort(unique(draft$team))

ui <- fluidPage(
  
  
  titlePanel("How Teams Draft"),
  
  sidebarLayout(
    
    sidebarPanel(
      
      h4("Choose your time period"),
      
      sliderInput(
        "years",
        "Draft year range:",
        min = min(draft$season, na.rm = TRUE),
        max = max(draft$season, na.rm = TRUE),
        value = c(2000, max(draft$season, na.rm = TRUE)),
        sep = ""
      ),
      
      selectInput(
        "metric",
        "Evaluation metric:",
        choices = c(
          "Weighted AV" = "w_av",
          "Pro Bowl selections" = "probowls",
          "All-Pro selections" = "allpro",
          "Hall of Fame selections" = "hof"
        )
      ),
      
      selectInput(
        "position",
        "Position:",
        choices = c("All positions", sort(unique(draft$position))),
        selected = "All positions"
      ),
      
      selectInput(
        "team",
        "Explore a team:",
        choices = teams,
        selected = teams[1]
      )
    ),
    
    mainPanel(
      
      tabsetPanel(
        
        tabPanel(
          "Team Rankings",
          h3("Which teams draft the best?"),
          plotlyOutput("rankings"),
          DTOutput("ranking_table")
        ),
        
        tabPanel(
          "Team History",
          h3("Draft performance over time"),
          plotlyOutput("team_history")
        ),
        
        tabPanel(
          "Player Explorer",
          h3("Explore drafted players"),
          DTOutput("player_table")
        )
      )
    )
  )
)

server <- function(input, output, session) {
  
  metric_label <- reactive({
    
    case_when(
      input$metric == "w_av" ~ "Weighted Approximate Value",
      input$metric == "probowls" ~ "Average Pro Bowl Selections",
      input$metric == "allpro" ~ "Average All-Pro Selections",
      input$metric == "hof" ~ "Average Hall of Fame Selections",
      TRUE ~ "Average Player Value"
    )
    
  })
  
  filtered_data <- reactive({
    
    data <- draft %>%
      filter(
        season >= input$years[1],
        season <= input$years[2]
      )
    
    if (input$position != "All positions") {
      data <- data %>%
        filter(position == input$position)
    }
    
    data
  })
  
  team_rankings <- reactive({
    
    filtered_data() %>%
      filter(!is.na(.data[[input$metric]])) %>%
      group_by(team) %>%
      summarise(
        score = mean(.data[[input$metric]], na.rm = TRUE),
        picks = n(),
        .groups = "drop"
      ) %>%
      filter(is.finite(score)) %>%
      arrange(desc(score))
  })
  
  output$rankings <- renderPlotly({
    
    ranking <- team_rankings() %>%
      slice_max(score, n = 32) %>%
      mutate(team = reorder(team, score))
    
    p <- ggplot(
      ranking,
      aes(
        x = score,
        y = team,
        text = paste(
          "Team:", team,
          "<br>Average:", round(score, 2),
          "<br>Draft picks:", picks
        )
      )
    ) +
      geom_col(fill = "red") +
      labs(
        x = metric_label(),
        y = NULL
      ) +
      theme_minimal()
    
    ggplotly(p, tooltip = "text")
  })
  
  output$ranking_table <- renderDT({
    
    team_rankings() %>%
      mutate(Rank = row_number()) %>%
      select(Rank, Team = team, Score = score,
             `Number of Picks` = picks)
    
  }, options = list(pageLength = 15))
  
  output$team_history <- renderPlotly({
    
    history <- filtered_data() %>%
      filter(team == input$team) %>%
      group_by(season) %>%
      summarise(
        score = mean(.data[[input$metric]], na.rm = TRUE),
        .groups = "drop"
      )
    
    p <- ggplot(
      history,
      aes(x = season, y = score)
    ) +
      geom_line(color = "red", linewidth = 1) +
      geom_point(color = "black", size = 2) +
      labs(
        x = "Draft year",
        y = metric_label(),
        title = paste(input$team, "Draft Performance")
      ) +
      theme_minimal()
    
    ggplotly(p)
  })
  
  output$player_table <- renderDT({
    
    filtered_data() %>%
      filter(team == input$team) %>%
      select(
        season,
        pfr_player_name,
        position,
        round,
        pick,
        car_av,
        w_av,
        probowls,
        allpro
      ) %>%
      arrange(desc(car_av))
    
  }, options = list(pageLength = 15))
  
}

shinyApp(ui = ui, server = server)
Shiny applications not supported in static R Markdown documents

Visualization : F*** Them Picks

On February 16th, 2022, Los Angeles Rams General Manager Les Snead