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

# 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 Tommy Flaherty")
Detailed Pitch Table for Tommy Flaherty
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Four-Seam 86.68 2222.97 200.56 6.18 17.66 FoulBallNotFieldable 5.96 2.24 5.75 6.7
Curveball 72.32 2387.90 46.45 -14.69 -12.26 StrikeCalled 5.92 2.10 5.00 1.5
Slider 80.05 2296.92 103.86 -10.64 4.01 BallCalled 5.80 2.53 5.03 3.5
Curveball 74.29 2410.79 60.96 -15.22 -6.90 InPlay 5.97 2.17 5.28 2.0
Slider 84.58 2115.61 207.66 5.39 11.48 FoulBallNotFieldable 5.92 2.23 5.81 6.9
Slider 77.40 2356.48 105.76 -13.09 5.49 FoulBallNotFieldable 5.81 2.54 5.01 3.5
Four-Seam 86.69 2248.71 199.22 4.62 14.29 BallCalled 5.94 2.18 5.78 6.6
Cutter 85.17 2240.40 191.11 2.58 14.26 BallCalled 5.90 2.03 5.95 6.4
Cutter 85.51 2288.86 186.62 1.69 15.76 FoulBallNotFieldable 6.08 2.05 5.60 6.2
Four-Seam 84.98 2150.77 200.61 5.92 16.95 InPlay 5.95 2.11 5.88 6.7
Four-Seam 85.03 2159.16 197.63 5.12 17.42 FoulBallNotFieldable 6.01 2.25 5.68 6.6
Cutter 85.39 2188.15 184.21 1.05 15.33 StrikeCalled 5.82 2.05 5.92 6.1
Slider 77.80 2482.27 92.40 -10.02 1.75 StrikeSwinging 5.88 2.10 5.15 3.1
Slider 75.30 2307.12 91.84 -14.46 2.01 BallCalled 5.97 2.13 5.28 3.1
Cutter 85.67 2160.04 176.97 -0.91 18.28 StrikeCalled 5.90 2.27 5.91 5.9
Slider 78.42 2429.58 111.16 -11.95 5.91 BallCalled 5.67 2.51 5.14 3.7
Cutter 86.15 2218.31 192.29 3.14 15.54 StrikeCalled 5.90 2.23 5.99 6.4
Cutter 85.72 2221.03 180.85 0.21 15.02 BallCalled 5.81 2.08 6.03 6.0
Cutter 85.77 2283.46 196.60 4.70 16.91 InPlay 5.87 2.10 6.05 6.6
Curveball 73.36 2471.78 67.21 -19.71 -6.53 BallCalled 6.04 2.07 4.94 2.2
Curveball 72.68 2394.39 60.46 -16.58 -7.67 InPlay 5.92 2.22 5.24 2.0
Curveball 74.29 2435.62 71.90 -19.15 -4.53 BallCalled 5.92 2.01 5.10 2.4
Slider 77.85 2463.38 97.00 -14.66 3.28 StrikeSwinging 5.74 2.28 5.19 3.2
Cutter 85.54 2244.01 191.48 2.82 15.07 StrikeCalled 5.88 2.04 5.77 6.4
Slider 79.51 2443.93 138.24 -9.87 12.48 StrikeSwinging 5.72 2.44 5.15 4.6
Cutter 83.60 2218.52 180.73 0.21 17.48 StrikeCalled 5.82 2.07 5.84 6.0
Curveball 70.57 2427.41 55.65 -16.17 -9.13 BallCalled 5.93 2.13 5.14 1.9
Slider 84.06 2202.73 198.29 3.22 10.95 StrikeCalled 5.79 2.37 5.88 6.6
Slider 78.38 2372.64 111.46 -11.59 6.01 StrikeSwinging 5.67 2.52 5.18 3.7
Cutter 84.87 2297.92 183.93 0.88 13.96 InPlay 5.87 1.92 5.81 6.1
Cutter 84.81 2245.41 185.92 1.54 16.11 BallCalled 5.81 2.19 5.94 6.2
Slider 84.46 2217.77 196.04 2.52 10.04 BallCalled 5.73 2.29 5.84 6.5
Cutter 84.53 2264.10 180.91 0.23 15.84 BallCalled 5.78 2.12 5.81 6.0
Changeup 84.02 2200.36 229.13 14.91 14.18 BallCalled 5.53 2.50 5.64 7.6
Cutter 84.26 2219.81 189.21 2.61 17.36 BallCalled 5.77 2.09 5.76 6.3
Cutter 83.42 2191.29 185.43 1.48 16.80 BallCalled 5.86 2.18 5.62 6.2
Four-Seam 84.04 2158.56 198.18 5.30 17.40 StrikeCalled 5.66 2.25 5.75 6.6
Slider 77.37 2392.42 99.11 -14.57 3.92 BallCalled 5.69 2.15 5.17 3.3
Slider 83.61 2192.37 200.91 4.67 13.49 StrikeCalled 5.74 2.24 5.71 6.7
Slider 76.48 2375.89 122.04 -10.39 7.92 BallCalled 5.75 2.36 5.19 4.1
Cutter 84.08 2208.74 186.33 1.58 15.55 StrikeSwinging 5.77 2.29 5.82 6.2
Four-Seam 83.97 2255.39 201.21 5.92 16.51 BallCalled 5.79 2.22 5.81 6.7
Four-Seam 83.89 2198.27 203.04 6.68 16.94 InPlay 5.91 2.21 5.75 6.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 %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:  43
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Tommy Flaherty")
Summary Pitch Table for Tommy Flaherty
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 2.33% 1 1 0 100.00% 0.00% 84.02 2200.36 14.18 14.91 229.13 7.6 5.53 2.50 5.64
Curveball 13.95% 6 3 3 50.00% 50.00% 72.92 2421.32 -7.84 -16.92 60.44 2.0 5.95 2.12 5.12
Cutter 34.88% 15 6 9 40.00% 60.00% 84.97 2232.67 15.95 1.59 186.17 6.2 5.86 2.11 5.85
Four-Seam 16.28% 7 2 5 28.57% 71.43% 85.04 2199.12 16.74 5.68 200.06 6.7 5.89 2.21 5.77
Slider 32.56% 14 6 8 42.86% 57.14% 79.66 2332.08 7.05 -7.53 133.98 4.5 5.78 2.34 5.34
# 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("Tommy Flaherty maximum FB velocity: ", max_fb_velocity, "mph\n")
## Tommy Flaherty maximum FB velocity:  86.69 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 Tommy Flaherty",
       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")
  )