# Load necessary libraries
library(readxl)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
library(knitr)
library(kableExtra)
## 
## Attaching package: 'kableExtra'
## The following object is masked from 'package:dplyr':
## 
##     group_rows
# Load the data for Jensen
file_path <- "C:/Users/Franco Castagliuolo/OneDrive - Bentley University/Neers 24/Jensen.xlsx"
jensen_data <- read_excel(file_path)

# Filter for Max Jensen's triples and home runs
jensen_filtered <- jensen_data %>%
  filter(PlayResult %in% c("Triple", "HomeRun"))

# Select relevant columns for the table and round the values
jensen_table <- jensen_filtered %>%
  select(PlayResult, ExitSpeed, Angle, Distance) %>%
  mutate(across(c(ExitSpeed, Angle, Distance), round, 2))
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `across(c(ExitSpeed, Angle, Distance), round, 2)`.
## Caused by warning:
## ! The `...` argument of `across()` is deprecated as of dplyr 1.1.0.
## Supply arguments directly to `.fns` through an anonymous function instead.
## 
##   # Previously
##   across(a:b, mean, na.rm = TRUE)
## 
##   # Now
##   across(a:b, \(x) mean(x, na.rm = TRUE))
# Print the filtered table using kable for a cleaner format
jensen_table %>%
  kable(format = "html", table.attr = "class='table table-striped'", align = 'c', caption = "Metrics") %>%
  kable_styling(full_width = F, position = "left")
Metrics
PlayResult ExitSpeed Angle Distance
Triple 100.08 15.86 322.92
HomeRun 101.81 23.27 397.77
# Create a plot of the pitches in and around the strike zone, excluding pitch type
ggplot(jensen_filtered, aes(x = PlateLocSide, y = PlateLocHeight)) +
  geom_point(aes(color = PlayResult), size = 3) +
  geom_rect(aes(xmin = -0.85, xmax = 0.85, ymin = 1.5, ymax = 3.5), fill = "transparent", color = "black", linetype = "dashed") +
  labs(title = "Nuke and Laser Locations in the Strike Zone",
       x = "Horizontal Location",
       y = "Vertical Location",
       color = "Play Result") +
  theme_minimal()