Skip to content

Instantly share code, notes, and snippets.

@bayesball
Created April 1, 2014 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bayesball/9915663 to your computer and use it in GitHub Desktop.
Save bayesball/9915663 to your computer and use it in GitHub Desktop.
Graphs locations of all pitches that resulted in balls in play for Cliff Lee on Marh 31, 2014
# Cliff Lee's pitches in first game of 2014 season
# March 31, 2014
# scrape all Gameday data for that day
library(pitchRx)
d <- scrape("2014-03-31", "2014-03-31")
# pick out the Cliff Lee pitch data
library(dplyr)
locations <- select(d$pitch,
pitch_type, px, pz, des, num, gameday_link)
names <- select(d$atbat, pitcher_name, batter_name,
num, gameday_link, event, stand)
data <- inner_join(locations, filter(names,
pitcher_name == "Cliff Lee"),
by = c("num", "gameday_link"))
topKzone <- 3.5
botKzone <- 1.6
inKzone <- -0.95
outKzone <- 0.95
kZone <- data.frame(
x=c(inKzone, inKzone, outKzone, outKzone, inKzone),
y=c(botKzone, topKzone, topKzone, botKzone, botKzone)
)
# focus on only the pitches that resulted "In play"
# define a new variable result that is either "OUT" or "HIT"
data.inplay <- subset(data, substr(des, 1, 7)=="In play")
data.inplay$result <- with(data.inplay,
ifelse(substr(des, 10, 12)=="out",
"OUT", "HIT"))
# the graph
print(ggplot(data.inplay, aes(px, pz, color=result)) +
geom_point(size=4) +
geom_path(aes(x, y), data=kZone, lwd=2, col="red") +
facet_wrap(~ stand, ncol=1) +
ylim(1, 4) + xlim(-1.5, 1.5))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment