In my last post I produced some NBA shot charts in R using data scraped from stats.nba.com and ggplot2. This time I extracted all shot location data available for 490 players and linked it to a Tableau dashboard.
The first dashboard shows each shot attempted during the 2014-15 NBA Regular Season. On the right, it is possible to select team, player, shot type, shot zone and shot range. The table above the chart is also updated in line with the filter selection (click on the image to open the dashboard on a new window).
Another way to visualise this data is to group shots by shot zone. The dashboard below displays shot accuracy for each zone of the court. Note how the “Above the Break 3” area is located in the 2-point region. This is due to the fact that I averaged the X and Y coordinates of all shots taken from “Above the Break 3”. Shots taken from the left and right bring the average location inside the 2-point region.
Select the NBA court jpeg file and click Edit. The X field limits should be -250 to 250 and Y field limits should be -52 to 418.
In order to scrape and save the shot data using R, I created a function that extract data for a player and ran it in a loop. This will take some time to run depending on the memory you have available (when I first ran this code it took about 40 minutes). Once we have the data in a readable format it is time to link it to Tableau.
## load the file players.csv from my NBA Shot Chart project repo in my GitHub page.
Feel free to comment or ask any questions in the comment section below.
Load the file into R and save it into a data frame object called players.
Re run the code and it should work.
I’ve downloaded the file from GitHub but I’m unsure how to load in a csv file. Could you help me out?
If you are talking about players.csv file, read my other comment. If you are talking about the .R file, you can load it using Rstudio and run it.
Where do I put the ‘players.csv’ file once I download it?
This file is a list of all players and playerIDs with existing shot data and can be used as a reference for player id’s. If you want to run charts for Kevin Durant for example, search for his name on the file and you will find the id 201142.
This is really awesome! How were you able to figure out the URL for the JSON?
I followed these steps from Greg Reda’s post. You basically need to start the developer tools in Chrome and go to the page that has the data you are looking for.
Does this still work? Or has NBA.com masked data?
It should work still. NBA may have changed the tables structure since then.