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
Keene614 <- read_excel("C:/Users/Franco Castagliuolo/OneDrive - Bentley University/Neers 24/Pitchers/Keene614/Keene614.xlsx")
# Filter the data for the pitcher Isler
Isler_data <- Keene614 %>%
  filter(Pitcher == "Isler, Nate")

# Create a detailed table for each pitch
detailed_pitch_table <- Isler_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 Isler")
Detailed Pitch Table for Isler
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Changeup 76.28 1083.00 229.17 11.70 11.72 BallCalled 5.76 1.81 5.89 7.6
Four-Seam 87.29 2225.81 201.44 8.26 22.45 FoulBallNotFieldable 6.41 0.78 6.13 6.7
Four-Seam 87.66 2137.49 201.97 8.10 21.35 FoulBallNotFieldable 6.33 0.97 6.30 6.7
Four-Seam 88.25 2272.09 182.28 0.84 22.49 StrikeSwinging 6.41 0.66 6.37 6.1
Splitter 76.57 918.01 250.50 4.28 3.29 BallCalled 5.83 1.51 5.80 8.3
Four-Seam 86.93 2252.43 188.25 3.10 22.79 BallCalled 6.22 1.01 6.24 6.3
Four-Seam 86.29 2215.24 177.59 -0.88 22.29 FoulBallFieldable 6.34 0.79 6.23 5.9
Four-Seam 87.70 2243.25 202.92 8.54 21.63 BallCalled 6.34 0.63 6.11 6.8
Four-Seam 88.29 2301.47 193.62 5.48 24.00 StrikeCalled 6.20 1.04 6.33 6.5
Curveball 74.77 2357.21 20.88 -5.83 -13.33 BallinDirt 6.21 0.90 5.42 0.7
Four-Seam 86.92 2239.05 182.76 1.15 25.31 InPlay 6.32 0.82 6.20 6.1
Four-Seam 89.10 2316.96 195.74 6.34 23.75 StrikeCalled 6.31 0.84 6.23 6.5
Curveball 75.86 2504.19 95.88 -18.05 3.57 BallCalled 5.75 1.49 6.01 3.2
Slider 76.35 2420.77 118.34 -9.00 6.73 BallinDirt 5.72 1.91 5.97 3.9
Four-Seam 87.57 2305.70 192.79 5.50 25.73 StrikeCalled 6.20 1.16 6.44 6.4
Four-Seam 88.50 2240.95 199.18 7.91 24.16 BallCalled 6.30 0.81 6.20 6.6
Four-Seam 86.87 2174.83 192.24 5.24 25.62 InPlay 6.25 0.89 6.31 6.4
Changeup 78.99 1201.39 240.19 14.32 9.77 BallinDirt 5.81 1.52 5.94 8.0
Four-Seam 86.77 2219.25 205.40 9.89 22.15 StrikeCalled 6.22 1.13 6.28 6.8
Four-Seam 85.97 2199.28 196.75 6.22 22.03 BallCalled 6.21 1.20 6.23 6.6
Four-Seam 87.55 2206.17 205.58 9.19 20.48 BallCalled 6.09 1.24 6.36 6.9
Four-Seam 86.73 2242.16 215.03 12.54 19.16 StrikeCalled 6.16 1.19 6.26 7.2
Four-Seam 86.29 2204.28 214.19 11.80 18.59 BallCalled 6.11 1.14 6.42 7.1
Four-Seam 87.02 2001.91 199.58 7.25 21.67 InPlay 6.19 0.98 6.28 6.7
# 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\n")
## Total number of pitches thrown:  24
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Isler")
Summary Pitch Table for Isler
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 8.33% 2 1 1 50.00% 50.00% 77.63 1142.20 10.75 13.01 234.68 7.8 5.78 1.67 5.92
Curveball 8.33% 2 1 1 50.00% 50.00% 75.32 2430.70 -4.88 -11.94 58.38 2.0 5.98 1.20 5.72
Four-Seam 75.00% 18 6 12 33.33% 66.67% 87.32 2222.13 22.54 6.47 197.07 6.6 6.26 0.96 6.27
Slider 4.17% 1 0 1 0.00% 100.00% 76.35 2420.77 6.73 -9.00 118.34 3.9 5.72 1.91 5.97
Splitter 4.17% 1 1 0 100.00% 0.00% 76.57 918.01 3.29 4.28 250.50 8.3 5.83 1.51 5.80
# 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("Isler maximum FB velocity: ", max_fb_velocity, "mph\n")
## Isler maximum FB velocity:  89.1 mph
# Prepare data for plotting pitch locations
pitch_location_data <- Isler_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 Isler",
       x = "Horizontal Location (feet)",
       y = "Vertical Location (feet)",
       color = "Swing/Take",
       shape = "Chase") +
  facet_wrap(~ AutoPitchType) + # Create individual graphs for each pitch type
  geom_text(aes(x = -1.75, y = 5, label = "RHH"), color = "black", size = 3, hjust = 0) + # Label for RHH
  geom_text(aes(x = 1.75, y = 5, label = "LHH"), color = "black", size = 3, hjust = 1) + # Label for LHH
  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 Isler",
       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")
  )