# 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 89.45 2120.79 247.40 16.96 8.14 BallCalled 5.06 2.04 5.74 8.2
Sinker 89.89 2177.93 234.49 19.16 14.84 InPlay 5.32 1.73 5.61 7.8
Curveball 77.44 2141.69 58.96 -6.15 -2.26 StrikeCalled 4.97 2.00 5.54 2.0
Sinker 90.46 2083.00 251.00 18.61 7.47 FoulBallNotFieldable 5.14 1.96 5.66 8.4
Slider 77.15 2124.68 124.47 -4.78 4.82 StrikeSwinging 4.99 1.98 5.61 4.1
Slider 77.64 2126.42 121.95 -4.81 4.40 StrikeCalled 4.93 1.97 5.54 4.1
Sinker 91.58 2261.38 244.12 19.31 10.40 BallCalled 5.26 1.75 NA 8.1
Curveball 77.05 2210.85 66.94 -6.90 -1.51 StrikeCalled 4.95 2.06 5.51 2.2
Slider 78.45 2186.46 138.90 -3.54 5.44 BallinDirt 5.08 1.90 5.59 4.6
Sinker 90.66 2132.32 237.41 17.56 12.29 StrikeCalled 5.23 1.92 5.71 7.9
Sinker 91.98 2245.26 254.27 20.92 6.93 FoulBallNotFieldable 5.15 2.06 5.51 8.5
Sinker 90.65 2140.83 254.02 19.01 6.57 BallCalled 5.27 2.04 5.33 8.5
Slider 76.45 2127.06 106.39 -3.74 2.66 FoulBallNotFieldable 5.10 2.02 5.57 3.5
Sinker 91.18 2141.83 252.94 19.13 7.02 BallCalled 5.27 1.85 5.42 8.4
Sinker 90.93 2180.03 249.76 20.05 8.51 FoulBallNotFieldable 5.17 2.01 5.42 8.3
Curveball 76.13 1887.22 86.17 -3.13 1.30 FoulBallNotFieldable 5.00 2.06 5.77 2.9
Sinker 90.83 2163.76 225.70 14.65 15.42 FoulBallNotFieldable 5.35 1.83 5.25 7.5
Curveball 77.39 2150.86 107.14 -7.39 3.89 BallCalled 4.95 2.19 5.49 3.6
Sinker 90.15 2147.08 245.49 18.67 9.67 InPlay 5.17 2.12 5.64 8.2
Curveball 75.67 2098.28 67.02 -6.16 -0.70 BallCalled 4.98 1.69 5.57 2.2
Sinker 88.82 2079.64 254.96 18.33 6.14 StrikeSwinging 5.13 1.60 5.69 8.5
Curveball 76.50 2178.44 68.50 -6.48 -0.74 StrikeCalled 5.01 1.63 5.51 2.3
Slider 76.43 2114.96 106.27 -2.97 2.53 StrikeSwinging 4.98 1.55 5.57 3.5
Curveball 76.34 2065.05 68.84 -5.26 -0.22 BallCalled 5.02 1.61 5.60 2.3
Sinker 90.24 2150.24 260.43 19.16 4.36 StrikeSwinging 5.19 1.46 5.83 8.7
Sinker 89.97 2099.90 249.79 18.52 7.98 BallCalled 5.19 1.39 5.92 8.3
Curveball 75.60 2146.37 75.17 -8.98 -0.70 StrikeCalled 4.99 1.59 5.37 2.5
Curveball 76.43 2090.53 65.14 -8.92 -2.32 FoulBallNotFieldable 4.95 1.70 5.38 2.2
Sinker 90.19 2123.54 227.73 16.30 15.89 StrikeSwinging 5.18 1.63 5.51 7.6
Sinker 90.46 2057.50 251.47 17.22 6.81 BallCalled 5.17 1.63 5.41 8.4
Curveball 76.56 2180.55 88.79 -10.32 1.25 BallCalled 5.09 1.75 5.34 3.0
Sinker 89.82 2098.03 254.80 19.49 6.33 BallCalled 5.23 1.69 5.99 8.5
Sinker 87.89 2087.54 228.63 15.24 14.50 BallCalled 5.06 1.76 5.70 7.6
Sinker 89.65 2106.05 248.31 16.62 7.74 StrikeCalled 5.20 1.86 5.80 8.3
Slider 77.83 2050.79 94.61 -5.66 2.14 StrikeCalled 5.11 1.66 5.51 3.2
Slider 74.56 2059.05 113.77 -3.84 3.24 BallCalled 5.09 1.70 5.62 3.8
Sinker 88.42 2049.63 253.02 16.70 6.20 BallCalled 5.17 1.66 5.67 8.4
Sinker 87.87 2095.74 228.86 14.44 13.85 BallCalled 5.16 1.58 5.66 7.6
Curveball 76.19 2126.74 65.07 -6.03 -1.22 FoulBallNotFieldable 4.93 1.48 5.75 2.2
Sinker 88.21 2116.90 220.41 12.14 15.52 FoulBallNotFieldable 5.13 1.67 5.99 7.3
Sinker 90.39 2238.29 230.19 16.43 14.89 InPlay 5.16 1.56 5.64 7.7
Curveball 75.16 2038.83 82.79 -6.61 0.90 StrikeCalled 5.03 1.89 5.51 2.8
Sinker 88.85 2092.34 254.55 17.65 6.00 StrikeCalled 5.04 1.95 5.79 8.5
Sinker 87.68 2110.57 232.19 15.68 13.34 BallCalled 5.02 1.77 5.88 7.7
Sinker 91.33 2249.50 230.99 17.50 15.30 BallCalled 5.33 1.29 5.59 7.7
Curveball 75.70 1973.11 104.18 -3.35 2.32 BallCalled 5.08 1.32 5.54 3.5
Sinker 90.34 2081.01 255.68 18.31 5.75 StrikeSwinging 5.19 1.71 5.59 8.5
Sinker 89.74 2110.95 231.70 17.50 14.99 BallCalled 5.08 1.72 5.74 7.7
Sinker 89.27 2237.17 233.05 18.34 14.98 FoulBallNotFieldable 5.28 1.54 5.73 7.8
Curveball 75.84 2120.18 77.22 -4.76 0.54 FoulBallNotFieldable 5.09 1.59 5.50 2.6
Curveball 75.79 1998.30 80.19 -5.23 0.84 BallCalled 5.15 1.48 5.58 2.7
Sinker 90.40 2158.76 226.08 14.36 14.97 InPlay 5.17 1.67 5.71 7.5
Sinker 89.97 2064.34 254.51 18.23 6.14 BallCalled 5.17 1.67 5.62 8.5
Sinker 89.91 2243.44 232.00 15.24 13.02 FoulBallNotFieldable 5.33 1.63 5.74 7.7
Curveball 75.71 2125.34 66.70 -5.48 -0.97 BallCalled 5.11 1.65 5.60 2.2
Sinker 89.54 2095.67 245.77 20.12 10.25 BallCalled 5.14 1.79 5.82 8.2
Sinker 89.37 2144.10 255.98 22.58 6.87 InPlay 5.16 1.72 5.64 8.5
Curveball 75.54 2075.17 75.57 -9.36 -0.72 InPlay 4.98 1.64 5.58 2.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:  58
# 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 29.31% 17 7 10 41.18% 58.82% 76.18 2094.56 -0.02 -6.50 76.73 2.6 5.02 1.73 5.54
Sinker 58.62% 34 15 19 44.12% 55.88% 89.89 2137.80 10.27 17.65 242.87 8.1 5.18 1.74 5.67
Slider 12.07% 7 1 6 14.29% 85.71% 76.93 2112.77 3.60 -4.19 115.19 3.8 5.04 1.83 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.98 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.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 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")
  )