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
Bristol621 <- read_excel("C:/Users/Franco Castagliuolo/OneDrive - Bentley University/Neers 24/Pitchers/Bristol 621/Bristol 621.xlsx")
# Filter the data for the pitcher Kutz
Kutz_data <- Bristol621 %>%
  filter(Pitcher == "Kutz, Charlie")

# Create a detailed table for each pitch
detailed_pitch_table <- Kutz_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 Kutz")
Detailed Pitch Table for Kutz
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Sinker 89.02 2288.88 106.56 -15.46 5.79 StrikeCalled 5.33 -1.94 6.15 3.6
Sinker 89.53 2246.46 104.75 -14.09 4.90 BallCalled 5.33 -2.09 5.68 3.5
Sinker 88.62 2414.26 120.53 -14.12 9.66 FoulBallNotFieldable 5.45 -1.98 6.26 4.0
Curveball 81.48 2719.46 313.28 10.51 -8.43 StrikeCalled 5.16 -2.19 5.62 10.4
Slider 80.28 2650.64 281.85 8.52 -0.43 BallCalled 5.19 -2.16 5.57 9.4
Slider 83.16 2695.69 340.73 1.67 -3.50 StrikeCalled 5.16 -2.19 5.86 11.4
Sinker 88.66 2478.13 82.36 -10.46 -0.31 FoulBallNotFieldable 5.34 -2.16 6.00 2.7
Curveball 82.49 2711.80 328.95 6.10 -8.69 StrikeSwinging 5.10 -2.17 5.70 11.0
Sinker 88.79 2475.24 116.48 -13.16 7.71 BallCalled 5.39 -2.10 6.16 3.9
Sinker 90.24 2505.15 111.45 -16.54 7.75 FoulBallNotFieldable 5.35 -2.09 6.10 3.7
Slider 81.29 2729.93 286.84 8.92 -1.27 InPlay 5.28 -2.15 5.62 9.6
Sinker 89.21 2500.30 108.97 -15.49 6.45 BallCalled 5.35 -2.15 6.09 3.6
Sinker 88.87 2467.31 102.99 -14.79 4.55 BallCalled 5.31 -2.18 6.15 3.4
Sinker 88.78 2388.64 106.05 -16.25 5.82 FoulBallNotFieldable 5.57 -1.95 6.68 3.5
Sinker 89.42 2459.52 118.45 -15.13 9.39 StrikeSwinging 5.47 -2.19 6.37 3.9
Slider 82.17 2741.75 297.67 11.05 -4.45 HitByPitch 5.12 -2.11 5.73 9.9
Changeup 86.70 2405.29 109.86 -18.31 7.91 BallCalled 5.48 -2.02 6.20 3.7
Sinker 88.06 2456.90 114.05 -16.53 8.61 InPlay 5.40 -2.21 6.22 3.8
Changeup 87.26 2333.62 114.44 -16.73 8.87 StrikeSwinging 5.36 -1.90 6.24 3.8
Sinker 88.36 2455.89 125.80 -14.63 11.68 BallCalled 5.30 -2.08 6.04 4.2
Slider 79.88 2526.39 257.42 7.44 2.91 BallCalled 5.23 -2.27 5.88 8.6
Changeup 87.73 2402.04 116.04 -15.81 8.93 InPlay 5.38 -2.16 6.30 3.9
Curveball 79.73 2738.89 293.49 13.00 -4.36 FoulBallNotFieldable 5.23 -2.16 5.54 9.8
Slider 80.15 2797.54 290.43 14.85 -4.21 BallCalled 5.30 -2.23 5.49 9.7
Changeup 87.02 2431.38 109.70 -16.00 6.86 HitByPitch 5.53 -2.06 6.09 3.7
# 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:  25
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Kutz")
Summary Pitch Table for Kutz
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 16.00% 4 1 3 25.00% 75.00% 87.18 2393.08 8.14 -16.71 112.51 3.8 5.44 -2.04 6.21
Curveball 12.00% 3 0 3 0.00% 100.00% 81.23 2723.38 -7.16 9.87 311.91 10.4 5.16 -2.17 5.62
Sinker 48.00% 12 5 7 41.67% 58.33% 88.96 2428.06 6.83 -14.72 109.87 3.6 5.38 -2.09 6.16
Slider 24.00% 6 3 3 50.00% 50.00% 81.16 2690.32 -1.82 8.74 292.49 9.8 5.21 -2.18 5.69
# 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("Kutz maximum FB velocity: ", max_fb_velocity, "mph\n")
## Kutz maximum FB velocity:  90.24 mph
# Prepare data for plotting pitch locations
pitch_location_data <- Kutz_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 Kutz",
       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 Kutz",
       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")
  )