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

# Create a detailed table for each pitch
detailed_pitch_table <- Colin_Dowlen_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
Curveball 74.16 2810.92 59.09 -16.74 -8.53 StrikeCalled 5.14 1.90 6.11 2.0
Four-Seam 88.90 2042.93 219.56 12.58 16.23 FoulBallNotFieldable 4.92 1.85 6.35 7.3
Curveball 75.58 2987.86 42.72 -12.78 -12.10 BallinDirt 5.05 1.85 6.12 1.4
Curveball 76.17 3005.98 48.25 -12.97 -9.85 BallCalled 5.18 1.86 5.84 1.6
Four-Seam 89.25 2153.58 211.97 10.64 18.18 FoulBallNotFieldable 4.95 1.79 6.30 7.1
Curveball 76.34 2920.84 83.50 -14.90 -0.21 InPlay 4.74 2.04 5.93 2.8
Four-Seam 88.30 2142.54 205.27 8.49 19.20 BallCalled 5.05 1.90 6.23 6.8
Curveball 74.78 2864.44 68.72 -16.53 -4.76 BallCalled 5.23 1.82 5.95 2.3
Four-Seam 87.55 2145.16 212.51 7.70 13.25 BallCalled 5.10 1.89 6.34 7.1
Four-Seam 87.80 2094.75 206.05 7.50 16.50 StrikeCalled 5.11 1.73 6.20 6.9
Four-Seam 88.14 2166.01 215.78 8.68 13.24 StrikeSwinging 5.10 1.82 6.34 7.2
Curveball 76.77 2931.58 61.57 -14.22 -6.11 StrikeSwinging 5.19 1.73 5.80 2.1
Curveball 75.77 3028.26 49.50 -15.71 -11.56 BallCalled 4.93 1.97 6.03 1.6
Four-Seam 88.91 2274.57 217.83 14.22 19.46 BallCalled 5.23 1.81 6.54 7.3
Four-Seam 89.50 2282.48 220.93 15.03 18.44 StrikeSwinging 5.12 1.72 6.43 7.4
Curveball 76.37 3023.16 51.41 -15.69 -10.89 FoulBallNotFieldable 5.10 1.87 5.97 1.7
Four-Seam 90.22 2145.43 206.23 9.09 19.67 StrikeCalled 5.12 1.70 6.03 6.9
Sinker 89.27 2153.24 231.37 13.31 11.87 StrikeCalled 5.08 1.76 6.19 7.7
Curveball 75.95 2993.88 59.91 -16.91 -7.95 BallCalled 5.12 1.86 5.87 2.0
Four-Seam 89.21 2172.22 209.66 9.96 18.77 BallCalled 5.14 1.85 6.18 7.0
Curveball 75.64 2848.30 59.00 -14.52 -6.98 StrikeSwinging 5.13 1.81 6.07 2.0
Curveball 75.82 3021.99 54.04 -15.93 -9.64 BallCalled 5.14 1.72 5.71 1.8
Curveball 75.85 2948.87 59.07 -15.65 -7.52 BallinDirt 5.09 1.77 6.00 2.0
Curveball 74.83 2914.37 50.53 -14.31 -9.82 StrikeCalled 5.09 1.90 5.62 1.7
Curveball 76.67 3088.30 60.81 -17.57 -8.00 BallCalled 4.84 2.03 5.93 2.0
Four-Seam 88.12 2124.84 218.18 10.64 14.69 BallCalled 5.15 1.81 6.40 7.3
Four-Seam 88.79 2118.28 220.83 10.99 13.88 BallCalled 5.11 1.70 6.28 7.4
Sinker 87.93 2147.27 232.43 14.27 12.19 FoulBallNotFieldable 5.05 1.68 6.45 7.7
Curveball 75.63 2913.89 56.08 -13.09 -7.19 StrikeSwinging 5.14 1.78 5.82 1.9
Four-Seam 88.12 2201.66 215.29 9.87 15.08 StrikeCalled 5.03 1.73 6.13 7.2
Curveball 75.47 2931.73 52.32 -15.04 -9.98 InPlay 5.00 1.94 5.64 1.7
Curveball 75.23 2971.38 64.07 -16.49 -6.25 StrikeCalled 5.04 1.79 5.96 2.1
Curveball 75.52 2900.45 60.24 -15.84 -7.38 FoulBallNotFieldable 5.14 1.76 6.08 2.0
Curveball 75.64 2931.21 57.99 -18.62 -9.83 BallinDirt 5.07 1.85 6.09 1.9
Four-Seam 88.04 2170.30 218.45 12.90 17.42 InPlay 5.11 1.75 6.26 7.3
Curveball 75.49 2902.55 49.44 -16.07 -11.89 BallCalled 5.10 1.67 5.62 1.6
Sinker 88.67 2168.19 225.07 16.57 17.80 StrikeSwinging 4.92 1.76 6.22 7.5
Four-Seam 88.54 2285.87 216.11 11.33 16.74 FoulBallNotFieldable 4.99 1.81 6.56 7.2
Curveball 75.65 2930.02 71.75 -16.37 -3.74 BallCalled 4.82 2.02 5.85 2.4
Curveball 75.53 2881.83 52.74 -16.12 -10.53 FoulBallNotFieldable 5.14 1.69 5.95 1.8
Sinker 89.52 2237.88 228.91 13.04 12.55 InPlay 4.93 1.68 6.11 7.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:  41
# 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
Curveball 53.66% 22 8 14 36.36% 63.64% 75.68 2943.26 -8.21 -15.55 57.85 1.9 5.06 1.85 5.91
Four-Seam 36.59% 15 6 9 40.00% 60.00% 88.63 2168.04 16.72 10.64 214.31 7.2 5.08 1.79 6.30
Sinker 9.76% 4 0 4 0.00% 100.00% 88.85 2176.64 13.60 14.30 229.44 7.6 5.00 1.72 6.24
# 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:  90.22 mph
# Prepare data for plotting pitch locations
pitch_location_data <- Colin_Dowlen_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)) +
  scale_y_continuous(limits = c(-25, 25)) +
  labs(title = "Pitch Movement for Colin Dowlen",
       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")
  )