# 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
Changeup 85.77 2088.28 212.22 8.69 15.12 BallCalled 5.16 2.29 5.61 7.1
Sinker 86.11 2081.16 225.96 10.44 11.52 FoulBallNotFieldable 5.21 2.06 5.43 7.5
Curveball 74.22 2871.39 46.92 -13.68 -11.08 BallCalled 5.27 2.37 4.94 1.6
Curveball 75.20 2817.60 61.48 -11.02 -4.32 StrikeCalled 5.27 2.25 5.07 2.0
Sinker 87.43 2089.68 235.61 11.82 9.27 StrikeSwinging 5.18 2.05 5.57 7.9
Changeup 85.95 2120.52 226.70 12.47 13.09 BallCalled 5.11 2.27 5.74 7.6
Curveball 74.16 2854.64 61.80 -15.63 -6.65 StrikeCalled 5.23 2.21 5.23 2.1
Curveball 73.70 2919.47 56.98 -15.74 -8.47 StrikeCalled 5.33 2.16 5.11 1.9
Curveball 74.36 2847.27 44.15 -13.76 -12.45 BallCalled 5.28 2.37 5.03 1.5
Curveball 74.98 2844.42 47.46 -14.61 -11.70 InPlay 5.25 2.19 5.06 1.6
Curveball 73.79 2854.75 49.18 -16.74 -12.56 BallCalled 5.30 2.17 5.21 1.6
Changeup 85.75 2063.15 233.85 12.23 10.24 BallCalled 5.16 2.28 5.91 7.8
Curveball 74.28 2838.72 65.21 -13.59 -4.55 StrikeCalled 5.26 2.17 5.18 2.2
Curveball 73.60 2896.47 38.97 -13.11 -14.39 BallCalled 5.38 2.04 5.10 1.3
Sinker 86.99 2046.14 236.38 11.18 8.63 BallCalled 5.21 2.17 5.72 7.9
Sinker 86.80 2078.64 240.01 13.58 9.05 BallCalled 5.23 2.11 5.71 8.0
Curveball 74.52 2853.17 47.28 -15.34 -12.39 BallCalled 5.27 2.07 5.02 1.6
Curveball 73.70 2858.21 64.41 -14.58 -5.15 InPlay 5.26 2.26 5.29 2.1
Four-Seam 87.10 2198.72 217.43 11.58 16.49 BallCalled 5.06 2.14 6.13 7.2
Curveball 73.04 2822.49 72.63 -17.27 -3.52 StrikeCalled 5.28 2.20 5.41 2.4
Curveball 73.84 2845.27 67.82 -14.95 -4.29 BallCalled 5.33 2.25 5.37 2.3
Four-Seam 86.97 2165.38 204.77 7.88 18.43 BallCalled 5.12 2.13 6.07 6.8
Sinker 87.06 2129.08 241.35 15.02 9.46 InPlay 5.21 2.13 5.73 8.0
Four-Seam 86.95 2144.06 214.75 12.15 18.71 StrikeCalled 5.24 2.20 5.76 7.2
Curveball 76.08 2886.44 70.21 -19.22 -5.31 BallCalled 4.99 2.40 5.13 2.3
Four-Seam 86.71 2080.15 211.42 10.58 18.45 BallCalled 5.18 2.15 5.95 7.0
Curveball 75.05 2777.03 41.03 -11.93 -12.11 InPlay 5.30 2.24 5.01 1.4
Four-Seam 86.38 2109.99 205.25 7.21 16.40 StrikeCalled 5.06 2.35 5.74 6.8
Sinker 87.12 2136.47 229.64 11.43 10.86 FoulBallNotFieldable 5.02 2.38 5.73 7.7
Curveball 74.86 2932.91 53.73 -18.15 -11.51 BallCalled 5.22 2.46 5.14 1.8
Curveball 75.34 2941.16 69.74 -18.73 -5.22 BallCalled 5.21 2.37 5.12 2.3
Four-Seam 86.98 2160.76 222.17 11.61 14.11 FoulBallNotFieldable 5.10 2.34 5.58 7.4
Curveball 76.16 2967.25 75.87 -16.18 -2.52 FoulBallNotFieldable 5.02 2.51 5.08 2.5
Changeup 86.71 2148.18 231.36 12.88 11.57 BallCalled 5.07 2.37 5.72 7.7
Curveball 74.53 2784.06 58.96 -16.55 -8.18 InPlay 5.16 2.27 5.00 2.0
Four-Seam 86.75 2072.01 218.89 12.43 16.72 FoulBallNotFieldable 5.20 2.17 5.87 7.3
Curveball 74.08 2819.64 50.16 -17.13 -12.52 InPlay 5.32 2.15 5.10 1.7
Changeup 85.93 2079.23 216.73 11.33 16.49 StrikeSwinging 5.24 2.28 5.84 7.2
Four-Seam 87.03 2148.82 222.34 11.89 14.31 InPlay 5.20 2.02 6.02 7.4
Curveball 74.61 2924.55 59.58 -18.34 -9.01 BallCalled 5.35 2.22 5.03 2.0
Changeup 86.36 2082.71 225.90 12.01 12.90 StrikeCalled 5.19 2.19 6.05 7.5
Curveball 75.24 2870.27 67.39 -18.94 -6.15 BallCalled 5.04 2.35 5.26 2.2
NA 87.42 2166.39 NA NA NA BallCalled 5.40 2.41 5.94 NA
Curveball 74.42 2849.50 51.10 -17.58 -12.25 InPlay 5.32 2.36 5.22 1.7
# 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:  44
# 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 13.64% 6 4 2 66.67% 33.33% 86.08 2097.01 13.23 11.60 224.46 7.5 5.16 2.28 5.81
Curveball 52.27% 23 11 12 47.83% 52.17% 74.51 2864.20 -8.53 -15.77 57.48 1.9 5.25 2.26 5.14
Four-Seam 18.18% 8 3 5 37.50% 62.50% 86.86 2134.99 16.70 10.67 214.63 7.1 5.14 2.19 5.89
Sinker 13.64% 6 2 4 33.33% 66.67% 86.92 2093.53 9.80 12.24 234.82 7.8 5.18 2.15 5.65
NA 2.27% 1 1 0 100.00% 0.00% 87.42 2166.39 NaN NaN NaN NaN 5.40 2.41 5.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("Colin Dowlen maximum FB velocity: ", max_fb_velocity, "mph\n")
## Colin Dowlen maximum FB velocity:  87.43 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 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")
  )