Skip to content

Instantly share code, notes, and snippets.

@bayesball
Created March 25, 2014 13:59
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/9762419 to your computer and use it in GitHub Desktop.
Save bayesball/9762419 to your computer and use it in GitHub Desktop.
Exploring Cliff Lee's pitches in 2013 season using pitchRx package
##################################################
# collect all pitches of Cliff Lee for 2013 season
# using pitchRx package
###################################################
# create database of 2013 pitches
# library(dplyr)
# my_db <- src_sqlite("pitchRx.2013", create = TRUE)
# library(pitchRx)
# scrape(start = "2013-01-01", end = "2013-12-01",
# connect = my_db$con)
# bring pitchfx data into R from database
library(dplyr)
my_db <- src_sqlite("pitchRx.2013")
locations <- select(tbl(my_db, "pitch"),
pitch_type, px, pz, des, num, gameday_link)
names <- select(tbl(my_db, "atbat"), pitcher_name, batter_name,
num, gameday_link, event, stand)
que <- inner_join(locations, filter(names,
pitcher_name == "Cliff Lee"),
by = c("num", "gameday_link"))
pitchfx <- collect(que) #submit query and bring data into R
# create date variable and restrict attention to pitches
# during regular season
pitchfx$gamedate <- substr(pitchfx$gameday_link, 5, 14 )
pitchfx <- subset(pitchfx,
as.numeric(substr(gamedate, 6, 7)) > 3)
# what pitches did Cliff throw?
table(pitchfx$pitch_type)
# where did he throw them?
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)
)
library(ggplot2)
print(ggplot(pitchfx, aes(px, pz, color=stand)) + geom_point() +
geom_path(aes(x, y), data=kZone, lwd=2, col="red") +
ylim(0, 5) + facet_wrap(~ stand, ncol=1))
# separate graphs for each pitch type
print(ggplot(pitchfx, aes(px, pz, color=pitch_type)) + geom_point() +
geom_path(aes(x, y), data=kZone, lwd=2, col="red") +
ylim(0, 5) + facet_wrap(~ pitch_type))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment