# Filter the data for the pitcher Nate Isler
Nate_Isler_data <- data %>%
  filter(Pitcher == "Isler, Nate")

# Create a detailed table for each pitch
detailed_pitch_table <- Nate_Isler_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 Nate Isler")
Detailed Pitch Table for Nate Isler
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Four-Seam 87.40 2159.75 200.34 7.49 21.56 StrikeCalled 6.52 1.12 5.53 6.7
Curveball 73.81 2384.99 31.94 -11.07 -15.98 BallCalled 6.41 1.24 5.23 1.1
Four-Seam 88.70 2196.49 193.47 5.13 22.76 BallCalled 6.44 1.08 5.61 6.4
Four-Seam 87.03 2119.37 195.99 5.96 22.20 BallCalled 6.49 1.10 5.65 6.5
Four-Seam 86.16 2218.29 204.44 8.92 21.04 BallCalled 6.34 1.23 5.57 6.8
Curveball 73.31 2230.85 35.22 -9.30 -11.21 InPlay 6.30 1.14 4.91 1.2
Changeup 86.33 1812.44 202.52 7.73 19.87 StrikeCalled 6.46 1.00 5.60 6.8
Curveball 75.68 2402.01 21.62 -4.78 -10.37 BallCalled 6.35 1.19 5.10 0.7
Four-Seam 87.68 2049.29 191.22 4.30 22.88 BallCalled 6.55 0.73 5.64 6.4
Four-Seam 87.36 2079.57 193.10 4.55 20.69 StrikeCalled 6.48 1.00 5.59 6.4
Four-Seam 87.80 1963.32 190.55 3.87 21.92 StrikeCalled 6.50 0.70 5.70 6.4
Changeup 87.25 1948.66 213.05 10.37 17.08 BallCalled 6.41 1.27 5.60 7.1
Four-Seam 87.19 2053.76 206.32 9.41 20.24 FoulBallNotFieldable 6.36 1.18 5.57 6.9
Four-Seam 87.01 1960.53 203.26 7.82 19.48 BallCalled 6.36 1.32 5.62 6.8
Four-Seam 86.42 2139.38 206.33 9.86 21.35 BallCalled 6.34 1.34 5.42 6.9
Four-Seam 86.04 2049.78 208.67 10.20 20.03 BallCalled 6.22 1.31 5.86 7.0
Curveball 72.43 2184.24 33.63 -13.34 -17.91 BallCalled 6.24 1.18 5.18 1.1
Four-Seam 86.44 2049.08 190.83 4.17 23.16 StrikeSwinging 6.55 1.20 5.53 6.4
Four-Seam 85.01 2078.03 201.36 8.54 23.27 StrikeSwinging 6.57 1.10 5.56 6.7
Four-Seam 86.76 1936.99 195.56 5.23 20.08 FoulBallNotFieldable 6.50 0.95 5.62 6.5
Four-Seam 87.26 1982.64 192.40 4.39 21.31 StrikeSwinging 6.42 0.97 5.55 6.4
Four-Seam 86.00 1905.89 197.06 5.84 20.39 InPlay 6.56 1.06 5.49 6.6
# 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:  22
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Nate Isler")
Summary Pitch Table for Nate Isler
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 9.09% 2 1 1 50.00% 50.00% 86.79 1880.55 18.48 9.05 207.79 6.9 6.44 1.14 5.60
Curveball 18.18% 4 3 1 75.00% 25.00% 73.81 2300.52 -13.87 -9.62 30.60 1.0 6.32 1.19 5.11
Four-Seam 72.73% 16 7 9 43.75% 56.25% 86.89 2058.89 21.40 6.60 198.18 6.6 6.45 1.09 5.59
# 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("Nate Isler maximum FB velocity: ", max_fb_velocity, "mph\n")
## Nate Isler maximum FB velocity:  88.7 mph
# Prepare data for plotting pitch locations
pitch_location_data <- Nate_Isler_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) +
  geom_rect(aes(xmin = -0.5, xmax = 0.5, ymin = 1.75, ymax = 3.25), fill = NA, color = "red", linetype = "solid", size = 1) + 
  geom_rect(aes(xmin = -0.75, xmax = 0.75, ymin = 1.5, ymax = 3.5), fill = NA, color = "black", linetype = "solid", size = 1) + 
  geom_rect(aes(xmin = -1.25, xmax = 1.25, ymin = 1.25, ymax = 3.75), fill = NA, color = "gray", linetype = "solid", size = 1) +
  scale_x_continuous(limits = c(-2, 2)) +
  scale_y_continuous(limits = c(0, 5)) +
  coord_fixed(ratio = 1) +
  labs(title = "Pitch Locations for Nate Isler",
       x = "Horizontal Location (feet)",
       y = "Vertical Location (feet)",
       color = "Swing/Take",
       shape = "Chase") +
  facet_wrap(~ AutoPitchType) +
  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
  scale_x_continuous(limits = c(-25, 25)) +  # Set horizontal limits to +/- 25 inches
  scale_y_continuous(limits = c(-25, 25)) +  # Set vertical limits to +/- 25 inches
  labs(title = paste("Pitch Movement for", unique(detailed_pitch_table$AutoPitchType)),
       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")
  )