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 Luczak
Luczak_data <- Bristol621 %>%
  filter(Pitcher == "Luczak, Andrew")

# Create a detailed table for each pitch
detailed_pitch_table <- Luczak_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 Luczak")
Detailed Pitch Table for Luczak
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Sinker 88.92 2414.46 228.08 18.54 17.94 HitByPitch 5.38 2.69 5.98 7.6
Sinker 87.46 2280.31 232.83 18.30 15.10 BallCalled 5.20 2.78 5.97 7.8
Curveball 75.27 2401.96 63.05 -9.67 -3.34 BallCalled 5.30 2.77 5.27 2.1
Sinker 88.61 2350.66 240.83 18.13 11.26 BallCalled 5.06 2.79 5.90 8.0
Sinker 89.59 2270.45 241.30 18.93 11.48 StrikeCalled 5.24 2.76 6.01 8.0
Four-Seam 89.78 2474.02 220.23 16.00 20.10 BallCalled 5.20 2.75 5.79 7.3
Four-Seam 87.63 2370.37 220.28 15.28 19.32 StrikeCalled 5.41 2.74 6.08 7.3
Four-Seam 88.47 2468.94 219.28 15.11 19.74 BallCalled 5.35 2.67 6.04 7.3
Slider 76.78 2462.29 76.19 -10.36 -1.08 FoulBallNotFieldable 5.26 2.93 5.50 2.5
Curveball 78.18 2649.05 76.07 -14.45 -1.95 BallCalled 5.26 2.70 5.49 2.5
Sinker 89.09 2448.28 226.93 18.98 19.06 BallCalled 5.42 2.66 5.97 7.6
Sinker 89.61 2458.55 228.27 18.32 17.64 InPlay 5.25 2.78 5.87 7.6
Curveball 76.95 2531.65 72.88 -14.25 -2.75 BallCalled 5.26 2.83 5.32 2.4
Curveball 75.71 2577.60 58.94 -17.27 -8.78 BallCalled 5.25 2.74 5.64 2.0
Sinker 89.01 2339.46 243.42 20.01 11.19 BallCalled 4.97 3.06 5.84 8.1
Sinker 89.00 2399.38 246.76 24.28 11.63 BallCalled 4.89 2.84 5.98 8.2
Curveball 77.89 2606.27 68.55 -15.72 -4.65 BallCalled 5.26 2.78 5.67 2.3
Sinker 89.49 2416.88 249.48 24.46 10.38 StrikeCalled 5.02 2.91 5.83 8.3
Sinker 89.55 2434.89 228.34 15.95 15.32 BallCalled 5.05 2.89 6.09 7.6
Slider 78.39 2648.73 75.38 -12.57 -1.86 StrikeCalled 5.26 2.71 5.64 2.5
Slider 78.05 2650.44 77.12 -19.51 -2.95 FoulBallNotFieldable 5.31 2.66 5.57 2.6
Four-Seam 91.19 2326.52 220.21 15.14 19.04 StrikeSwinging 5.50 2.56 6.03 7.3
Sinker 90.53 2458.21 237.84 19.70 13.57 StrikeSwinging 5.09 2.92 5.64 7.9
Sinker 89.91 2397.45 247.18 20.47 9.78 BallCalled 5.10 2.64 5.96 8.2
Curveball 76.19 2555.17 86.10 -12.35 0.69 BallinDirt 5.32 2.77 5.50 2.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:  25
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Luczak")
Summary Pitch Table for Luczak
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Curveball 24.00% 6 5 1 83.33% 16.67% 76.70 2553.62 -3.46 -13.95 70.93 2.4 5.28 2.77 5.48
Four-Seam 16.00% 4 2 2 50.00% 50.00% 89.27 2409.96 19.55 15.38 220.00 7.3 5.36 2.68 5.98
Sinker 48.00% 12 7 5 58.33% 41.67% 89.23 2389.08 13.70 19.67 237.61 7.9 5.14 2.81 5.92
Slider 12.00% 3 0 3 0.00% 100.00% 77.74 2587.15 -1.96 -14.15 76.23 2.5 5.28 2.77 5.57
# 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("Luczak maximum FB velocity: ", max_fb_velocity, "mph\n")
## Luczak maximum FB velocity:  91.19 mph
# Prepare data for plotting pitch locations
pitch_location_data <- Luczak_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 Luczak",
       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 Luczak",
       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")
  )