knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)
This illustrates new functions and data from the CalledStrike package to compute and graph pitch values.
library(dplyr) library(readr) library(ggplot2) library(CalledStrike) library(mgcv) library(metR)
Read in Statcast data from 2019 season:
sc <- read_csv("~/Dropbox/2016 WORK/BLOG Baseball R/OTHER/StatcastData/statcast2019.csv")
The runs value of each count and the runs value for each end-of-PA outcome are contained in the dataset count_values
contained in the CalledStrike
package.
count_values
The function compute_pitch_values()
will create count variables and compute pitch values for all pitches in a Statcast dataset.
sc <- compute_pitch_values(sc)
Remove plays like stolen bases and pickoff plays (with pitch value of 0) that don't affect pitch value.
sc %>% filter(abs(Runs.y) > 0) -> sc
This gives graph for a specific pitch type. Note that I have changed the sign on the pitch values so we get perspective from the pitcher.
Pitch values of changeups to right-handed hitters.
pitch_value_contour(filter(sc, pitch_type == "CH", stand == "R"), title = "Pitch Values of Changeups, Stand = R")
Compare pitch values of changeups to left and right-handed hitters.
changeups <- filter(sc, pitch_type == "CH") d <- split(changeups, changeups$stand) pitch_value_contour(d, title = "Pitch Value of Changeups")
Compare for left and right sided pitchers.
sc_L <- filter(sc, p_throws == "L") sc_R <- filter(sc, p_throws == "R") d <- c(split(sc_L, sc_L$stand), split(sc_R, sc_R$stand)) names(d) <- c("Pitch L, Stand L", "Pitch L, Stand R", "Pitch R, Stand L", "Pitch R, Stand R") pitch_value_contour(d, title = "Pitch Value of Changeups")
The function pitch_value_plot()
constructs heat maps using the geom_tile()
geometric object from the ggplot2 package. They don't require an input of values for the contour lines.
Pitch values of changeups to right-handed hitters.
pitch_value_plot(filter(sc, pitch_type == "CH", stand == "R"), title = "Pitch Values of Changeups, Stand = R")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.