Wednesday, September 1, 2010

Heat Map and Pitch F/X

Nothing breakthrough here, not really any analysis, just some R fun. I wanted to give a heads up for a really interesting function I found in R for anyone that likes Pitch F/X data tools. While Dave Allen is the king of heat maps for Pitch F/X in my opinion, I haven't seen him utilize the smoothScatter function with the RColorBrewer package in this way (or perhaps he does but with different colors or smoothing--maybe someone else does, though). I know that the 'contour' function and some other types always give me fits when I try to use them, depending on what type of data I have. SmoothScatter is easy to use, and it's just like making a regular scatter plot with a color representation that you can make a 'heat map' type.

The function does a kernel density estimate and then automatically blends the colors on the 2-dimensional scatterplot. If you want, you can also include the actual points along with other options. It seems to work pretty well (though I had to create my own palette in RCB that was a reversal of what the default was...I imagine there's a more elegant way to do it than I did, like reverse score the density estimation output).

Anyway, here's the heat map of the same data I had in a previous post for Johnny Cueto. It's just pitch location, and the strike zone is not normalized (it's simply an average height of batters for all pitches for that game). Red is where lots of pitches were, while blue indicates areas where pitches were not located. I'm still working on the key for that, but I think it's pretty straight forward (also, it's the catcher's view). Finally, I provided the R-Code below in case you want to implement it with your data (I apologize for the terrible code formatting, but Blogger really doesn't seem to have many options). The code is very simple. If you want the data to be smoother or less smooth just increase (decrease) the 'nbin' option and play with the function in the 'transformation' option. Just a note, the 'col="black"' portion is used only if you decide to include the points on your plot. I found both ways to be kind of neat depending on what you want to know.

Finally, from what I've read just now, smoothScatter is a part of the base R graphics package. However, if you have an older version of R, I'm not sure which package it is part of. It used to be part of 'geneplotter', but that does not seem to be available on the Install Packages menu anymore. I have the new version at my office, but not here at home...otherwise, I would have changed the color of the strike zone outline to be more visible. But that's an easy fix if you're doing it at home. I'd recommend playing with different color palettes as well and updating your R version to the newest one (64-bit if you're not running a crappy 32-bit Vista OS on your home machine like me...).


R CODE:

library(graphics)
library(RColorBrewer)

brewer.pal(11, "RdYlBu")

buylrd <- c("#313695", "#4575B4", "#74ADD1", "#ABD9E9", "#E0F3F8", "#FFFFBF", "#FEE090", "#FDAE61", "#F46D43", "#D73027", "#A50026")

smoothScatter(data$pz~data$px, nbin=1000, colramp = colorRampPalette(c(buylrd)), nrpoints=Inf, pch="", cex=.7, transformation = function(x) x^.6, col="black", main="Johnny Cueto Location (May 11, 2010)", xlab="Horizontal Location", ylab="Vertical Location")

lines(c(0.708335, 0.708335), c(mean(data$sz_bot), mean(data$sz_top)), col="white", lty="dashed", lwd=2)

lines(c(-0.708335, -0.708335), c(mean(data$sz_bot), mean(data$sz_top)), col="white", lty="dashed", lwd=2)

lines(c(-0.708335, 0.708335), c(mean(data$sz_bot), mean(data$sz_bot)), col="white", lty="dashed", lwd=2)

lines(c(-0.708335, 0.708335), c(mean(data$sz_top), mean(data$sz_top)), col="white", lty="dashed", lwd=2)

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. oh so good .... I like your blogger post because you talking about Sex and Marriage and i like any thing or any post talking about it as Map of Ancient Greece Ancient Greece for Kids ,Ancient Greece Timeline,Ancient Greece Government ,BBC Ancient Greece ,Ancient Greece Food ,Ancient Greece Religion, Ancient Greece Clothing ,Women in Ancient Greece ,Ancient Greece Art ,Ancient Greece Gods ,Ancient Greece Facts ,Ancient Greece Geography,British Museum so i will be happy if your visit my site AncientGreece.Me

    ReplyDelete
  3. Great stuff ! I love it. I just love it so much, that the greedy visual-data gnome in me wants more ! Thanks for sharing !

    ReplyDelete