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

# 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 Johnny Knox")
Detailed Pitch Table for Johnny Knox
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Changeup 80.09 1922.25 246.59 19.49 9.95 BallCalled 3.03 3.41 6.02 8.2
Changeup 77.92 1858.51 255.85 22.12 7.16 BallCalled 2.99 3.40 6.04 8.5
Changeup 79.74 1810.15 250.80 17.44 7.58 StrikeCalled 3.10 3.35 5.80 8.4
Four-Seam 85.83 1959.99 202.87 9.22 23.16 StrikeCalled 5.16 1.85 6.15 6.8
Curveball 69.74 1945.42 102.89 -12.89 4.98 BallCalled 4.19 2.57 5.70 3.4
Changeup 81.30 1987.30 260.79 20.73 4.88 BallCalled 2.95 3.15 5.89 8.7
Changeup 78.93 1937.39 247.36 19.69 9.78 BallCalled 3.02 3.37 5.96 8.2
Changeup 78.06 1840.97 252.06 19.21 7.78 StrikeCalled 3.00 3.20 6.13 8.4
Curveball 69.53 1935.70 136.99 -11.30 14.25 InPlay 3.97 2.66 5.66 4.6
Slider 69.19 1906.89 128.41 -7.14 7.60 BallCalled 4.08 2.59 5.55 4.3
Changeup 78.99 1780.45 258.32 20.77 5.81 BallCalled 2.95 3.20 5.96 8.6
Changeup 79.39 1773.79 260.12 18.64 4.73 StrikeCalled 3.01 3.33 6.09 8.7
Curveball 71.53 1933.25 82.27 -8.91 0.63 InPlay 5.25 1.60 5.92 2.7
Four-Seam 87.14 1971.19 201.76 8.88 23.53 StrikeCalled 5.19 1.54 6.03 6.7
Curveball 71.05 2018.95 146.11 -7.78 13.45 StrikeCalled 4.13 2.47 5.71 4.9
Curveball 71.48 2064.95 120.48 -10.36 7.88 InPlay 4.03 2.49 5.60 4.0
Changeup 78.96 1759.03 246.43 16.65 8.75 BallCalled 3.29 3.07 5.86 8.2
Changeup 76.62 1777.37 251.83 19.39 7.99 StrikeCalled 3.10 3.24 5.84 8.4
Changeup 72.32 1288.17 265.75 12.69 2.79 BallinDirt 3.56 2.97 6.10 8.9
Four-Seam 85.01 2003.32 204.30 9.02 21.26 FoulBallNotFieldable 5.12 1.87 6.21 6.8
Changeup 74.77 1292.68 246.52 17.30 9.14 BallCalled 3.84 2.74 6.23 8.2
Changeup 78.29 1848.27 258.22 20.30 5.89 StrikeSwinging 2.84 3.23 5.93 8.6
Curveball 71.17 2021.78 88.69 -11.13 1.37 BallCalled 5.24 1.60 5.79 3.0
Changeup 80.63 1855.21 265.43 20.33 3.10 BallCalled 3.18 3.30 5.88 8.8
Four-Seam 87.20 2032.69 208.54 11.68 22.73 StrikeCalled 5.39 1.63 6.19 7.0
Curveball 70.94 2012.03 87.01 -10.98 1.35 BallCalled 5.33 1.56 5.72 2.9
Changeup 78.40 1848.83 250.04 18.98 8.47 BallCalled 3.18 3.31 6.20 8.3
Changeup 79.35 1812.51 262.59 21.30 4.32 FoulBallNotFieldable 3.23 3.41 5.65 8.8
Changeup 74.73 1194.41 268.41 13.43 2.08 StrikeSwinging 3.86 2.98 6.20 8.9
Changeup 80.83 1800.25 242.77 16.99 10.29 BallCalled 4.21 4.58 4.82 8.1
Curveball 72.61 2078.21 62.60 -6.82 -1.66 BallinDirt 5.17 1.71 5.84 2.1
Changeup 78.99 1816.55 248.27 18.33 8.70 BallCalled 3.08 3.39 6.14 8.3
Changeup 77.71 1710.73 248.33 18.10 8.74 BallCalled 3.03 3.23 5.97 8.3
Curveball 71.55 2012.99 76.95 -7.76 0.00 BallCalled 5.23 1.42 5.82 2.6
Changeup 80.02 1763.33 253.37 20.73 7.72 StrikeCalled 3.21 3.32 5.90 8.4
Changeup 75.51 1272.54 231.93 10.75 10.05 BallCalled 3.96 2.73 6.09 7.7
Changeup 81.23 1800.10 242.51 19.16 11.50 BallCalled 3.16 3.32 5.79 8.1
Changeup 81.28 1777.18 244.64 18.02 9.99 BallCalled 3.10 3.40 5.81 8.2
Changeup 79.93 1799.06 246.14 19.76 10.24 FoulBallNotFieldable 3.14 3.27 5.85 8.2
Changeup 74.58 1196.55 262.53 16.26 3.74 InPlay 3.84 2.77 6.26 8.8
Changeup 80.72 1858.07 249.94 22.03 9.59 StrikeCalled 3.18 3.10 5.96 8.3
Changeup 81.64 1884.72 258.06 19.71 5.67 InPlay 2.93 3.56 5.90 8.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 %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 Johnny Knox")
Summary Pitch Table for Johnny Knox
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 66.67% 28 16 12 57.14% 42.86% 78.60 1723.80 7.37 18.51 252.70 8.4 3.25 3.26 5.94
Curveball 21.43% 9 5 4 55.56% 44.44% 71.07 2002.59 4.69 -9.77 100.44 3.4 4.73 2.01 5.75
Four-Seam 9.52% 4 0 4 0.00% 100.00% 86.30 1991.80 22.67 9.70 204.37 6.8 5.22 1.72 6.15
Slider 2.38% 1 1 0 100.00% 0.00% 69.19 1906.89 7.60 -7.14 128.41 4.3 4.08 2.59 5.55
# 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("Johnny Knox maximum FB velocity: ", max_fb_velocity, "mph\n")
## Johnny Knox maximum FB velocity:  87.2 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 Johnny Knox",
       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")
  )