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

# Create a detailed table for each pitch
detailed_pitch_table <- William_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 William Jaun")
Detailed Pitch Table for William Jaun
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Cutter 87.91 2195.84 196.53 1.33 5.68 StrikeCalled 5.24 -1.01 5.21 6.6
Sinker 90.05 2265.42 128.72 -28.79 24.37 BallCalled 6.04 -2.01 5.05 4.3
NA 73.13 2360.76 NA NA NA BallCalled 6.08 -1.39 4.80 NA
Four-Seam 88.39 2214.96 139.37 -17.53 21.71 StrikeSwinging 5.80 -1.82 5.30 4.6
Changeup 82.16 1518.25 156.26 -7.43 18.36 BallCalled 5.82 -1.48 5.54 5.2
Sinker 89.09 2194.79 136.33 -16.77 18.87 FoulBallNotFieldable 5.94 -1.82 5.36 4.5
Sinker 89.05 2207.31 134.55 -10.93 12.01 BallCalled 5.54 -1.66 5.46 4.5
Curveball 74.62 2378.27 303.80 8.94 -4.08 BallCalled 5.95 -1.62 5.01 10.1
Four-Seam 88.24 2274.27 137.93 -15.07 18.07 FoulBallNotFieldable 5.72 -1.90 5.27 4.6
Sinker 88.42 2219.09 133.28 -18.03 18.38 StrikeSwinging 5.73 -2.07 5.31 4.4
Sinker 89.56 2344.29 133.23 -14.75 15.15 StrikeCalled 5.62 -1.80 5.44 4.4
NA 88.34 2239.70 NA NA NA StrikeCalled 6.00 -1.90 5.27 NA
Curveball 75.78 2401.80 328.36 8.35 -11.87 StrikeCalled 5.79 -1.58 5.20 10.9
Four-Seam 88.45 2235.30 144.83 -11.64 17.85 BallCalled 5.78 -1.86 5.45 4.8
Curveball 74.09 2333.17 285.83 7.33 -0.17 BallCalled 6.05 -1.57 5.27 9.5
Four-Seam 88.85 2261.85 146.19 -14.31 22.76 StrikeSwinging 5.92 -1.80 5.33 4.9
Four-Seam 89.50 2246.81 138.94 -15.71 19.37 StrikeCalled 5.75 -2.00 5.41 4.6
Curveball 75.53 2422.43 298.05 15.01 -6.22 StrikeCalled 5.79 -1.53 5.01 9.9
Four-Seam 89.73 2298.86 135.59 -17.16 18.88 FoulBallNotFieldable 5.81 -2.12 5.26 4.5
NA 76.62 2525.65 NA NA NA FoulBallNotFieldable 6.28 -1.39 4.90 NA
Sinker 89.40 2291.93 124.30 -19.02 14.19 InPlay 5.71 -2.08 5.54 4.1
Sinker 87.93 2070.93 121.72 -19.20 13.03 BallCalled 5.80 -1.91 5.28 4.1
Sinker 88.60 2305.77 130.97 -16.48 15.45 BallCalled 5.65 -1.84 5.36 4.4
Curveball 73.79 2297.41 298.29 14.06 -5.95 StrikeCalled 5.87 -1.35 5.23 9.9
Sinker 87.77 2192.58 127.02 -14.88 12.42 StrikeSwinging 5.63 -1.95 5.45 4.2
Four-Seam 90.27 2266.78 136.32 -15.88 17.82 StrikeSwinging 5.79 -1.92 5.20 4.5
# 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:  26
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for William Jaun")
Summary Pitch Table for William Jaun
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 3.85% 1 1 0 100.00% 0.00% 82.16 1518.25 18.36 -7.43 156.26 5.2 5.82 -1.48 5.54
Curveball 19.23% 5 2 3 40.00% 60.00% 74.76 2366.62 -5.66 10.74 302.87 10.1 5.89 -1.53 5.14
Cutter 3.85% 1 0 1 0.00% 100.00% 87.91 2195.84 5.68 1.33 196.53 6.6 5.24 -1.01 5.21
Four-Seam 26.92% 7 1 6 14.29% 85.71% 89.06 2256.98 19.49 -15.33 139.88 4.6 5.80 -1.92 5.32
Sinker 34.62% 9 4 5 44.44% 55.56% 88.87 2232.46 15.99 -17.65 130.01 4.3 5.74 -1.90 5.36
NA 11.54% 3 1 2 33.33% 66.67% 79.36 2375.37 NaN NaN NaN NaN 6.12 -1.56 4.99
# 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("William Jaun maximum FB velocity: ", max_fb_velocity, "mph\n")
## William Jaun maximum FB velocity:  90.27 mph
# Prepare data for plotting pitch locations
pitch_location_data <- William_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 William Jaun",
       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 William 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")
  )