knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)
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)

# Load the data
Shore624 <- read_excel("C:/Users/Franco Castagliuolo/OneDrive - Bentley University/Neers 24/Pitchers/Shore 624/Shore 624.xlsx")
# Filter the data for the pitcher Aidan Krupp
Aidan_Krupp_data <- Shore624 %>%
  filter(Pitcher == "Krupp, Aidan")

# Create a detailed table for each pitch
detailed_pitch_table <- Aidan_Krupp_data %>%
  select(AutoPitchType, RelSpeed, SpinRate, SpinAxis, HorzBreak, InducedVertBreak, PitchCall, RelHeight, RelSide, Extension) %>%
  rename(
    ReleaseSpeed = RelSpeed,
    Tilt = SpinAxis,
    HorizontalBreak = HorzBreak,
    InducedVerticalBreak = InducedVertBreak,
    ReleaseHeight = RelHeight,
    ReleaseSide = RelSide
  ) %>%
  mutate(
    ReleaseSpeed = round(ReleaseSpeed, 2),
    SpinRate = round(SpinRate, 2),
    Tilt = round(Tilt, 2),
    HorizontalBreak = round(HorizontalBreak, 2),
    InducedVerticalBreak = round(InducedVerticalBreak, 2),
    ReleaseHeight = round(ReleaseHeight, 2),
    ReleaseSide = round(ReleaseSide, 2),
    Extension = round(Extension, 2),
    ClockTilt = round((Tilt / 30) %% 12, 1) # Interpret Tilt as clock face
  )

# Display the detailed table
knitr::kable(detailed_pitch_table, caption = "Detailed Pitch Table for Aidan Krupp")
Detailed Pitch Table for Aidan Krupp
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Changeup 86.31 1984.39 269.34 21.57 1.62 FoulBallNotFieldable 4.29 3.19 5.35 9.0
Changeup 84.31 1859.46 269.01 21.40 1.79 InPlay 4.21 3.01 5.60 9.0
Changeup 85.14 1923.23 265.76 21.19 2.94 BallCalled 4.25 2.98 5.52 8.9
Changeup 85.42 1926.13 245.30 18.55 9.89 BallCalled 4.10 2.88 5.62 8.2
Changeup 85.96 1898.63 272.87 20.68 0.30 BallCalled 4.20 2.96 5.71 9.1
Changeup 84.03 1903.53 250.38 20.52 8.75 BallCalled 4.14 2.83 5.56 8.3
Changeup 85.72 1878.96 264.62 22.27 3.53 BallCalled 4.03 2.85 5.62 8.8
Changeup 85.43 1922.78 257.27 21.38 6.24 BallCalled 4.04 2.86 5.67 8.6
Sinker 87.42 2013.51 250.29 18.90 8.08 FoulBallNotFieldable 4.06 2.97 5.68 8.3
Changeup 85.93 1903.90 249.40 20.87 9.16 InPlay 4.04 3.07 5.78 8.3
Changeup 84.14 1906.69 254.45 19.46 6.79 FoulBallNotFieldable 4.05 3.06 5.55 8.5
Changeup 85.56 1940.94 253.60 18.69 6.83 FoulBallNotFieldable 4.12 3.06 5.56 8.5
Changeup 86.62 1953.22 259.59 21.00 5.22 StrikeSwinging 4.13 3.04 5.52 8.7
Changeup 84.69 1984.95 259.33 19.78 5.08 BallCalled 3.95 3.00 5.71 8.6
Changeup 85.77 1993.15 266.26 21.89 2.82 FoulBallNotFieldable 4.11 2.96 5.44 8.9
Changeup 85.52 1889.63 255.94 26.48 7.84 BallCalled 3.95 2.93 5.53 8.5
Changeup 84.63 1942.22 254.18 23.00 7.77 FoulBallNotFieldable 3.96 3.04 5.58 8.5
Changeup 85.77 1876.73 255.03 20.30 6.77 InPlay 3.95 2.86 5.47 8.5
# Calculate the total number of pitches
total_pitches <- nrow(detailed_pitch_table)

# Create a summary table
pitch_summary <- detailed_pitch_table %>%
  group_by(AutoPitchType) %>%
  summarise(
    TotalPitches = n(),
    Usage = sprintf("%.2f%%", n() / total_pitches * 100),
    Balls = sum(PitchCall == "BallCalled"),
    Strikes = sum(PitchCall != "BallCalled"), # Count everything not a ball as a strike
    BallPercentage = sprintf('%.2f%%', Balls / TotalPitches * 100),
    StrikePercentage = sprintf('%.2f%%', Strikes / TotalPitches * 100),
    AvgVelocity = round(mean(ReleaseSpeed, na.rm = TRUE), 2),
    AvgSpinRate = round(mean(SpinRate, na.rm = TRUE), 2),
    AvgInducedVertBreak = round(mean(InducedVerticalBreak, na.rm = TRUE), 2),
    AvgHorzBreak = round(mean(HorizontalBreak, na.rm = TRUE), 2),
    AvgTilt = round(mean(Tilt, na.rm = TRUE), 2),
    AvgClockTilt = round(mean(ClockTilt, na.rm = TRUE), 1), # Clock face interpretation
    AvgReleaseHeight = round(mean(ReleaseHeight, na.rm = TRUE), 2),
    AvgReleaseSide = round(mean(ReleaseSide, na.rm = TRUE), 2),
    AvgExtension = round(mean(Extension, na.rm = TRUE), 2)
  ) %>%
  select(AutoPitchType, Usage, everything())

# Display the total number of pitches
cat("Total number of pitches thrown: ", total_pitches, "\n")
## Total number of pitches thrown:  18
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Aidan Krupp")
Summary Pitch Table for Aidan Krupp
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 94.44% 17 8 9 47.06% 52.94% 85.35 1922.86 5.49 21.12 258.96 8.6 4.09 2.98 5.58
Sinker 5.56% 1 0 1 0.00% 100.00% 87.42 2013.51 8.08 18.90 250.29 8.3 4.06 2.97 5.68
# Calculate maximum fastball velocity
max_fb_velocity <- detailed_pitch_table %>%
  filter(AutoPitchType %in% c("Four-Seam", "Two-Seam", "Sinker", "Cutter")) %>%
  summarise(MaxFBVelocity = max(ReleaseSpeed, na.rm = TRUE)) %>%
  pull(MaxFBVelocity)

# Display the maximum fastball velocity
cat("Aidan Krupp maximum FB velocity: ", max_fb_velocity, "mph\n")
## Aidan Krupp maximum FB velocity:  87.42 mph
# Prepare data for plotting pitch locations
pitch_location_data <- Aidan_Krupp_data %>%
  select(AutoPitchType, PlateLocHeight, PlateLocSide, PitchCall) %>%
  rename(
    PitchHeight = PlateLocHeight,
    PitchSide = PlateLocSide
  ) %>%
  mutate(
    SwingTake = ifelse(PitchCall %in% c("StrikeSwinging", "FoulBallNonSwinging", "FoulBallFieldable", "FoulBallNotFieldable", "InPlay"), "Swing", "Take"),
    Chase = ifelse(SwingTake == "Swing" & (PitchSide < -0.75 | PitchSide > 0.75 | PitchHeight < 1.5 | PitchHeight > 3.5), "Chase", "Non-Chase")
  )

# Create the scatter plot with specified strike zone boxes
ggplot(pitch_location_data, aes(x = PitchSide, y = PitchHeight, color = SwingTake, shape = Chase)) +
  geom_point(size = 3) + # Increase point size
  geom_rect(aes(xmin = -0.5, xmax = 0.5, ymin = 1.75, ymax = 3.25), fill = NA, color = "red", linetype = "solid", size = 1) + # Red box
  geom_rect(aes(xmin = -0.75, xmax = 0.75, ymin = 1.5, ymax = 3.5), fill = NA, color = "black", linetype = "solid", size = 1) + # Black box
  geom_rect(aes(xmin = -1.25, xmax = 1.25, ymin = 1.25, ymax = 3.75), fill = NA, color = "gray", linetype = "solid", size = 1) + # Gray box
  scale_x_continuous(limits = c(-2, 2)) +
  scale_y_continuous(limits = c(0, 5)) +
  coord_fixed(ratio = 1) + # Adjust ratio to shrink vertical distance
  labs(title = "Pitch Locations for Aidan Krupp",
       x = "Horizontal Location (feet)",
       y = "Vertical Location (feet)",
       color = "Swing/Take",
       shape = "Chase") +
  facet_wrap(~ AutoPitchType) + # Create individual graphs for each pitch type
  theme_minimal() +
  theme(
    legend.position = "right",
    panel.grid.major = element_line(color = "grey80"),
    panel.grid.minor = element_line(color = "grey90"),
    axis.text = element_text(color = "black"),
    axis.title = element_text(color = "black"),
    plot.title = element_text(color = "black"),
    legend.background = element_rect(fill = "white", color = NA),
    legend.key = element_rect(fill = "white", color = NA),
    legend.text = element_text(color = "black"),
    legend.title = element_text(color = "black")
  )

# Create the scatter plot for horizontal and vertical breaks
ggplot(detailed_pitch_table, aes(x = HorizontalBreak, y = InducedVerticalBreak, color = AutoPitchType)) +
  geom_point(size = 3) + # Increase point size
  labs(title = "Pitch Movement for Aidan Krupp",
       x = "Horizontal Break (inches)",
       y = "Induced Vertical Break (inches)",
       color = "Pitch Type") +
  theme_minimal() +
  theme(
    legend.position = "right",
    panel.grid.major = element_line(color = "grey80"),
    panel.grid.minor = element_line(color = "grey90"),
    axis.text = element_text(color = "black"),
    axis.title = element_text(color = "black"),
    plot.title = element_text(color = "black"),
    legend.background = element_rect(fill = "white", color = NA),
    legend.key = element_rect(fill = "white", color = NA),
    legend.text = element_text(color = "black"),
    legend.title = element_text(color = "black")
  )