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
Upper626 <- read_excel("C:/Users/Franco Castagliuolo/OneDrive - Bentley University/Neers 24/Pitchers/Upper 626/Upper 626.xlsx")
# Filter the data for the pitcher Joshua Sibley
Joshua_Sibley_data <- Upper626 %>%
  filter(Pitcher == "Sibley, Joshua")

# Create a detailed table for each pitch
detailed_pitch_table <- Joshua_Sibley_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 Joshua Sibley")
Detailed Pitch Table for Joshua Sibley
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Four-Seam 86.21 2298.44 156.33 -7.60 18.62 StrikeCalled 5.52 -0.71 5.38 5.2
Four-Seam 84.43 2260.64 134.14 -11.73 12.78 StrikeCalled 5.51 -0.83 5.40 4.5
Curveball 76.85 2777.93 32.27 -13.57 -20.14 BallCalled 5.76 -0.73 5.06 1.1
Four-Seam 85.40 2325.63 144.04 -10.50 15.74 FoulBallNotFieldable 5.43 -0.88 5.51 4.8
Changeup 85.73 2358.14 112.99 -11.56 6.10 BallCalled 5.47 -0.75 5.47 3.8
Curveball 77.55 2806.63 305.56 8.68 -4.68 StrikeSwinging 5.85 -0.43 4.62 10.2
Changeup 83.04 2236.49 139.02 -6.98 9.31 BallCalled 5.44 -0.80 5.50 4.6
Changeup 84.88 2391.88 129.95 -13.83 12.83 BallCalled 5.49 -0.79 5.51 4.3
Cutter 84.34 2338.10 175.66 -0.61 9.28 BallCalled 5.47 -0.81 5.48 5.9
Changeup 85.65 2410.94 134.66 -12.81 13.90 StrikeCalled 5.55 -0.66 5.42 4.5
Changeup 83.26 2304.40 133.91 -12.68 13.45 InPlay 5.52 -0.62 5.46 4.5
Changeup 85.03 2350.60 117.35 -13.70 8.22 BallCalled 5.54 -0.86 5.44 3.9
Four-Seam 85.12 2365.10 143.74 -12.73 18.63 FoulBallNotFieldable 5.57 -0.48 5.57 4.8
Curveball 76.46 2633.39 292.15 13.98 -4.03 BallCalled 5.82 -0.51 4.70 9.7
Four-Seam 85.75 2264.35 173.73 -1.54 15.37 BallCalled 5.63 -0.61 5.49 5.8
Cutter 84.20 2262.47 167.96 -2.57 13.45 InPlay 5.44 -0.62 5.34 5.6
Curveball 77.44 2716.60 304.75 7.43 -3.62 StrikeCalled 5.82 -0.65 4.79 10.2
Curveball 77.17 2797.30 317.11 10.39 -9.55 BallCalled 5.89 -0.33 5.07 10.6
Cutter 84.18 2321.83 163.48 -4.01 14.84 BallCalled 5.62 -0.42 5.33 5.4
Four-Seam 86.55 2357.09 160.52 -5.08 15.65 InPlay 5.55 -0.51 5.38 5.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:  20
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Joshua Sibley")
Summary Pitch Table for Joshua Sibley
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 30.00% 6 4 2 66.67% 33.33% 84.60 2342.07 10.63 -11.93 127.98 4.3 5.50 -0.75 5.47
Curveball 25.00% 5 3 2 60.00% 40.00% 77.09 2746.37 -8.40 5.38 250.37 8.4 5.83 -0.53 4.85
Cutter 15.00% 3 2 1 66.67% 33.33% 84.24 2307.47 12.52 -2.40 169.03 5.6 5.51 -0.62 5.38
Four-Seam 30.00% 6 1 5 16.67% 83.33% 85.58 2311.88 16.13 -8.20 152.08 5.1 5.54 -0.67 5.46
# 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("Joshua Sibley maximum FB velocity: ", max_fb_velocity, "mph\n")
## Joshua Sibley maximum FB velocity:  86.55 mph
# Prepare data for plotting pitch locations
pitch_location_data <- Joshua_Sibley_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 Joshua Sibley",
       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 Joshua Sibley",
       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")
  )