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

# 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 Colin Dowlen")
Detailed Pitch Table for Colin Dowlen
AutoPitchType ReleaseSpeed SpinRate Tilt HorizontalBreak InducedVerticalBreak PitchCall ReleaseHeight ReleaseSide Extension ClockTilt
Four-Seam 88.95 2163.59 220.94 13.59 16.84 InPlay 5.18 1.99 5.91 7.4
Curveball 75.97 3032.28 57.15 -17.31 -9.42 BallCalled 5.17 2.14 5.20 1.9
Four-Seam 88.59 2176.70 218.40 11.47 15.67 StrikeSwinging 5.20 2.01 5.87 7.3
Curveball 76.03 3076.40 47.84 -18.22 -14.71 StrikeCalled 5.22 2.08 4.94 1.6
Four-Seam 89.42 2231.40 220.45 12.14 15.40 BallCalled 5.18 2.00 6.07 7.3
Curveball 76.11 2942.03 58.22 -16.30 -8.58 BallCalled 5.04 2.26 5.48 1.9
Four-Seam 89.21 2146.74 206.52 8.52 18.20 StrikeSwinging 5.25 1.98 6.15 6.9
Curveball 76.66 2943.38 46.07 -15.08 -12.99 StrikeCalled 5.26 1.98 5.33 1.5
Four-Seam 88.42 2173.89 222.60 8.78 10.64 BallCalled 5.30 2.13 5.79 7.4
Curveball 75.06 3007.68 47.66 -16.80 -13.62 HitByPitch 5.28 2.20 5.05 1.6
Sinker 88.61 2241.19 235.84 14.64 11.10 StrikeCalled 5.27 1.82 6.07 7.9
Curveball 75.35 2986.66 58.16 -16.41 -8.47 BallCalled 5.25 2.04 5.38 1.9
Curveball 75.20 3013.61 71.98 -18.84 -4.45 InPlay 5.06 2.26 5.53 2.4
Curveball 75.15 2933.73 44.15 -14.36 -13.05 StrikeCalled 5.24 2.08 5.30 1.5
Curveball 75.86 2957.86 40.52 -11.78 -12.17 StrikeSwinging 5.42 2.10 5.15 1.4
Curveball 75.78 2958.04 48.89 -14.68 -11.09 BallCalled 5.23 2.11 5.21 1.6
Four-Seam 88.65 2129.13 218.26 12.71 17.37 StrikeCalled 5.32 1.88 5.72 7.3
Sinker 88.53 2105.30 223.79 16.13 18.09 BallCalled 5.21 2.19 5.64 7.5
Curveball 75.08 2956.93 38.27 -11.37 -12.53 BallCalled 5.22 2.18 5.25 1.3
Sinker 87.81 2060.24 220.00 12.99 16.71 FoulBallNotFieldable 5.23 2.10 5.95 7.3
Curveball 76.80 2982.76 52.02 -15.18 -10.25 StrikeCalled 5.22 2.00 5.35 1.7
Four-Seam 89.09 2228.33 219.26 10.83 14.39 BallCalled 5.16 1.96 6.02 7.3
Curveball 75.17 2948.60 63.27 -16.12 -6.58 InPlay 5.19 2.24 5.49 2.1
Four-Seam 87.85 2178.43 220.89 13.88 17.16 FoulBallNotFieldable 5.27 2.04 5.83 7.4
Curveball 75.73 3032.82 39.47 -14.49 -15.81 BallCalled 5.23 2.10 5.45 1.3
Curveball 76.24 3021.68 33.39 -11.38 -15.54 BallCalled 5.26 2.15 5.29 1.1
Four-Seam 88.27 2217.32 219.43 12.53 16.44 InPlay 5.23 1.87 5.99 7.3
Curveball 74.82 2910.43 42.45 -14.20 -13.88 BallCalled 5.26 2.04 5.19 1.4
Sinker 86.77 2113.65 229.57 10.14 9.76 StrikeSwinging 5.18 2.08 5.97 7.7
Sinker 87.91 2103.97 225.77 11.69 12.52 BallCalled 5.16 2.13 5.66 7.5
Curveball 74.61 2867.54 62.98 -18.62 -7.71 BallCalled 5.21 2.12 5.12 2.1
Four-Seam 87.20 2042.36 212.24 8.42 14.50 InPlay 5.19 2.01 5.83 7.1
Four-Seam 87.51 2146.69 214.80 9.46 14.74 FoulBallNotFieldable 5.18 1.94 5.86 7.2
Four-Seam 88.21 2148.43 217.44 11.76 16.61 BallCalled 5.22 1.82 5.75 7.2
Curveball 75.59 2950.85 54.73 -16.96 -10.36 InPlay 5.00 2.15 5.47 1.8
Four-Seam 87.28 2073.68 218.49 11.03 15.12 StrikeCalled 5.16 2.10 5.70 7.3
Four-Seam 86.93 2149.68 219.53 12.14 15.97 BallCalled 5.16 2.04 5.86 7.3
Changeup 87.08 2095.62 221.25 11.32 14.15 BallCalled 5.17 1.97 5.86 7.4
Curveball 75.82 2863.68 59.78 -17.00 -8.30 StrikeCalled 5.21 2.06 4.98 2.0
Sinker 87.66 2073.68 221.55 9.93 12.37 FoulBallNotFieldable 5.10 2.11 5.86 7.4
Curveball 76.14 3026.03 55.95 -20.10 -11.85 BallCalled 5.18 2.01 5.51 1.9
Sinker 86.93 2066.13 220.44 10.36 13.34 BallCalled 5.19 1.84 5.91 7.3
Curveball 75.90 2921.28 64.82 -16.34 -5.98 BallCalled 5.06 2.05 5.51 2.2
Four-Seam 87.03 2156.60 215.64 12.39 18.53 StrikeCalled 5.27 1.90 5.83 7.2
Sinker 87.94 2184.96 224.64 12.68 14.04 StrikeSwinging 5.16 1.90 6.05 7.5
Four-Seam 86.56 2110.76 211.18 10.41 18.46 FoulBallNotFieldable 5.30 1.78 5.68 7.0
Curveball 76.23 2926.23 49.78 -15.70 -11.67 BallCalled 5.19 1.92 5.43 1.7
Four-Seam 88.03 2131.91 217.17 10.31 14.88 BallCalled 5.21 1.75 6.07 7.2
Sinker 87.40 2105.23 220.58 8.85 11.54 FoulBallNotFieldable 5.19 1.84 5.92 7.4
Curveball 74.63 2937.50 48.07 -16.68 -13.35 InPlay 5.17 2.05 5.44 1.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 == "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:  50
# Display the summary table
knitr::kable(pitch_summary, caption = "Summary Pitch Table for Colin Dowlen")
Summary Pitch Table for Colin Dowlen
AutoPitchType Usage TotalPitches Balls Strikes BallPercentage StrikePercentage AvgVelocity AvgSpinRate AvgInducedVertBreak AvgHorzBreak AvgTilt AvgClockTilt AvgReleaseHeight AvgReleaseSide AvgExtension
Changeup 2.00% 1 1 0 100.00% 0.00% 87.08 2095.62 14.15 11.32 221.25 7.4 5.17 1.97 5.86
Curveball 46.00% 23 12 11 52.17% 47.83% 75.65 2965.13 -10.97 -15.82 51.55 1.7 5.20 2.10 5.31
Four-Seam 34.00% 17 6 11 35.29% 64.71% 88.07 2153.27 15.94 11.20 217.25 7.2 5.22 1.95 5.88
Sinker 18.00% 9 3 6 33.33% 66.67% 87.73 2117.15 13.27 11.93 224.69 7.5 5.19 2.00 5.89
# 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("Colin Dowlen maximum FB velocity: ", max_fb_velocity, "mph\n")
## Colin Dowlen maximum FB velocity:  89.42 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 Colin Dowlen",
       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")
  )