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 Kyle Carozza
Kyle_Carozza_data <- Valley628 %>%
  filter(Pitcher == "Carozza, Kyle")

# Create a detailed table for each pitch
detailed_pitch_table <- Kyle_Carozza_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 Kyle Carozza")
Detailed Pitch Table for Kyle Carozza
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Four-Seam 89.59 2327.11 217.23 14.09 19.77 BallCalled 6.12 1.05 6.85 7.2
Four-Seam 89.26 2319.88 216.73 14.14 20.21 StrikeCalled 6.01 1.03 7.03 7.2
Four-Seam 90.38 2311.51 206.17 9.96 21.46 StrikeCalled 6.01 0.92 6.91 6.9
Four-Seam 90.11 2296.30 213.46 12.89 20.78 BallCalled 6.13 0.89 6.59 7.1
Curveball 78.60 2325.94 13.75 -1.24 -3.51 InPlay 5.86 1.11 6.36 0.5
Four-Seam 88.60 2283.80 218.14 12.57 17.32 InPlay 6.01 1.01 6.76 7.3
Four-Seam 88.61 2316.44 192.71 4.61 21.74 FoulBallFieldable 6.11 1.05 6.89 6.4
Four-Seam 90.61 2344.47 202.10 7.80 20.53 FoulBallNotFieldable 5.97 1.13 6.71 6.7
Curveball 78.55 2409.47 41.62 -5.30 -4.53 BallinDirt 5.83 1.23 6.45 1.4
Four-Seam 88.57 2213.63 212.34 11.80 19.88 InPlay 5.94 1.17 7.06 7.1
Curveball 76.95 2437.66 41.63 -7.30 -7.00 StrikeCalled 5.90 1.17 6.26 1.4
Four-Seam 89.84 2330.10 212.62 13.67 22.59 InPlay 6.00 1.02 6.73 7.1
Four-Seam 88.90 2277.85 211.07 10.27 18.14 StrikeSwinging 5.99 1.06 7.00 7.0
Four-Seam 85.85 2239.11 209.21 10.26 19.55 StrikeCalled 6.09 0.83 7.21 7.0
Curveball 79.37 2496.42 45.45 -5.32 -4.09 BallCalled 5.77 1.08 6.40 1.5
Four-Seam 88.82 2229.89 207.57 9.58 19.45 StrikeSwinging 5.94 0.97 6.84 6.9
Curveball 77.10 2377.30 32.39 -4.88 -6.34 StrikeSwinging 5.84 1.11 5.97 1.1
Curveball 75.17 2224.08 22.62 -4.81 -9.99 StrikeCalled 5.82 1.03 6.34 0.8
Four-Seam 90.09 2281.46 208.41 10.69 21.10 BallCalled 5.96 0.94 6.59 6.9
Four-Seam 89.97 2247.55 206.64 9.71 20.63 BallCalled 5.98 1.02 6.57 6.9
Curveball 77.46 2225.52 10.14 -1.11 -4.71 StrikeSwinging 5.83 1.02 6.38 0.3
Four-Seam 85.65 2172.59 213.94 11.53 18.52 BallCalled 5.95 1.08 6.67 7.1
Four-Seam 86.33 2202.87 217.96 12.08 16.82 BallCalled 5.94 1.09 6.72 7.3
Four-Seam 88.61 2227.74 203.88 9.34 22.46 BallCalled 6.03 1.06 6.71 6.8
Four-Seam 86.89 2225.15 208.36 10.01 19.91 BallCalled 6.05 0.73 6.72 6.9
Curveball 77.28 2396.78 22.66 -3.81 -7.51 BallinDirt 5.76 0.87 6.19 0.8
Four-Seam 88.37 2176.56 203.44 6.99 17.35 FoulBallNotFieldable 5.96 0.98 6.87 6.8
Curveball 76.90 2391.48 18.57 -2.59 -6.29 StrikeSwinging 5.75 1.21 6.41 0.6
Curveball 77.43 2441.28 40.54 -7.75 -7.70 InPlay 5.78 1.20 5.97 1.4
Four-Seam 88.47 2273.85 206.41 9.18 19.72 StrikeSwinging 5.96 0.93 6.74 6.9
Four-Seam 88.06 2234.33 205.49 9.62 21.40 BallCalled 6.05 0.92 6.72 6.8
Four-Seam 88.17 2194.50 207.62 9.31 19.03 BallCalled 5.96 1.08 6.94 6.9
Four-Seam 88.16 2218.21 207.92 10.70 21.49 BallCalled 5.92 0.90 6.80 6.9
Four-Seam 87.64 2222.22 208.68 10.21 20.07 BallCalled 5.93 1.08 6.76 7.0
# 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:  34
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Kyle Carozza")
Summary Pitch Table for Kyle Carozza
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Curveball 29.41% 10 1 9 10.00% 90.00% 77.48 2372.59 -6.17 -4.41 28.94 1 5.81 1.1 6.27
Four-Seam 70.59% 24 12 12 50.00% 50.00% 88.56 2256.96 20.00 10.46 209.09 7 6.00 1.0 6.81
# 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("Kyle Carozza maximum FB velocity: ", max_fb_velocity, "mph\n")
## Kyle Carozza maximum FB velocity:  90.61 mph
# Prepare data for plotting pitch locations
pitch_location_data <- Kyle_Carozza_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 Kyle Carozza",
       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 Kyle Carozza",
       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")
  )