# Filter the data for the pitcher Jacob Lapham
Brian_Foley_data <- data %>%
  filter(Pitcher == "Lapham, Jacob")

# Create a detailed table for each pitch
detailed_pitch_table <- Brian_Foley_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 Jacob Lapham")
Detailed Pitch Table for Jacob Lapham
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Sinker 90.38 2200.24 256.36 22.36 6.67 StrikeCalled 5.05 1.57 5.63 8.5
Curveball 77.54 2073.56 158.25 -2.58 8.31 StrikeSwinging 4.93 1.57 5.67 5.3
Sinker 91.34 2098.21 226.81 16.28 16.43 FoulBallNotFieldable 5.24 1.38 5.83 7.6
Curveball 77.03 2062.52 95.71 -7.96 2.57 StrikeCalled 5.01 1.54 5.68 3.2
Curveball 77.11 2173.86 79.85 -5.46 0.73 BallCalled 4.94 1.40 5.72 2.7
Sinker 90.69 2141.24 229.34 18.98 17.62 StrikeCalled 5.29 1.31 5.57 7.6
Curveball 77.15 2005.30 91.85 -6.53 2.03 FoulBallNotFieldable 5.09 1.50 5.64 3.1
Sinker 90.73 2191.99 233.00 15.90 13.35 StrikeCalled 5.20 1.50 5.65 7.8
Sinker 91.02 2097.00 248.31 16.74 7.86 BallCalled 5.06 1.60 5.77 8.3
Slider 77.99 2028.70 92.35 -4.56 2.25 BallCalled 5.04 1.38 5.57 3.1
Sinker 89.80 2063.23 242.27 17.99 10.72 BallCalled 5.14 1.57 5.63 8.1
Sinker 88.60 2020.32 223.62 13.75 15.74 BallCalled 5.10 1.66 5.73 7.5
Sinker 89.31 2061.19 247.11 16.47 8.24 BallCalled 5.16 1.62 5.66 8.2
Sinker 89.11 2010.52 223.27 14.55 16.82 StrikeCalled 5.31 1.52 5.73 7.4
Sinker 89.82 2003.86 249.99 19.21 8.25 InPlay 5.15 1.63 5.67 8.3
Sinker 89.56 2008.98 246.86 18.94 9.38 BallCalled 5.12 1.78 5.51 8.2
Sinker 89.40 2021.28 245.22 20.25 10.61 FoulBallNotFieldable 5.16 1.68 5.74 8.2
Curveball 77.43 2030.11 125.02 -6.01 5.92 BallCalled 4.98 1.54 5.59 4.2
Sinker 89.99 2145.58 237.23 18.23 13.06 BallCalled 5.13 1.65 5.51 7.9
Sinker 89.86 2084.60 225.91 14.60 15.41 InPlay 5.23 1.61 5.86 7.5
Curveball 77.26 1949.31 95.80 -3.30 2.22 StrikeSwinging 4.90 1.41 5.71 3.2
Curveball 76.23 1926.45 122.66 -4.52 4.90 StrikeCalled 4.95 1.51 5.62 4.1
Sinker 89.35 2158.10 235.42 17.50 13.29 StrikeCalled 5.23 1.44 5.66 7.8
Slider 75.10 2126.47 109.99 -4.39 3.29 StrikeCalled 5.09 1.63 5.57 3.7
Curveball 74.24 1931.96 91.59 -2.35 1.80 BallCalled 5.06 1.40 5.56 3.1
Sinker 88.49 1964.13 245.08 17.12 9.16 BallCalled 5.16 1.52 5.77 8.2
Curveball 73.74 2081.80 80.69 -4.90 0.98 BallCalled 5.07 1.38 5.56 2.7
Sinker 88.83 1894.44 224.59 12.61 14.05 InPlay 5.11 1.46 5.80 7.5
Curveball 75.36 2143.97 98.36 -9.77 3.24 StrikeCalled 5.01 1.35 5.49 3.3
Sinker 87.80 2081.46 230.34 17.76 16.02 BallCalled 5.20 1.40 5.60 7.7
Slider 75.20 2143.05 84.48 -4.50 1.29 BallinDirt 4.96 1.47 5.57 2.8
Sinker 87.90 2029.86 248.03 19.56 9.10 BallCalled 5.07 1.60 5.55 8.3
Sinker 88.18 2103.76 232.76 17.05 14.17 InPlay 5.14 1.57 5.76 7.8
Sinker 87.62 2065.97 253.30 19.09 6.99 BallinDirt 5.02 1.75 5.68 8.4
Sinker 88.08 2109.86 235.39 19.07 14.53 InPlay 5.14 1.59 5.67 7.8
Sinker 89.27 2129.70 260.84 21.26 4.68 StrikeCalled 5.06 1.67 5.50 8.7
Sinker 88.12 2083.55 234.19 19.59 15.40 InPlay 5.09 1.64 5.72 7.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:  37
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Jacob Lapham")
Summary Pitch Table for Jacob Lapham
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Curveball 27.03% 10 4 6 40.00% 60.00% 76.31 2037.88 3.27 -5.34 103.98 3.5 4.99 1.46 5.62
Sinker 64.86% 24 9 15 37.50% 62.50% 89.30 2073.71 11.98 17.70 238.97 8.0 5.15 1.57 5.68
Slider 8.11% 3 1 2 33.33% 66.67% 76.10 2099.41 2.28 -4.48 95.61 3.2 5.03 1.49 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("Jacob Lapham maximum FB velocity: ", max_fb_velocity, "mph\n")
## Jacob Lapham maximum FB velocity:  91.34 mph
# Prepare data for plotting pitch locations
pitch_location_data <- Brian_Foley_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.85, xmax = 0.85, 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 Jacob Lapham",
       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"),
       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")
  )