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 Max Simpson
Max_Simpson_data <- Shore624 %>%
  filter(Pitcher == "Simpson, Max")

# Create a detailed table for each pitch
detailed_pitch_table <- Max_Simpson_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 Max Simpson")
Detailed Pitch Table for Max Simpson
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Sinker 88.57 2040.85 251.62 18.17 7.22 BallCalled 5.70 2.28 6.06 8.4
Changeup 87.87 1979.44 246.75 18.62 9.16 BallCalled 5.67 2.19 5.97 8.2
Four-Seam 88.45 2197.90 219.23 13.61 17.86 StrikeCalled 5.90 1.95 6.22 7.3
Curveball 77.21 1770.88 58.12 -10.27 -5.05 StrikeCalled 5.91 1.61 5.59 1.9
Curveball 77.98 1930.13 56.53 -10.15 -5.38 BallCalled 6.07 1.73 5.48 1.9
Sinker 88.40 2115.02 232.46 15.56 13.13 BallCalled 5.63 2.13 6.22 7.7
Changeup 87.80 2005.20 263.06 18.33 3.42 StrikeCalled 5.42 2.30 6.11 8.8
Sinker 87.91 2081.07 251.61 18.51 7.33 StrikeSwinging 5.51 2.36 6.04 8.4
Sinker 88.73 2199.28 228.58 15.61 14.95 StrikeCalled 5.60 2.43 5.98 7.6
Curveball 77.97 1873.43 64.58 -12.21 -4.37 StrikeCalled 5.84 1.71 5.60 2.2
Curveball 78.23 2043.38 90.28 -16.67 1.58 StrikeSwinging 5.55 2.13 5.49 3.0
Curveball 78.75 2054.07 92.02 -14.02 2.02 InPlay 5.55 2.16 5.52 3.1
Sinker 87.88 2095.58 248.84 18.34 8.29 BallCalled 5.53 2.31 5.89 8.3
Curveball 77.56 1865.15 62.24 -12.29 -5.00 BallCalled 5.55 2.18 5.51 2.1
Sinker 89.68 2077.56 248.65 18.82 8.46 BallCalled 5.40 2.37 6.31 8.3
Sinker 88.44 2005.84 239.87 16.21 10.58 StrikeCalled 5.49 2.23 6.14 8.0
Sinker 88.50 2077.62 235.60 15.62 11.85 BallCalled 5.46 2.30 6.22 7.9
Sinker 88.01 1944.08 241.69 17.46 10.55 BallCalled 5.63 2.26 6.23 8.1
Curveball 76.96 1898.72 46.26 -8.91 -7.07 BallCalled 5.83 1.83 5.70 1.5
Changeup 88.06 1976.36 254.40 18.53 6.36 StrikeCalled 5.53 2.33 6.22 8.5
Curveball 77.47 1851.11 48.40 -10.22 -7.63 InPlay 5.93 1.80 5.84 1.6
Changeup 87.02 1980.65 246.22 18.79 9.46 BallCalled 5.44 2.37 6.03 8.2
Changeup 85.62 2053.24 259.31 19.15 4.88 BallCalled 5.50 2.44 5.89 8.6
Curveball 76.49 1953.01 57.45 -13.22 -7.03 StrikeCalled 5.70 2.13 5.49 1.9
Curveball 76.65 1923.31 58.83 -14.61 -7.32 StrikeCalled 5.69 2.06 5.49 2.0
Changeup 87.30 1905.98 242.35 17.71 10.45 BallCalled 5.62 2.40 5.95 8.1
Curveball 76.65 1857.81 59.91 -13.36 -6.26 BallCalled 5.77 1.91 5.62 2.0
Curveball 76.18 1896.10 54.75 -12.80 -7.57 BallCalled 5.67 2.21 5.77 1.8
Sinker 87.89 2098.92 238.41 17.56 11.97 StrikeCalled 5.56 2.33 6.08 7.9
Sinker 87.71 2026.84 241.16 17.42 10.75 BallCalled 5.52 2.42 6.01 8.0
Curveball 76.92 1881.63 59.52 -13.14 -6.32 StrikeCalled 5.75 2.03 5.58 2.0
Curveball 77.09 1932.62 48.27 -10.98 -8.39 BallCalled 5.60 2.14 5.73 1.6
Sinker 88.56 2044.90 248.58 17.66 8.06 InPlay 5.43 2.37 6.30 8.3
Curveball 78.71 1870.69 38.10 -6.20 -6.54 BallCalled 5.90 1.77 5.60 1.3
Changeup 87.56 1938.46 238.43 17.85 12.11 InPlay 5.65 2.15 6.23 7.9
# 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:  35
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Max Simpson")
Summary Pitch Table for Max Simpson
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 20.00% 7 4 3 57.14% 42.86% 87.32 1977.05 7.98 18.43 250.07 8.3 5.55 2.31 6.06
Curveball 42.86% 15 7 8 46.67% 53.33% 77.39 1906.80 -5.36 -11.94 59.68 2.0 5.75 1.96 5.60
Four-Seam 2.86% 1 0 1 0.00% 100.00% 88.45 2197.90 17.86 13.61 219.23 7.3 5.90 1.95 6.22
Sinker 34.29% 12 7 5 58.33% 41.67% 88.36 2067.30 10.26 17.25 242.26 8.1 5.54 2.32 6.12
# 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("Max Simpson maximum FB velocity: ", max_fb_velocity, "mph\n")
## Max Simpson maximum FB velocity:  89.68 mph
# Prepare data for plotting pitch locations
pitch_location_data <- Max_Simpson_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 Max Simpson",
       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 Max Simpson",
       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")
  )