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 Charlie Kutz
Charlie_Kutz_data <- Upper626 %>%
  filter(Pitcher == "Kutz, Charlie")

# Create a detailed table for each pitch
detailed_pitch_table <- Charlie_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 Charlie Kutz")
Detailed Pitch Table for Charlie Kutz
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Changeup 88.66 2496.26 92.14 -20.18 1.95 StrikeCalled 5.63 -1.90 5.57 3.1
Sinker 89.19 2371.82 90.19 -17.33 1.30 BallCalled 5.30 -1.99 5.59 3.0
Sinker 88.44 2343.85 118.75 -17.64 11.03 InPlay 5.59 -1.84 5.45 4.0
Sinker 87.90 2434.41 108.47 -16.64 6.68 StrikeCalled 5.45 -2.00 5.35 3.6
Sinker 88.76 2433.30 102.69 -17.92 5.20 BallCalled 5.29 -1.90 6.06 3.4
Sinker 88.37 2419.57 111.00 -15.68 7.23 InPlay 5.47 -1.95 5.45 3.7
Sinker 89.73 2438.12 106.66 -18.71 6.81 StrikeCalled 5.56 -1.94 5.07 3.6
Sinker 90.64 2363.55 120.88 -16.78 11.28 BallCalled 5.45 -1.81 5.36 4.0
NA 80.12 2579.68 NA NA NA BallCalled 5.60 -1.94 4.96 NA
Four-Seam 87.91 2506.32 130.44 -11.95 11.31 BallCalled 5.61 -1.91 5.25 4.3
Sinker 87.39 2246.32 104.29 -9.65 3.60 StrikeCalled 5.58 -1.72 5.56 3.5
NA 82.06 2511.94 NA NA NA BallCalled 5.74 -1.90 5.07 NA
NA 87.99 2292.68 NA NA NA HitByPitch 5.86 -1.89 5.41 NA
Changeup 86.81 2276.26 116.37 -12.14 7.26 BallCalled 5.59 -1.61 5.49 3.9
Sinker 88.32 2450.80 127.45 -12.32 10.58 BallCalled 5.60 -1.81 5.41 4.2
Sinker 87.69 2409.80 108.95 -12.16 5.38 BallCalled 5.53 -1.68 5.52 3.6
Sinker 88.31 2436.13 117.13 -13.11 7.84 StrikeCalled 5.60 -1.68 5.58 3.9
Changeup 87.54 2357.86 113.98 -15.37 8.07 InPlay 5.58 -1.73 5.42 3.8
# 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:  18
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Charlie Kutz")
Summary Pitch Table for Charlie Kutz
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 16.67% 3 1 2 33.33% 66.67% 87.67 2376.79 5.76 -15.90 107.50 3.6 5.60 -1.75 5.49
Four-Seam 5.56% 1 1 0 100.00% 0.00% 87.91 2506.32 11.31 -11.95 130.44 4.3 5.61 -1.91 5.25
Sinker 61.11% 11 5 6 45.45% 54.55% 88.61 2395.24 6.99 -15.27 110.59 3.7 5.49 -1.85 5.49
NA 16.67% 3 2 1 66.67% 33.33% 83.39 2461.43 NaN NaN NaN NaN 5.73 -1.91 5.15
# 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("Charlie Kutz maximum FB velocity: ", max_fb_velocity, "mph\n")
## Charlie Kutz maximum FB velocity:  90.64 mph
# Prepare data for plotting pitch locations
pitch_location_data <- Charlie_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 Charlie 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 Charlie 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")
  )