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

# 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 Harry Meehan")
Detailed Pitch Table for Harry Meehan
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Changeup 79.20 1793.72 156.59 -7.70 19.30 BallCalled 5.54 -0.49 5.48 5.2
Changeup 77.76 1821.26 159.04 -6.57 18.77 BallCalled 5.57 -0.43 5.23 5.3
Changeup 79.26 1911.94 161.35 -6.38 20.45 BallCalled 5.35 -0.51 5.47 5.4
Changeup 78.41 1884.85 155.22 -6.88 16.41 BallCalled 5.42 -0.74 5.52 5.2
Changeup 79.56 1893.59 162.57 -5.05 17.62 BallCalled 5.45 -0.41 5.54 5.4
Changeup 78.20 1835.69 170.64 -2.75 18.31 InPlay 5.54 -0.36 5.30 5.7
Changeup 78.52 1815.00 152.53 -8.54 17.98 BallCalled 5.37 -0.27 5.72 5.1
Changeup 78.03 1881.00 155.97 -7.86 19.18 BallCalled 5.31 -0.45 5.71 5.2
Changeup 77.62 1881.58 148.94 -7.95 14.68 StrikeCalled 5.24 -0.87 5.62 5.0
Changeup 78.29 1831.86 153.00 -7.32 15.87 BallCalled 5.07 -0.78 6.00 5.1
Changeup 77.75 1855.95 147.77 -10.00 17.41 BallCalled 5.38 -0.66 5.59 4.9
Changeup 77.72 1886.29 148.86 -8.22 15.09 FoulBallNotFieldable 5.18 -0.89 5.95 5.0
Curveball 65.92 1974.16 280.15 10.34 0.28 BallCalled 5.22 -0.01 4.95 9.3
Curveball 63.40 1968.58 277.10 11.45 0.97 BallCalled 5.27 -0.27 5.09 9.2
Changeup 77.68 1863.22 149.20 -9.68 17.74 BallCalled 5.15 -0.62 5.72 5.0
Changeup 77.95 1876.22 150.35 -9.00 17.36 FoulBallNotFieldable 5.24 -0.31 5.59 5.0
Curveball 67.94 2058.36 283.90 9.58 -0.49 FoulBallNotFieldable 5.33 -0.21 5.06 9.5
Changeup 78.07 1856.40 155.55 -7.80 18.75 FoulBallNotFieldable 5.28 -0.33 5.60 5.2
Changeup 77.57 1856.06 156.42 -6.15 15.53 FoulBallNotFieldable 5.27 -0.33 5.51 5.2
Curveball 67.20 2060.68 291.17 11.37 -2.18 InPlay 5.27 -0.17 5.18 9.7
Changeup 76.76 1853.05 164.39 -3.22 13.12 StrikeCalled 5.36 -0.19 5.80 5.5
Curveball 67.28 2044.65 287.55 13.62 -1.98 BallCalled 5.36 -0.14 4.98 9.6
Changeup 75.79 1848.31 145.83 -10.49 17.23 InPlay 5.32 -0.21 5.65 4.9
Changeup 76.23 1707.66 149.97 -7.63 14.94 BallCalled 5.37 0.00 5.47 5.0
Changeup 75.86 1821.09 159.20 -5.16 15.31 FoulBallNotFieldable 5.36 -0.24 5.53 5.3
Curveball 66.59 2042.52 320.01 6.36 -5.65 BallCalled 5.14 -0.26 5.43 10.7
Changeup 76.39 1852.59 162.40 -4.54 15.84 BallCalled 5.21 -0.45 5.48 5.4
Changeup 76.97 1893.17 153.62 -5.77 13.16 BallCalled 5.18 -0.27 5.66 5.1
Changeup 77.74 1808.86 171.60 -2.55 18.82 StrikeCalled 5.22 -0.19 5.75 5.7
Changeup 75.71 1789.03 168.88 -2.70 15.35 BallCalled 5.51 0.07 5.58 5.6
Slider 77.04 1918.95 164.51 -3.48 14.06 InPlay 5.23 -0.11 5.85 5.5
Slider 78.65 1925.00 162.19 -4.91 16.74 StrikeCalled 5.35 0.19 5.98 5.4
Curveball 68.06 2120.94 302.32 8.31 -3.29 BallCalled 5.21 -0.05 5.38 10.1
Changeup 76.72 1824.83 162.84 -4.76 16.92 BallCalled 5.29 -0.09 5.95 5.4
Changeup 77.20 1908.35 164.46 -4.73 18.52 FoulBallNotFieldable 5.33 -0.28 5.55 5.5
Changeup 77.61 1818.79 149.11 -6.33 12.15 InPlay 5.42 -0.20 5.52 5.0
Changeup 77.22 1823.15 164.74 -4.86 19.47 StrikeCalled 5.43 -0.05 5.89 5.5
Curveball 66.75 2015.30 309.32 8.89 -5.09 BallCalled 5.29 -0.19 5.20 10.3
Changeup 77.61 1851.88 166.69 -3.78 17.55 BallCalled 5.17 -0.14 6.04 5.6
Changeup 76.30 1829.95 168.93 -2.94 16.76 StrikeCalled 5.32 -0.25 5.44 5.6
Curveball 67.52 2081.16 277.76 12.19 0.44 BallCalled 5.17 -0.24 5.55 9.3
Changeup 77.95 1848.38 163.31 -4.91 18.01 InPlay 5.29 -0.34 5.64 5.4
# 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 %in% c("BallCalled", "BallinDirt")),
    Strikes = sum(!PitchCall %in% c("BallCalled", "BallinDirt")), # 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:  42
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Harry Meehan")
Summary Pitch Table for Harry Meehan
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 73.81% 31 16 15 51.61% 48.39% 77.54 1845.93 16.89 -6.20 158.06 5.3 5.33 -0.36 5.63
Curveball 21.43% 9 7 2 77.78% 22.22% 66.74 2040.71 -1.89 10.23 292.14 9.7 5.25 -0.17 5.20
Slider 4.76% 2 0 2 0.00% 100.00% 77.84 1921.97 15.40 -4.20 163.35 5.4 5.29 0.04 5.92
# Calculate maximum fastball velocity
max_fb_velocity <- detailed_pitch_table %>%
  filter(AutoPitchType %in% c("Four-Seam", "Sinker", "Cutter")) %>%
  summarise(MaxFBVelocity = max(ReleaseSpeed, na.rm = TRUE)) %>%
  pull(MaxFBVelocity)

# Display the maximum fastball velocity
cat("Harry Meehan maximum FB velocity: ", max_fb_velocity, "mph\n")
## Harry Meehan maximum FB velocity:  -Inf 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", "FoulBall", "FoulBallFieldable", "FoulBallNotFieldable", "InPlay"), "Swing", "Take"),
    Chase = ifelse(SwingTake == "Swing" & (PitchSide < -0.85 | PitchSide > 0.85 | 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 Harry Meehan",
       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")
  )