How are you? These past few months have been really good for the site content wise. To overshare a little we passed our 2020 views count for the year back in June. In my head the best part of this accomplishment is the idea my life feels more in equilibrium. It’s been a good few months, moving house, celebrating best friends birthdays, milestones etc.
Anyway, todays blog a little different in the sense that I’ve never done any content on baseball before. Mainly because it isn’t as big here as in the US, but nonetheless I have always admired the idea of mapping where shots / runs are created. In fact I first came across this idea years ago, by Jacob Olsufka who produced this awesome hex map of hits.
We will look to strip it back a little and rather than aggregate shots into hex’s just plot the original shots for a player for a season.
You will see the chart we will look to recreate is very similar to that seen on Statcast
The baseball savant website allows you not only to visually see the spray chart for the selected filters you’ve applied but also download the csv file.
This is what we will do to retrieve our data for this tutorial. I’ve posted the example data on the Github repo. Here is how the chart renders on the site.
Its worth noting that this chart is interactive in the sense that it offers tooltips as well as the option to change the venue background chart, see how we can switch it to the Atlanta Braves venue. We will revisit this when we build our chart.
Okay so we have downloaded our CSV. Lets jump into Tableau to start mapping our data.
The two fields we will need are Hc X and Hc Y these are the positional points of where the shot has been hit.
Lets make two transformations.
([Hc X] – 125.42)/10
and, Plot Y
(198.27 – [Hc Y])/10
These are calculations to make sure our data is in the right shape, dividing by 10 isn’t necessary per se, but because I wanted to use the makepoint function incase I required more layers at some point its better to have these scaled down between geographic points.
Off the back of those calculations we can create
MAKEPOINT([Plot Y],[Plot X])
Double click the makepoint calculation.
Turn the map off, Map > Background Maps > off
Drag Plot X and Plot Y onto detail (make them dimensions) and change the marks to a circle.
This is the vague structure of the baseball map that we need.
We could drag events onto colour to bring the visual to life more and cross reference some data points with whats seen on the site. What I will do is filter out anything that isnt a single, double, triple or home run.
The final thing we will want to do is map onto the background an image of the pitch.
Now this is the tricky bit, mainly because…. all stadium layouts are slightly different. So it is a case of what to choose. Now by all means you could go find a standardised one similar to Jacobs at the start of the blog, but for today lets find a method to take it from the site directly.
To do this I am going to use the SVG Export extension (It is one of my favourite extensions to help mirror branding!)
All you have to do is add your extension to the page you are looking at,
When you run this, it will give you all the SVG icons from the desired webpage.
It’s worth noting at this stage, you will want to change your selection of what pitch background you want to map.
One of these images will be the pitch that we will use to map.
Click the ellipse and download the file with a height and width of 500. Make sure to change the format to PNG before downloading, so that we can use it within Tableau.
I’ve used co-ordinates that as best accurately map the points onto the pitch as possible. Of course these may change dependent on the scale of your own pitch and co-ordinates, but I hope they provide good guidance.
The main final thing is to fix your axis as well as add in any final desired tooltips.
and there we have it, our first baseball chart in Tableau.
Perhaps in future weeks we look at retrieving the data in a more automated way or having the option to map multiple different pitches.
Let me know how you get on with this one – I can be found on the usual channels.