# 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
Sinker 88.18 2135.89 223.35 13.25 15.35 BallCalled 5.14 1.97 5.58 7.4
Four-Seam 88.00 2141.50 220.91 13.69 17.16 BallCalled 5.15 1.92 5.60 7.4
Sinker 86.48 2075.59 226.40 14.46 15.15 InPlay 5.10 2.00 5.84 7.5
Curveball 75.05 2900.55 65.96 -18.49 -6.41 StrikeCalled 5.20 2.07 4.73 2.2
Curveball 75.59 2927.43 65.02 -21.05 -7.99 FoulBallNotFieldable 5.20 2.06 4.69 2.2
Curveball 74.97 2924.64 54.68 -17.87 -10.76 BallCalled 5.26 2.13 4.80 1.8
Sinker 88.78 2190.79 239.05 16.27 11.04 InPlay 5.18 1.92 5.59 8.0
Curveball 74.87 2861.39 65.29 -13.58 -4.56 BallCalled 5.30 1.94 4.68 2.2
Curveball 75.22 2860.85 63.07 -16.33 -6.48 BallCalled 5.27 1.94 4.70 2.1
Four-Seam 87.07 2167.24 217.61 11.43 16.22 BallCalled 5.16 2.06 5.58 7.3
Changeup 86.74 2091.70 219.66 11.25 14.95 BallCalled 5.18 2.03 5.42 7.3
Four-Seam 86.41 2118.18 227.50 17.32 17.27 BallCalled 5.19 2.01 5.85 7.6
Four-Seam 87.53 2158.12 219.20 11.48 15.42 StrikeCalled 5.18 1.91 5.96 7.3
Curveball 76.16 2860.97 58.66 -14.25 -7.00 StrikeSwinging 5.30 1.90 5.13 2.0
Curveball 77.03 3054.63 58.09 -16.38 -8.51 BallCalled 5.40 1.88 5.02 1.9
Curveball 76.84 2939.69 68.84 -16.07 -4.54 InPlay 5.02 2.19 5.23 2.3
Four-Seam 86.91 2212.17 219.21 13.33 17.64 InPlay 5.29 1.82 5.46 7.3
Sinker 86.16 2084.00 230.94 16.80 14.94 BallCalled 5.15 2.00 5.81 7.7
Four-Seam 86.37 2110.04 219.36 12.90 17.07 BallCalled 5.15 1.98 5.90 7.3
Sinker 87.25 2141.68 229.88 14.82 13.70 StrikeCalled 5.19 2.03 5.91 7.7
Curveball 75.00 2852.77 59.16 -20.35 -10.37 InPlay 5.34 1.99 4.96 2.0
Sinker 88.07 2152.28 225.85 14.28 15.10 FoulBallNotFieldable 5.18 1.95 6.00 7.5
Curveball 77.14 2837.55 68.10 -11.29 -3.30 BallCalled 5.10 2.02 5.33 2.3
Changeup 86.94 2140.40 226.37 13.51 13.97 FoulBallNotFieldable 5.16 1.99 5.85 7.5
Curveball 74.07 2897.52 44.44 -16.20 -14.58 FoulBallNotFieldable 5.23 1.99 5.10 1.5
Curveball 74.00 2769.63 58.98 -14.32 -6.90 BallCalled 5.35 1.85 5.11 2.0
Sinker 87.51 2090.03 235.42 15.16 11.65 FoulBallNotFieldable 5.08 1.94 5.82 7.8
Curveball 74.24 2878.71 52.25 -13.64 -8.81 InPlay 5.37 1.91 4.94 1.7
Curveball 75.03 2933.95 53.30 -12.84 -7.90 BallCalled 5.25 2.03 4.89 1.8
Sinker 87.81 2153.89 244.89 16.68 9.08 BallCalled 5.06 2.02 5.90 8.2
Sinker 87.16 2115.84 229.92 14.78 13.67 BallCalled 5.10 1.92 5.82 7.7
Four-Seam 87.21 2174.30 221.50 13.47 16.51 StrikeCalled 5.12 2.00 5.90 7.4
Four-Seam 86.57 2158.50 217.35 12.08 17.09 InPlay 5.15 1.98 5.96 7.2
# 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:  33
# 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 6.06% 2 1 1 50.00% 50.00% 86.84 2116.05 14.46 12.38 223.01 7.4 5.17 2.01 5.64
Curveball 42.42% 14 7 7 50.00% 50.00% 75.37 2892.88 -7.72 -15.90 59.70 2.0 5.26 1.99 4.95
Four-Seam 24.24% 8 4 4 50.00% 50.00% 87.01 2155.01 16.80 13.21 220.33 7.3 5.17 1.96 5.78
Sinker 27.27% 9 4 5 44.44% 55.56% 87.49 2126.67 13.30 15.17 231.74 7.7 5.13 1.97 5.81
# 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:  88.78 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")
  )