Sports. Statistics. Economics. And a naive junior faculty member pretending to know more than nothing about something.

Thursday, January 20, 2011

sab-R-metrics: Intermediate Boxplots and Histograms

Last week, I began talking about using the base graphics in R. Those graphics were pretty bland, and my hope for the next two posts is to introduce some interesting additions to the basic graphics that come from R: color, legends, lines, shapes, multiple graphs side-by-side, text, point types, and custom axes. If you have missed any of the previous sab-R-metrics posts, go ahead and start at the beginning, working your way through the posts below:

Last time, I did more with box plots than with histograms and scatterplots. I plan to work with box plots and histograms today in a little more detail, showing how the different graphing options work for each. Next time, I'll work with scatterplots, as there are a number of other things we can do with those.

I'll begin again with box plots. However, we're going to use new data this time. Hopefully, this will get you in practice with getting data, saving it correctly and in the right place, and calling it back up in R. For this post, I'll be using Pitch F/X data again (because it's so easily available at Joe Lefkowitz's site). I'm a big Shaun Marcum fan, so let's use his 2010 data. Click here for the direct link to the data.

Don't forget to change all the variable names at the top to lowercase letters and underscores (_) instead of spaces. Then, remember to save it in a directory that is convenient for you with an informative, yet simple file name. I named mine 'marcum10.csv'. Go ahead and open it up in R. My code calling the data and checking to make sure it imported correctly is below. I also made sure to subset the data that has all the Pitch F/X information that we want to use, leaving out missing valued observations:

Okay, now we're ready to have some fun. First, go ahead and make a box plot of Marcum's four-seam fastball velocity by inning (using just the pitches coded 'FF'). Use the version we last left off with when working with the Pujols data last time, but change the title of the plot so that it is telling us the correct player. Here is what it should look like to start:

##boxplots & histogram of fastballs (generic FA version) only with width by num. of obs.

Again, the 'varwidth=' option is pretty neat as we can see how many pitches Marcum throws each inning. But they're still pretty boring. Perhaps we should begin here by adding some color. For this, we'll use the "col=" option. Marcum is a newly minted Brewer, so lets use the old school Brewer blue (or something close to it). I've also added the histogram with color below:

Notice that for multiple colors, we use the "c()" function, which tells R that we want to use a vector of colors. The box plot function knows that we want them to alternate if it is typed in this way, and voila, pretty! Of course, we can also go overboard. For example, this code:

Honestly, this is hideous. The lesson here is that you CAN use too much color. Remember to use color to help portray the information you are trying to communicate, rather than just to make things bright. There are a LOT of color options in R, which I will go over more specifically in a later post, taking advantage of RColorBrewer and transparency options for the colors. When creating a graphic, the idea is to consolidate the information that you want to communicate into an easy to see picture that really makes your point stand out. In our box plot example, the color does not give us a whole lot other than sprucing it up a bit. That is fine, but in my opinion it makes it a little easier to look at, while showing our main point here: Shaun Marcum's fastball velocity goes down a bit while the game progresses (leaving aside the sampling bias issue for now related to the fact that when he is allowed to stay in longer, it is likely when he has a bit more stamina to do so).

Okay, so we've got a little color in the plots, but there are other color options as well. For example, perhaps we don't want the borders of the boxes to be the black default. We can go ahead and make the Brewers colors alternate with the borders as well (though, the gold is a bit ugly here on both plots). I also change the axis labels using the "names=c()" option in the "boxplot()" function to say "1st, 2nd..." instead of just numbering them.

There are a lot of options for adjusting your box plots, and I again would recommend keeping things simple. I am not a fan of the extra border color, but it is up to you to convey your information as you please.

As you have seen from the plots I display here, I have both the box plot and histogram in the same window side-by-side. This is a very useful feature in R, allowing us to compare certain graphs to one another (for example, pitch locations of fastballs and curveballs side-by-side...but I'll get to these plots later). To make plots within the same window, we use the "par(mfrow=c(,))" function. Here, the number of rows of graphs you want in your plot is indicated by the first number in the "c(,)" command, while the number of columns is after the comma (just like when calling vectors and cells from our data). So, if we want two graphs side by side, we'd type:

Now, remember that the order of your graphs will go left to right, top to bottom, so be sure to order them the way you want after the first command. The above plot should look just like the previous one shown for the new border colors.

We can also make the graphs stacked on top of one another (less desirable in my opinion), or in a 2-by-2 matrix of plots.

#stacked graphspar(mfrow=c(2,1))

#2-by-2 matrix of graphspar(mfrow=c(2,2))

And you can extrapolate from here, adding each graph just after the command. Of course, you don't want to put too many graphics within the same window, as they would be too small or trying to convey too much information at once. I usually limit to 1-by-3 or 2-by-2 max in a single window. But again, optimal ways of displaying your points really depends on the type of data you are working with. As I've said throughout, keep things easy to read so that they convey your message as simply and concisely as possible.

The last thing I want to go over today is saving your graphics. R has it's own window where the graphics pop up; however, copying and pasting these into Word and other places can make them blurry. In addition, if you adjust the window size (sometimes you'll want to do that to make them look right), you may never be able to get that exact window size back again by the click-and-drag procedure. Therefore, it is usually a good idea to save your graphics using an R graphical device. I'll talk about two of them here.

The first one I learned to use was for creating a PDF of your graphic. However, I later learned that using .png files is the best for keeping your graphics crisp. Both have advantages I'll talk about later on, but for single graphics, PNG seems to be the way to go. The most important part about saving using these graphical devices is that you can ALWAYS make the graphics the same size, rather than rescaling them in the R window or later on in Word or another program. When we want to save a graphic, we'll start with the following:

Always be sure to turn off your device after you are finished with it. While you will not see your graph within R, you can go to your working directory and find the file that you just created. You'll want to fiddle around with the height and width depending on your graphic to get it looking the way you'd like. There are a number of other options for each device, and I would suggest using "help(pdf)" or "help(png)" for more information on how to adjust the background and foreground colors, among other options. For the PDF device, it will continue to add pages to the PDF file, which is convenient in certain circumstances I'll talk about later. However, if you leave the PNG device open, each time you run a new graph, it will overwrite the previous one. Remember that you can also use the "par(mfrow=c())" function within the device to make side-by-side graphs in the same window or on the same page in a PDF.

Next time, we'll work with Scatterplots and really get into adding text, legends, lines and shapes, and different types of points to your plots. After that, we'll get to some more advanced graphing and adjusting colors and color palettes. Today's code should look something like this:

4 comments:

Great post, your tutorials are really helping me out. Just one quick question: is there a quick way to add labels to outliers beyond the whiskers in box plots? i've been poking around, but haven't been able to come up with anything that works.

in case anyone else is wondering about this, i found a quick and dirty way to do this -- just run identify(dataset$xvar, plots$yvar, rownames(dataset)) after generating the plot.this allows you to label the outliers with a left click directly in the plot.

Followers

About Me

I am currently an Assistant Professor at the University of Florida within the Department of Tourism, Recreation and Sport Management. I played baseball in college and left my high school pitching coach gig to attend graduate school and proceed to educate young minds about sports economics, sports analytics, and sport management. I consider myself an Orioles fan, but I honestly don't care much who wins...unless I picked them beforehand. You can reach me at bmmillsy AT umich DOT edu (to be updated soon).