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 Jaun
Jaun_data <- Keene614 %>%
  filter(Pitcher == "Jaun, William")

# Create a detailed table for each pitch
detailed_pitch_table <- Jaun_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 Jaun")
Detailed Pitch Table for Jaun
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
NA NA NA NA NA NA StrikeCalled NA NA NA NA
Four-Seam 87.92 2259.40 141.07 -16.19 21.35 BallCalled 5.86 -1.90 5.67 4.7
Four-Seam 88.10 2328.39 142.52 -14.21 19.84 FoulBallNotFieldable 5.74 -2.06 5.95 4.8
Four-Seam 88.67 2277.58 147.04 -12.87 21.09 InPlay 5.73 -1.98 5.95 4.9
Curveball 75.35 2428.74 304.74 6.05 -2.62 StrikeCalled 5.86 -1.96 5.51 10.2
Four-Seam 89.04 2280.76 145.69 -13.03 20.31 StrikeSwinging 5.70 -2.00 6.11 4.9
Curveball 77.25 2486.79 294.36 7.05 -1.59 BallinDirt 5.88 -1.61 5.64 9.8
Four-Seam 88.77 2350.25 143.01 -15.38 21.65 StrikeSwinging 5.83 -2.10 6.14 4.8
Curveball 74.36 2445.23 293.58 10.72 -3.01 BallinDirt 5.99 -1.61 5.44 9.8
Four-Seam 87.01 2258.87 145.31 -13.94 21.41 StrikeCalled 5.69 -2.09 5.78 4.8
Curveball 75.01 2373.66 283.97 11.50 -1.24 BallinDirt 5.96 -1.66 5.36 9.5
Four-Seam 88.60 2299.66 138.50 -15.40 18.64 StrikeSwinging 5.74 -1.98 5.93 4.6
Four-Seam 89.81 2397.31 141.41 -14.83 19.78 BallCalled 5.79 -2.05 5.89 4.7
Curveball 74.16 2339.90 292.56 8.52 -1.91 BallCalled 5.92 -1.69 5.64 9.8
Four-Seam 87.27 2276.24 148.23 -12.03 20.72 FoulBallNotFieldable 5.75 -2.16 5.86 4.9
Four-Seam 88.64 2353.12 135.82 -15.41 17.14 BallCalled 5.71 -2.03 5.99 4.5
Four-Seam 87.57 2236.18 138.08 -14.21 17.19 InPlay 5.63 -2.13 6.11 4.6
Curveball 75.88 2446.25 301.25 8.37 -3.38 BallinDirt 5.79 -1.85 5.64 10.0
Sinker 86.48 2234.80 137.12 -14.80 17.34 FoulBallNotFieldable 5.61 -2.09 5.94 4.6
Sinker 88.17 2259.39 130.47 -15.18 14.17 InPlay 5.66 -2.16 5.95 4.3
Curveball 75.64 2423.36 287.86 7.09 -0.75 StrikeCalled 5.98 -1.89 5.67 9.6
Slider 77.60 2293.90 244.92 10.19 6.40 BallinDirt 5.92 -1.77 5.55 8.2
Curveball 74.21 2386.47 297.60 11.22 -4.10 StrikeCalled 5.91 -1.62 5.52 9.9
Four-Seam 87.76 2327.83 143.95 -13.61 20.04 FoulBallNotFieldable 5.85 -1.92 5.86 4.8
Four-Seam 88.58 2219.53 143.13 -13.75 19.72 InPlay 5.95 -1.84 5.73 4.8
Sinker 86.37 2228.48 136.94 -14.47 16.88 StrikeCalled 5.92 -2.07 5.75 4.6
Four-Seam 87.64 2345.34 143.08 -14.53 20.71 FoulBallNotFieldable 5.89 -1.81 5.75 4.8
Curveball 76.12 2465.76 295.47 9.63 -3.02 StrikeCalled 5.95 -1.66 5.59 9.8
Sinker 87.71 2279.33 135.28 -14.14 15.47 BallCalled 5.75 -2.00 6.01 4.5
Sinker 86.52 2183.66 138.84 -14.94 18.29 FoulBallNotFieldable 5.68 -2.08 6.00 4.6
Curveball 75.77 2377.20 304.49 13.03 -7.33 BallinDirt 5.87 -1.78 5.49 10.1
Four-Seam 87.85 2327.12 134.16 -16.69 17.50 InPlay 5.74 -2.00 5.96 4.5
Four-Seam 87.70 2329.54 139.51 -16.21 20.30 StrikeCalled 5.76 -2.31 6.08 4.7
Curveball 75.56 2419.78 287.64 13.54 -2.72 StrikeCalled 5.88 -1.90 5.68 9.6
Four-Seam 89.78 2491.93 134.89 -18.24 19.43 BallCalled 5.88 -2.06 5.87 4.5
Four-Seam 89.44 2277.85 135.86 -18.15 19.96 FoulBallNotFieldable 5.75 -2.05 6.03 4.5
Curveball 76.96 2494.53 311.59 10.06 -7.46 InPlay 5.96 -1.78 5.79 10.4
Sinker 86.50 2233.51 136.95 -16.12 18.49 FoulBallNotFieldable 5.84 -2.11 5.77 4.6
Changeup 78.98 1386.34 138.37 -7.80 10.14 StrikeSwinging 5.79 -1.95 6.07 4.6
Changeup 80.28 1412.80 122.48 -9.54 7.42 BallinDirt 5.84 -1.88 6.07 4.1
Four-Seam 87.61 2175.62 138.36 -13.66 16.46 StrikeCalled 5.82 -1.97 5.77 4.6
Curveball 73.96 2336.88 311.99 10.72 -8.12 HitByPitch 5.88 -1.93 5.18 10.4
Sinker 85.72 2178.80 137.83 -16.36 19.34 StrikeCalled 5.92 -2.16 5.71 4.6
Curveball 76.30 2373.67 319.24 5.06 -4.47 BallinDirt 5.83 -1.79 5.76 10.6
Four-Seam 87.93 2292.92 141.63 -15.10 20.29 InPlay 5.76 -2.15 5.97 4.7
Sinker 86.71 2315.50 136.49 -17.00 19.19 InPlay 5.85 -2.23 5.71 4.5
Curveball 75.06 2330.17 290.19 8.89 -1.60 BallCalled 5.91 -1.79 5.40 9.7
Sinker 87.30 2241.24 130.82 -17.62 16.57 InPlay 5.71 -2.13 5.76 4.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\n")
## Total number of pitches thrown:  48
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Jaun")
Summary Pitch Table for Jaun
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 4.17% 2 0 2 0.00% 100.00% 79.63 1399.57 8.78 -8.67 130.43 4.3 5.81 -1.92 6.07
Curveball 31.25% 15 2 13 13.33% 86.67% 75.44 2408.56 -3.55 9.43 298.44 9.9 5.90 -1.77 5.55
Four-Seam 41.67% 20 4 16 20.00% 80.00% 88.28 2305.27 19.68 -14.87 141.06 4.7 5.78 -2.03 5.92
Sinker 18.75% 9 1 8 11.11% 88.89% 86.83 2239.41 17.30 -15.63 135.64 4.5 5.77 -2.11 5.84
Slider 2.08% 1 0 1 0.00% 100.00% 77.60 2293.90 6.40 10.19 244.92 8.2 5.92 -1.77 5.55
NA 2.08% 1 0 1 0.00% 100.00% NaN NaN NaN NaN NaN NaN NaN NaN NaN
# 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("Jaun maximum FB velocity: ", max_fb_velocity, "mph\n")
## Jaun maximum FB velocity:  89.81 mph
# Prepare data for plotting pitch locations
pitch_location_data <- Jaun_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 Jaun",
       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 Jaun",
       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")
  )