Tuesday 27 June 2017

Spray angle / horizontal angle for Baseball Savant data

When you first show batted balls from baseball savant (type = "X") in Tableau by dragging the hc X and Hc Y fields on the columns and rows, you see something like this:
Left and right field are correct, but we tend to see this being up-side-down. Also you will notice that home plate is not 0,0 but more like 125, 200 (this is not the same for every ballpark it seems, but will do for this demo)

By creating two fields

X = [Hc X]-125
Y = ([Hc Y]-200)*-1

and replace the HcX/Y with them we can easily change the picture (colour coded by hit location):


To calculate the spray angle / horizontal angle we can use Tableau's ATAN2 function (Returns the arc tangent of two given numbers (X and Y), result is in radians):

((ATAN2([Y],[X]) / PI() * 180) - 90) * -1
where the PI() * 180 converts the radians to degrees, and the -90 makes the vertical axis (basically straight up from home plate) the 0 degrees line. The * -1 is making left of this 0-line negative, and right of the line positive.

Because of pop-ups and foul balls behnd the catcher you end up with some large degrees numbers, but by showing only the -45 to 45 degrees range, we get a picture we could expect:


Where am I going with this from here? Combining it with the ballpark data in this article and data from this excellent site about ballparks with the end goal to show for every hit ball in baseball savant in what park it would be a home run.

1 comment:

  1. Do you have a Tableau Public page for this? I have the spray angle. The spray angle is the X axis, correct? What do I use for the Y axis?

    ReplyDelete