# 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.53 2082.45 254.93 22.00 7.30 StrikeSwinging 3.11 3.56 5.28 8.5
Four-Seam 86.07 2053.44 206.76 9.25 19.57 BallCalled 5.42 1.73 5.54 6.9
Changeup 79.50 2016.14 257.13 16.53 5.25 FoulBallNotFieldable 3.05 3.53 5.27 8.6
Curveball 71.92 1979.05 126.43 -10.43 9.39 InPlay 4.15 2.65 4.96 4.2
Four-Seam 86.99 1998.76 200.12 7.10 20.72 BallCalled 5.37 1.71 5.55 6.7
Changeup 78.69 1707.04 263.46 20.57 3.93 FoulBallFieldable 2.91 3.53 5.27 8.8
Curveball 70.46 2013.98 127.55 -12.00 11.03 BallCalled 3.98 2.74 4.77 4.3
Changeup 79.63 1846.29 266.46 16.13 2.38 FoulBallNotFieldable 2.92 3.43 5.02 8.9
Changeup 83.86 1924.32 226.84 15.04 15.43 BallCalled 4.57 2.60 5.09 7.6
Changeup 80.41 1896.43 265.06 16.13 2.89 FoulBallFieldable 3.01 3.51 5.22 8.8
Curveball 71.84 2090.37 130.26 -13.14 13.24 BallCalled 3.90 2.76 4.85 4.3
Changeup 79.93 1931.70 261.23 17.40 4.12 BallCalled 3.10 3.25 5.59 8.7
Changeup 79.84 1830.10 254.47 20.74 7.35 StrikeCalled 3.01 3.49 5.13 8.5
Changeup 74.44 1257.20 279.98 14.99 -0.90 InPlay 4.01 2.71 5.38 9.3
Changeup 81.49 1851.74 254.63 22.74 7.70 StrikeCalled 3.11 3.35 5.15 8.5
Four-Seam 85.06 2061.48 205.58 9.06 20.21 BallCalled 5.34 1.85 5.24 6.9
Changeup 81.42 1896.00 252.70 19.76 7.56 FoulBallNotFieldable 3.07 3.41 5.32 8.4
Curveball 71.27 2206.75 94.15 -9.44 2.40 BallCalled 4.09 2.76 5.17 3.1
Four-Seam 84.53 2099.14 204.84 10.09 23.08 FoulBallNotFieldable 5.26 1.71 5.62 6.8
Changeup 82.23 1785.83 269.18 20.35 1.63 FoulBallNotFieldable 3.07 3.48 5.20 9.0
Changeup 76.68 1281.27 270.02 14.13 1.51 StrikeSwinging 3.94 2.77 5.76 9.0
Changeup 82.06 1974.10 234.84 19.36 15.16 BallCalled 4.72 3.72 4.88 7.8
Changeup 81.99 1987.58 253.14 22.31 8.28 StrikeCalled 3.04 3.63 5.20 8.4
Changeup 78.98 1907.87 272.44 24.42 0.55 StrikeCalled 2.71 3.53 5.13 9.1
Changeup 76.29 1275.83 271.29 13.44 1.27 BallCalled 3.85 2.82 5.65 9.0
Changeup 83.52 1973.25 261.96 23.37 4.62 StrikeSwinging 3.05 3.76 5.01 8.7
Changeup 79.67 1887.21 250.61 18.43 7.98 StrikeCalled 3.04 3.58 5.11 8.4
Curveball 69.88 1958.53 58.63 -9.34 -3.78 BallCalled 5.16 1.63 4.99 2.0
Changeup 80.18 1902.61 269.85 18.93 1.56 FoulBallNotFieldable 2.93 3.43 5.13 9.0
Slider 70.55 2081.83 127.55 -6.66 6.81 StrikeSwinging 3.92 2.70 4.94 4.3
Changeup 82.54 1818.61 232.05 15.03 13.09 StrikeSwinging 4.58 4.24 4.51 7.7
Changeup 77.15 1174.64 278.69 10.90 -0.20 StrikeCalled 3.86 2.76 5.47 9.3
Changeup 76.81 1903.69 279.30 18.65 -1.45 InPlay 2.11 3.87 5.07 9.3
Changeup 80.80 1574.09 255.68 13.63 4.80 StrikeSwinging 3.00 3.47 5.21 8.5
Changeup 74.25 1142.37 270.11 11.64 1.62 InPlay 3.63 2.81 5.41 9.0
Changeup 80.34 1836.69 263.66 21.10 3.84 StrikeSwinging 2.98 3.48 5.21 8.8
Changeup 80.95 1948.50 257.60 18.82 5.58 FoulBallNotFieldable 3.02 3.55 5.30 8.6
Changeup 76.59 1296.88 247.24 17.75 8.96 InPlay 3.98 2.87 5.66 8.2
Changeup 85.58 1940.47 229.49 15.38 14.46 FoulBallNotFieldable 5.12 2.05 5.27 7.6
Changeup 81.38 1995.48 256.55 21.88 6.73 StrikeCalled 2.95 3.38 5.30 8.6
Changeup 81.12 1941.16 259.68 21.82 5.45 BallCalled 2.97 3.44 5.28 8.7
Curveball 72.86 2116.96 119.61 -10.14 7.46 BallCalled 3.99 2.65 5.03 4.0
Changeup 81.08 1924.67 254.91 22.24 7.42 InPlay 2.86 3.69 5.06 8.5
Changeup 78.94 1789.50 256.95 21.29 6.43 InPlay 3.20 3.54 4.99 8.6
Changeup 83.00 1881.82 208.06 8.61 17.41 BallCalled 5.53 2.30 4.82 6.9
Changeup 78.18 1727.08 256.24 16.09 5.36 StrikeCalled 3.24 3.42 4.91 8.5
Changeup 74.23 1289.15 283.50 11.58 -1.14 BallCalled 3.82 2.83 5.27 9.4
Changeup 77.78 1817.17 253.97 18.29 6.75 FoulBallNotFieldable 3.07 3.59 4.78 8.5
Changeup 73.90 1242.92 276.47 11.80 0.50 BallCalled 3.71 2.88 5.28 9.2
Changeup 79.62 1768.24 265.78 17.20 2.79 FoulBallNotFieldable 2.98 3.58 4.99 8.9
# 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:  50
# 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 78.00% 39 8 31 20.51% 79.49% 79.63 1749.44 5.49 17.70 258.36 8.6 3.41 3.30 5.19
Curveball 12.00% 6 5 1 83.33% 16.67% 71.37 2060.94 6.62 -10.75 109.44 3.6 4.21 2.53 4.96
Four-Seam 8.00% 4 3 1 75.00% 25.00% 85.66 2053.20 20.90 8.88 204.32 6.8 5.35 1.75 5.49
Slider 2.00% 1 0 1 0.00% 100.00% 70.55 2081.83 6.81 -6.66 127.55 4.3 3.92 2.70 4.94
# 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:  86.99 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")
  )