Skip to content

Instantly share code, notes, and snippets.

@bayesball
Created October 23, 2023 14:58
Show Gist options
  • Save bayesball/a1f8ddb4593e7b31b83022e511f5e560 to your computer and use it in GitHub Desktop.
Save bayesball/a1f8ddb4593e7b31b83022e511f5e560 to your computer and use it in GitHub Desktop.
Quarto document showing work for Zack Wheeler 2023 NLCS work
---
title: "Zach Wheeler 2023 NCS"
format: html
editor: visual
---
Load in necessary packages:
```{r}
library(baseballr)
library(dplyr)
library(ggplot2)
library(janitor)
```
Also need this helper function when we plot pitch locations:
```{r}
add_zone <- function(Color = "red"){
topKzone <- 3.5
botKzone <- 1.6
inKzone <- -0.85
outKzone <- 0.85
kZone <- data.frame(
x=c(inKzone, inKzone, outKzone, outKzone, inKzone),
y=c(botKzone, topKzone, topKzone, botKzone, botKzone)
)
geom_path(aes(.data$x, .data$y), data=kZone,
lwd=1, col=Color)
}
```
Read in playoff data for two days:
```{r}
day1 <- statcast_search("2023-10-16", "2023-10-16")
day2 <- statcast_search("2023-10-21", "2023-10-21")
```
Focus on pitches thrown by Wheeler (statcast id 554430):
```{r}
zw <- rbind(day1, day2) |>
filter(pitcher == 554430)
```
Table of pitches thrown each game:
```{r}
table(zw$game_date)
```
What types of pitches were thrown each game?
```{r}
tabyl(zw, game_date, pitch_name) |>
adorn_percentages()
```
Outcomes of each pitch type?
```{r}
tabyl(zw, description, pitch_name)
```
Outcomes of balls put into play?
First define Hit variable:
```{r}
hits <- c("single", "double", "triple", "home_run")
zw |>
mutate(Hit = ifelse(events %in% hits, "YES", "NO")) ->
zw
```
Graph of launch variables where color of point corresponds to outcome:
```{r}
ggplot(filter(zw, type == "X"),
aes(launch_angle, launch_speed,
color = Hit)) +
geom_point() +
geom_point(data = filter(zw, events == "home_run"),
size = 5) +
theme(text=element_text(size=18)) +
theme(plot.title = element_text(colour = "blue", size = 18,
hjust = 0.5, vjust = 0.8, angle = 0)) +
ggtitle("Launch Variables for BIP - Zack Wheeler's 2023 NLCS")
```
Define count and number of pitch variables:
```{r}
zw |>
mutate(Count = paste(balls, strikes, sep = "-"),
N_Pitch = balls + strikes + 1 ) -> zw
```
Look at frequencies of count after each pitch
```{r}
tabyl(zw, N_Pitch, Count)
```
Put counts in a data frame suitable for graphing:
```{r}
df <- data.frame(N_Pitch = c(1, 1, 2, 2, 2, 3, 3, 4, 4, 5),
Count = c("0-1", "1-0", "0-2", "1-1", "2-0", "1-2", "2-1",
"2-2", "3-1", "3-2"),
Advantage = c(1, -1, 2, 0, -2, 1, -1, 0, -2, -1),
N = c(31, 12, 17, 19, 2, 18, 9, 14, 2, 7))
```
```{r}
ggplot(df, aes(N_Pitch, Advantage, label = Count,
size = N)) +
geom_label(color = "white", fill = "red") +
ggtitle("Zack Wheeler's Counts for the 2023 NLCS") +
theme(text=element_text(size=18)) +
labs(y = "Pitcher Advantage",
x = "# of Pitches") +
theme(plot.title = element_text(colour = "blue", size = 18,
hjust = 0.5, vjust = 0.8, angle = 0))
```
Relationship between count and launch speed?
```{r}
ggplot(filter(zw, type == "X"),
aes(launch_speed, Count)) +
geom_point() +
theme(text=element_text(size=18))
```
Count and estimated batting average?
```{r}
ggplot(filter(zw, type == "X"),
aes(estimated_ba_using_speedangle, Count)) +
geom_point() +
theme(text=element_text(size=18)) +
ggtitle("Relationship Between Estimated BA and Count") +
theme(plot.title = element_text(colour = "blue", size = 18,
hjust = 0.5, vjust = 0.8, angle = 0))
```
Pitch locations:
```{r}
ggplot(zw, aes(plate_x, plate_z,
color = description)) +
geom_point() +
add_zone("black") +
facet_wrap(~ pitch_name, ncol = 3) +
theme(aspect.ratio = 1) +
theme(text=element_text(size=18)) +
ggtitle("Pitch Locations for Each Pitch Type") +
theme(plot.title = element_text(colour = "blue", size = 18,
hjust = 0.5, vjust = 0.8, angle = 0))
```
Some of these location patterns are better understood by exploring association between pitch type and batter side:
```{r}
tabyl(zw, stand, pitch_name)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment