Last active
December 18, 2015 15:11
-
-
Save abresler/d1f4f84f513ba443124d to your computer and use it in GitHub Desktop.
Simple visualization of NBA player movements using the DiagrammeR R package
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(DiagrammeR) | |
library(magrittr) | |
library(visNetwork) | |
moments <- | |
'https://gist.githubusercontent.com/rich-iannone/9c4e50cd5d811b841349/raw/65bc97ddac2f9e52a4252fcf2060007fe1afeb7d/moment.csv' %>% | |
read_csv() | |
# Generate the court | |
court <- | |
create_nodes(nodes = "court", | |
x = 0, | |
y = 0, | |
group = "court") | |
# Provide the location the ball | |
ball <- | |
DiagrammeR::create_nodes(nodes = "ball", | |
x = moments$x_loc[1] * 19.92, | |
y = moments$y_loc[1] * 19.8, | |
group = "ball") | |
# Provide the location the players | |
players <- | |
create_nodes(nodes = moments$player_id[-1], | |
x = moments$x_loc[-1] * 19.92, | |
y = moments$y_loc[-1] * 19.8, | |
group = letters[1:10] | |
) | |
pics <- | |
moments %>% | |
slice(-1) %>% | |
select(team_id, player_id) %>% | |
mutate(pics = moments$player_id[-1] %>% paste0('http://stats.nba.com/media/players/230x185/',.,'.png') | |
) %>% | |
.$pics | |
# Combine the node data frames | |
combined <- combine_nodes(court, players, ball) | |
# Render using 'visNetwork' | |
visNetwork(nodes = combined) %>% | |
visNodes(physics = TRUE, | |
fixed = TRUE) %>% | |
visPhysics(stabilization = list(enabled = TRUE, | |
onlyDynamicEdges = FALSE, | |
fit = TRUE)) %>% | |
visGroups(groupname = "court", | |
shape = "image", | |
image = "http://stats.nba.com/media/img/fullcourt.svg", | |
size = 1000) %>% | |
visGroups(groupname = "ball", | |
shape = "image", | |
image = "https://raw.githubusercontent.com/rich-iannone/DiagrammeR/master/inst/examples/orange-circle.png", | |
size = 30) %>% | |
visGroups(groupname = "a", | |
shape = "image", | |
image = pics[1], | |
size = 50) %>% | |
visGroups(groupname = "b", | |
shape = "image", | |
image = pics[2], | |
size = 50) %>% | |
visGroups(groupname = "c", | |
shape = "image", | |
image = pics[3], | |
size = 50) %>% | |
visGroups(groupname = "d", | |
shape = "image", | |
image = pics[4], | |
size = 50) %>% | |
visGroups(groupname = "e", | |
shape = "image", | |
image = pics[5], | |
size = 50) %>% | |
visGroups(groupname = "f", | |
shape = "image", | |
image = pics[6], | |
size = 50) %>% | |
visGroups(groupname = "g", | |
shape = "image", | |
image = pics[7], | |
size = 50) %>% | |
visGroups(groupname = "h", | |
shape = "image", | |
image = pics[8], | |
size = 50) %>% | |
visGroups(groupname = "i", | |
shape = "image", | |
image = pics[9], | |
size = 50) %>% | |
visGroups(groupname = "j", | |
shape = "image", | |
image = pics[10], | |
size = 50) %>% | |
visInteraction(dragNodes = FALSE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
team_id | player_id | x_loc | y_loc | radius | moment | game_clock | shot_clock | player_name | player_jersey | |
---|---|---|---|---|---|---|---|---|---|---|
-1 | -1 | 43.51745 | 10.76997 | 1.11823 | 0 | 715.32 | 19 | ball | NA | |
1610612745 | 1891 | 43.21625 | 12.94610 | 0.00000 | 0 | 715.32 | 19 | Jason Terry | 31 | |
1610612745 | 2772 | 90.84496 | 7.79534 | 0.00000 | 0 | 715.32 | 19 | Trevor Ariza | 1 | |
1610612745 | 2730 | 77.19964 | 34.36718 | 0.00000 | 0 | 715.32 | 19 | Dwight Howard | 12 | |
1610612745 | 2746 | 46.24382 | 21.14748 | 0.00000 | 0 | 715.32 | 19 | Josh Smith | 5 | |
1610612745 | 201935 | 81.09920 | 48.10742 | 0.00000 | 0 | 715.32 | 19 | James Harden | 13 | |
1610612746 | 2440 | 88.12605 | 11.23036 | 0.00000 | 0 | 715.32 | 19 | Matt Barnes | 22 | |
1610612746 | 200755 | 84.41011 | 43.47075 | 0.00000 | 0 | 715.32 | 19 | JJ Redick | 4 | |
1610612746 | 101108 | 46.18569 | 16.49072 | 0.00000 | 0 | 715.32 | 19 | Chris Paul | 3 | |
1610612746 | 201599 | 78.64683 | 31.87798 | 0.00000 | 0 | 715.32 | 19 | DeAndre Jordan | 6 | |
1610612746 | 201933 | 65.89714 | 25.57281 | 0.00000 | 0 | 715.32 | 19 | Blake Griffin | 32 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment