We all have visited the Baseball-Reference site. Moreover, some of us analyze its data in a daily basis. At least in my case, that’s true. As some of you may already know, I’m writing a thesis on Baseball as part of the requirements for my Masters Degree in Computer Science.

Even though I cannot tell very much about my thesis, I must say that I’ve written a’lot of lines of code to compute a bunch of metrics( probably 100+ stats in total ) for pitchers, batters, fielders and even parks from Retrosheet’s files. And here is were BbRef comes into play. I’ve compared most of the metrics( i.e. HBP, H, BB, GDP ) worked by my programs to the site’s metrics, and to be honest, If it hasn’t been for BbRef, my code would have been buggier than Windows 98 ( badum tss ).

But really, BbRef is a very good site with a’lot of good information about players, franchises and it even has the log for thousands of games. One cool feature about the site ( apart of those already mentioned ) is that it displays a bar graph( as the one below ) for every team in every season. This graph displays basic information about every game played by the team in that season such as game date, runs scored, runs allowed, etc.

So apart from thanking the BbRef team in my thesis for maintaining such a great site, I also wanted to show my gratitude to them by teaching you how to create the Game Results graph in R using Retrosheet’s game files as the input data.

The 2002 Oakland Athletics

The next graph shows the game results for the Oakland Athletics’ 2002 season. As you may already know( either because you saw the Moneyball movie or you’re a baseball nerd ) the A’s won 20 games in a row during that season. That is the longest winning streak since the Chicago Cubs’ winning streak of 21 games in 1935.



The Code

The above graph was done using the ggplot2 package. So if not installed in your system yet, make sure you install it and run the following code. As usual, I’m making use of the dplyr and data.table packages. Feel free to get the 2002 season game file from here.

Share this: Twitter

Facebook

