Created
October 29, 2016 12:47
-
-
Save bayesball/d9f821ba6e2f8adf2a870364eb584a6e to your computer and use it in GitHub Desktop.
Code for Swing and Miss Study on 10/29/16
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(pitchRx) | |
| library(dplyr) | |
| library(ggplot2) | |
| # scrape data | |
| ws3 <- scrape("2016-10-28", "2016-10-28") | |
| # choose variables of interest | |
| p <- select(ws3$pitch, des, px, pz, pitch_type, inning_side, | |
| gameday_link, num, inning) | |
| p <- inner_join(p, | |
| select(ws3$atbat, pitcher_name, | |
| batter_name, gameday_link, num)) | |
| # classify pitches as swings and misses | |
| swing_outcomes <- c("Foul", "Foul (Runner Going)", | |
| "Foul Bunt", "Foul Tip", | |
| "In play, no out", "In play, out(s)", | |
| "In play, run(s)", "Swinging Strike", | |
| "Swinging Strike (Blocked)") | |
| miss_outcomes <- c("Swinging Strike", | |
| "Swinging Strike (Blocked)") | |
| p <- mutate(p, | |
| Swing=ifelse(des %in% swing_outcomes, "yes", "no"), | |
| Miss=ifelse(des %in% miss_outcomes, "yes", "no"), | |
| Team=ifelse(inning_side=="bottom", "Cubs", "Tribe"), | |
| Type=ifelse(pitch_type %in% | |
| c("FC", "FF", "FT", "SI"), | |
| "fast", "slow")) | |
| # create a new data frame with only the swings | |
| d_swing <- filter(p, Swing=="yes") | |
| # compute percentages of swings for each team | |
| summarize(group_by(d_swing, Team), | |
| N=n(), Miss=sum(Miss=="yes"), | |
| Pct=round(Miss / N * 100)) | |
| # break these pcts by pitch type | |
| S <- summarize(group_by(d_swing, Team, Type), | |
| N=n(), Miss=sum(Miss=="yes"), | |
| Pct=round(Miss / N * 100)) | |
| # code for first graph | |
| TH <- theme( | |
| plot.title = element_text( | |
| colour = "red", | |
| size = 18, | |
| hjust = 0.5, | |
| vjust = 0.8, | |
| angle = 0 | |
| ) | |
| ) | |
| ggplot(S, aes(Type, Pct)) + geom_point(size=3, color="red")+ | |
| facet_wrap(~ Team, ncol=1) + coord_flip() + | |
| ggtitle("Percentage of Missed Swings by Type of Pitch") + | |
| TH | |
| # code for 2nd graph | |
| zone <- function(){ | |
| 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) | |
| ) | |
| ggplot(aes(x, y), data=kZone) + | |
| geom_path(lwd=1.5, col="red") + | |
| xlim(-2, 2) + ylim(0, 5) + | |
| coord_fixed(ratio = 1)} | |
| zone() + | |
| geom_point(data=d_swing, | |
| aes(px, pz, color=Type, size=Miss)) + | |
| facet_wrap(~ Team) + | |
| ggtitle("Locations of Swung Pitches") + | |
| TH + | |
| geom_point(data=d_swing[126, ], | |
| aes(px, pz), size=5, color="purple") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment