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

# Create a detailed table for each pitch
detailed_pitch_table <- Nate_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 Nate Isler")
Detailed Pitch Table for Nate Isler
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Four-Seam 88.31 2105.82 176.58 -1.29 22.77 BallCalled 6.51 0.68 6.19 5.9
Four-Seam 86.90 2091.34 211.20 10.94 19.28 BallCalled 6.49 0.66 5.95 7.0
Four-Seam 87.06 2171.59 218.80 14.46 19.18 BallCalled 6.32 1.04 5.88 7.3
Four-Seam 85.09 2045.54 214.66 12.73 19.71 BallCalled 6.45 0.88 5.97 7.2
Four-Seam 86.33 2135.52 211.48 9.95 17.41 BallCalled 6.45 1.08 6.01 7.0
Changeup 86.63 2019.15 214.83 11.26 17.37 InPlay 6.51 1.12 5.84 7.2
Curveball 71.83 2209.30 38.67 -13.21 -14.72 BallCalled 6.24 1.21 5.66 1.3
Curveball 72.34 2338.11 39.79 -13.01 -13.81 BallCalled 6.27 1.31 5.40 1.3
Four-Seam 86.00 2202.30 208.63 10.04 19.59 BallCalled 6.43 1.18 6.09 7.0
Four-Seam 85.46 2144.52 220.96 14.53 18.11 StrikeCalled 6.32 1.32 6.07 7.4
Four-Seam 85.15 2142.93 219.09 14.14 18.71 InPlay 6.41 1.28 5.92 7.3
Four-Seam 85.72 2061.39 198.32 7.19 23.07 InPlay 6.37 0.80 6.02 6.6
Four-Seam 86.17 2232.94 203.98 10.22 24.40 StrikeCalled 6.41 1.42 5.91 6.8
Four-Seam 86.44 2247.11 207.06 11.08 23.13 FoulBallNotFieldable 6.44 1.45 5.81 6.9
Four-Seam 87.96 2191.02 196.59 6.93 24.59 BallCalled 6.60 1.00 5.85 6.6
Curveball 73.90 2286.21 39.73 -10.62 -11.06 InPlay 6.32 1.39 5.40 1.3
Four-Seam 86.28 2082.66 206.95 10.98 22.95 FoulBallNotFieldable 6.47 1.23 5.84 6.9
Four-Seam 87.56 2181.49 209.69 11.15 21.05 BallCalled 6.51 1.21 5.83 7.0
Curveball 71.16 2269.90 22.79 -8.44 -17.98 StrikeSwinging 6.25 1.37 5.70 0.8
Four-Seam 87.87 2193.61 203.53 7.68 19.13 StrikeCalled 6.50 1.05 5.92 6.8
Four-Seam 86.28 2086.59 194.88 5.80 23.40 StrikeCalled 6.26 1.46 5.86 6.5
Splitter 76.08 786.53 263.08 7.49 2.69 BallCalled 6.02 1.98 5.53 8.8
Four-Seam 86.86 2176.92 203.35 8.36 20.83 StrikeSwinging 6.39 1.52 5.90 6.8
Four-Seam 88.96 2216.00 200.05 7.07 20.78 BallCalled 6.39 1.08 6.02 6.7
Four-Seam 86.66 2100.94 209.06 9.65 18.83 BallCalled 6.46 1.49 5.84 7.0
Four-Seam 86.01 2082.59 203.18 7.81 19.68 BallCalled 6.38 1.32 5.95 6.8
Four-Seam 87.88 2115.58 200.65 6.87 19.69 BallCalled 6.44 1.14 5.98 6.7
Four-Seam 87.01 2148.02 192.94 4.99 23.14 StrikeSwinging 6.39 1.46 5.89 6.4
Four-Seam 87.15 2153.96 206.15 8.79 19.34 BallCalled 6.46 1.24 5.98 6.9
Four-Seam 86.42 2118.21 189.98 4.08 24.64 BallCalled 6.40 1.13 6.04 6.3
Four-Seam 85.92 2046.14 185.58 2.19 23.89 StrikeSwinging 6.31 1.07 6.08 6.2
Cutter 85.07 1934.14 174.33 -2.00 21.68 FoulBallNotFieldable 6.49 0.85 6.06 5.8
Four-Seam 85.26 2002.32 201.48 8.04 21.82 FoulBallNotFieldable 6.42 0.95 6.04 6.7
Four-Seam 85.73 2091.78 205.24 8.67 19.70 BallCalled 6.40 0.83 5.99 6.8
Four-Seam 85.55 2011.02 185.53 2.48 27.01 FoulBallNotFieldable 6.33 0.69 6.12 6.2
Four-Seam 88.43 2141.98 196.23 6.50 23.76 StrikeSwinging 6.45 0.59 6.01 6.5
Four-Seam 88.80 2246.52 191.93 4.96 24.86 InPlay 6.47 0.57 6.17 6.4
# 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:  37
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Nate Isler")
Summary Pitch Table for Nate Isler
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 2.70% 1 0 1 0.00% 100.00% 86.63 2019.15 17.37 11.26 214.83 7.2 6.51 1.12 5.84
Curveball 10.81% 4 2 2 50.00% 50.00% 72.31 2275.88 -14.39 -11.32 35.24 1.2 6.27 1.32 5.54
Cutter 2.70% 1 0 1 0.00% 100.00% 85.07 1934.14 21.68 -2.00 174.33 5.8 6.49 0.85 6.06
Four-Seam 81.08% 30 15 15 50.00% 50.00% 86.71 2132.28 21.48 8.23 202.46 6.8 6.42 1.09 5.97
Splitter 2.70% 1 1 0 100.00% 0.00% 76.08 786.53 2.69 7.49 263.08 8.8 6.02 1.98 5.53
# 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("Nate Isler maximum FB velocity: ", max_fb_velocity, "mph\n")
## Nate Isler maximum FB velocity:  88.96 mph
# Prepare data for plotting pitch locations
pitch_location_data <- Nate_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 Nate Isler",
       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 Nate 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")
  )