new paragraph
# echo echoes back the code, why you get the option to view / not view it
# code goes in here
setwd("~/Library/CloudStorage/OneDrive-LoyolaUniversityMaryland/fall 26/IS470")
source("https://raw.githubusercontent.com/ptallon/SportsAnalytics_Fall2026/refs/heads/main/SharedCode.R")
## suppressMessages(library(data.table)) can be used to hide error messages / warnings that pop up when library is run
load_packages(c("data.table", "dplyr", "ggplot2", "stringr", "scales", "hms", "gganimate",
"gifski", "ggforce", "sf", "av", "knitr", "kableExtra", "ggimage"))
y1 <- fread("BDB2022/tracking2018.csv")
y2 <- fread("BDB2022/tracking2019.csv")
y3 <- fread("BDB2022/tracking2020.csv")
tracking_df <- rbind(y1, y2, y3)
rm(y1, y2, y3) # rm = remove from memory
plays <- fread("BDB2022/plays.csv")
players <- fread("BDB2022/players.csv")
games <- fread("BDB2022/games.csv")
plays_df <- left_join(plays, players, by = c("kickerId" = "nflId"))
plays_df <- left_join(plays_df, games, by = c("gameId"))
df <- tracking_df |>
filter(gameId == 2018123000, playId == 36) |>
left_join(plays |> select(gameId, playId, absoluteYardlineNumber, playDescription),
by = c("gameId", "playId")) |>
data.frame()
source("https://raw.githubusercontent.com/mlfurman3/gg_field/main/gg_field.R") # allows you to use someone else's code legally
ggplot(df) +
# change the colors, shapes, and sizes of the points
scale_size_manual(values = c(6, 4, 6), guide = "none") +
scale_shape_manual(values = c(21, 16, 21), guide = "none") +
scale_fill_manual(values = c("firebrick1", "black", "purple"), guide = "none") +
scale_color_manual(values = c("black", "#663300", "black"), guide = "none") +
# visualize the field
gg_field(yardmin = max(-5, min(df$x) - 5), yardmax = min(125, max(df$x) + 5)) +
# add points for each player and the ball
geom_point(aes(x = x, y = y, shape = team, color = team, size = team, fill = team)) +
# insert jersey number for each player (!= football so we don't have a bunch of N/As for the ball)
geom_text(data = df |> filter(team != "football"),
aes(x = x, y = y, label = jerseyNumber), color = "white", size = 3.5, vjust = 0.36)