Skip to content

Instantly share code, notes, and snippets.

@bayesball
Created January 23, 2015 20:03
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/a8e5255c98954d151974 to your computer and use it in GitHub Desktop.
Save bayesball/a8e5255c98954d151974 to your computer and use it in GitHub Desktop.
R function to plot win probabilities for a specific baseball game
graph.game <- function(d, game.id){
require(ggplot2)
d1 <- subset(d, substr(HALF.INNING, 1, 12) == game.id)
d1$Play <- 1:dim(d1)[1]
yr <- substr(d1[1, "HALF.INNING"], 4, 7)
mo <- substr(d1[1, "HALF.INNING"], 8, 9)
day <- substr(d1[1, "HALF.INNING"], 10, 11)
print(ggplot(d1, aes(Play, P.NEW)) + geom_line() +
ylim(0, 1) + geom_hline(yintercept=0.5, color="red") +
ylab("Probability Home Team Wins") +
labs(title = paste(d1[1, "AWAY_TEAM_ID"], "AT",
substr(d1[1, "HALF.INNING"], 1, 3),
"--", mo, "/", day, "/", yr )))
d1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment