Everyone’s excited about the newly released NBA player movement data that’s been released – at least I am. I stumbled across this post which shows how to visualize player movement data in Python, but I wanted to figure out how to do the same in R. We’ll use a combination of several R packages including RCurl, jsonlite, png and plotrix.

We will be replicating a play in R while shadowing James Harden (#13):

First, we’ll read in the movement data and plot a basketball court.

We have a basketball court!

full court image

Next, we need to overlay James’ movement on this image.
Here we will capture the home and away team information.

This will capture the movement moment data and provide column headers when we are done compiling all of the data.

Next we add the player name information to each set of movement data.

Here is James Harden’s movement with green being earlier in the play and black being later.

We can also calculate player distance traveled:

James Harden moved the most around the court on that play while JJ Redick moved the second most.

player_id lastname firstname jersey position totalDist
1 201935 Harden James 13 G 197.4482
2 200755 Redick JJ 4 G 184.5041
3 101108 Paul Chris 3 G 176.1983
4 1891 Terry Jason 31 G 173.3089
5 2746 Smith Josh 5 F 162.2261
6 2440 Barnes Matt 22 F 161.9764
7 2772 Ariza Trevor 1 F 153.3894
8 201933 Griffin Blake 32 F 153.0766
9 2730 Howard Dwight 12 C 123.4396
10 201599 Jordan DeAndre 6 C 119.9199

Please tweet at us and let us know if you have any improvements upon this work or other ideas. The possibilities are endless with what we can start to do with this data.

About the author

Leave a Reply