0

So I'm going to start from the very beginning so sorry if it gets a little lengthy. My runnable code for you all is...

structure(list(X1 = 0:29, seasoncode = c("E2015", "E2015", "E2015", 
"E2015", "E2015", "E2015", "E2015", "E2015", "E2015", "E2015", 
"E2015", "E2015", "E2015", "E2015", "E2015", "E2015", "E2015", 
"E2015", "E2015", "E2015", "E2015", "E2015", "E2015", "E2015", 
"E2015", "E2015", "E2015", "E2015", "E2015", "E2015"), gamecode = c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), pbp_id = 1:30, 
period = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L), game_minute = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), game_clock = structure(c(36000, 
32400, 32400, 32400, 32400, 32220, 32220, 32160, 32160, 31980, 
30960, 30780, 30360, 30240, 29100, 28980, 28500, 28500, 27720, 
27720, 27600, 27540, 27360, 27240, 26940, 26820, 26580, 26580, 
26100, 25980), class = c("hms", "difftime"), units = "secs"), 
team_id = c(NA, "MAD", "MAD", "KHI", "KHI", "KHI", "KHI", 
"KHI", "MAD", "KHI", "KHI", "KHI", "MAD", "KHI", "KHI", "KHI", 
"MAD", "KHI", "MAD", "MAD", "KHI", "KHI", "KHI", "KHI", "KHI", 
"MAD", "KHI", "MAD", "MAD", "MAD"), team_name = c(NA, "Real Madrid", 
"Real Madrid", "Khimki Moscow Region", "Khimki Moscow Region", 
"Khimki Moscow Region", "Khimki Moscow Region", "Khimki Moscow Region", 
"Real Madrid", "Khimki Moscow Region", "Khimki Moscow Region", 
"Khimki Moscow Region", "Real Madrid", "Khimki Moscow Region", 
"Khimki Moscow Region", "Khimki Moscow Region", "Real Madrid", 
"Khimki Moscow Region", "Real Madrid", "Real Madrid", "Khimki Moscow Region", 
"Khimki Moscow Region", "Khimki Moscow Region", "Khimki Moscow Region", 
"Khimki Moscow Region", "Real Madrid", "Khimki Moscow Region", 
"Real Madrid", "Real Madrid", "Real Madrid"), player_id = c(NA, 
"P005927", "P005927", "P005800", "P001438", "P005800", "P001438", 
NA, "PAAX", "PBFX", "PBFX", "P001438", "PTGB", "P001438", 
"P005800", "P001438", "PBMT", "PBGV", "PJCC", "PJCC", "P005800", 
"P003367", "P003367", "P005800", "P003367", "P005927", "P005800", 
"PAAX", "PBMT", "PTGB"), player_name = c(NA, "AYON, GUSTAVO", 
"AYON, GUSTAVO", "HONEYCUTT, TYLER", "AUGUSTINE, JAMES", 
"HONEYCUTT, TYLER", "AUGUSTINE, JAMES", NA, "REYES, FELIPE", 
"MONIA, SERGEY", "MONIA, SERGEY", "AUGUSTINE, JAMES", "LLULL, SERGIO", 
"AUGUSTINE, JAMES", "HONEYCUTT, TYLER", "AUGUSTINE, JAMES", 
"FERNANDEZ, RUDY", "VYALTSEV, EGOR", "MACIULIS, JONAS", "MACIULIS, JONAS", 
"HONEYCUTT, TYLER", "RICE, TYRESE", "RICE, TYRESE", "HONEYCUTT, TYLER", 
"RICE, TYRESE", "AYON, GUSTAVO", "HONEYCUTT, TYLER", "REYES, FELIPE", 
"FERNANDEZ, RUDY", "LLULL, SERGIO"), player_jersey_number = c(NA, 
14L, 14L, 33L, 5L, 33L, 5L, NA, 9L, 12L, 12L, 5L, 23L, 5L, 
33L, 5L, 5L, 9L, 8L, 8L, 33L, 0L, 0L, 33L, 0L, 14L, 33L, 
9L, 5L, 23L), event_desc_id = c("BP", "2FGAB", "AG", "FV", 
"D", "3FGA", "O", "TO", "2FGA", "D", "3FGM", "AS", "TO", 
"ST", "3FGM", "AS", "RV", "CM", "2FGAB", "AG", "FV", "D", 
"3FGA", "O", "2FGA", "D", "CM", "RV", "3FGM", "AS"), event_desc = c("Begin Period", 
"Missed Two Pointer (0/1 -  0 pt)", "Shot Rejected (1)", 
"Block (1)", "Def Rebound (1)", "Missed Three Pointer (0/1 -  0 pt)", 
"Off Rebound (1)", "Turnover (1)", "Missed Two Pointer (0/1 -  0 pt)", 
"Def Rebound (1)", "Three Pointer (1/1 -  3 pt)", "Assist (1)", 
"Turnover (1)", "Steal (1)", "Three Pointer (1/2 -  3 pt)", 
"Assist (2)", "Foul Drawn (1)", "Foul (1)", "Missed Two Pointer (0/1 -  0 pt)", 
"Shot Rejected (1)", "Block (2)", "Def Rebound (1)", "Missed Three Pointer (0/1 -  0 pt)", 
"Off Rebound (1)", "Missed Two Pointer (0/1 -  0 pt)", "Def Rebound (1)", 
"Foul (1)", "Foul Drawn (1)", "Three Pointer (1/1 -  3 pt)", 
"Assist (1)"), score_home = c(NA, 0L, NA, NA, NA, 0L, NA, 
NA, 0L, NA, 3L, NA, NA, NA, 6L, NA, NA, NA, 6L, NA, NA, NA, 
6L, NA, 6L, NA, NA, NA, 6L, NA), score_visitor = c(NA, 0L, 
NA, NA, NA, 0L, NA, NA, 0L, NA, 0L, NA, NA, NA, 0L, NA, NA, 
NA, 0L, NA, NA, NA, 0L, NA, 0L, NA, NA, NA, 3L, NA), coord_x = c(NA, 
0L, NA, NA, NA, -501L, NA, NA, 269L, NA, -163L, NA, NA, NA, 
-690L, NA, NA, NA, 18L, NA, NA, NA, -539L, NA, 94L, NA, NA, 
NA, 702L, NA), coord_y = c(NA, 181L, NA, NA, NA, 602L, NA, 
NA, 188L, NA, 771L, NA, NA, NA, 319L, NA, NA, NA, 232L, NA, 
NA, NA, 526L, NA, 37L, NA, NA, NA, 370L, NA), `Points off OREB` = c(NA, 
NA, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), 
TWO.FGA = c(NA, NA, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 
0L, 0L, 0L), TWO.FGM = c(NA, NA, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L), THREE.FGA = c(NA, NA, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), THREE.FGM = c(NA, 
NA, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), 
Missed.Attemts = c(NA, NA, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 
0L, 0L, 0L, 0L, 0L), Made.Points = c(NA, NA, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("X1", 
"seasoncode", "gamecode", "pbp_id", "period", "game_minute", 
"game_clock", "team_id", "team_name", "player_id", "player_name", 
"player_jersey_number", "event_desc_id", "event_desc", "score_home", 
"score_visitor", "coord_x", "coord_y", "Points off OREB", "TWO.FGA", 
"TWO.FGM", "THREE.FGA", "THREE.FGM", "Missed.Attemts", "Made.Points"
), row.names = c(NA, -30L), class = c("tbl_df", "tbl", "data.frame"
))

It ends up looking something like this...

gamecode    pbp_id  period  team_id team_name   player_id   player_name event_desc_id   event_desc  coord_x coord_y Points off OREB TWO.FGA TWO.FGM THREE.FGA   THREE.FGM   Missed.Attemts  Made.Points

1              7       1        KHI Khimki Moscow Region    P001438 AUGUSTINE, JAMES    5    O  Off Rebound (1)                     0   0   0   0   0   0   0
1              8       1        KHI Khimki Moscow Region                TO  Turnover (1)                                            0   0   0   0   0   0   0
1              9       1        MAD Real Madrid PAAX    REYES, FELIPE   9   2FGA    Missed Two Pointer (0/1 -  0 pt)                0   0   269 188 0   0   0   0   0   0   0
1             10       1        KHI Khimki Moscow Region    PBFX    MONIA, SERGEY   12  D   Def Rebound (1)                         0   0   0   0   0   0   0
1             11       1        KHI Khimki Moscow Region    PBFX    MONIA, SERGEY   12  3FGM    Three Pointer (1/1 -  3 pt)         3   0   -163    771 0   0   0   0   0   0   0
1             12       1        KHI Khimki Moscow Region    P001438 AUGUSTINE, JAMES    5   AS  Assist (1)                          0   0   0   0   0   0   0
1             13       1        MAD Real Madrid PTGB    LLULL, SERGIO   23  TO  Turnover (1)                                        0   0   0   0   0   0   0
1             14       1        KHI Khimki Moscow Region    P001438 AUGUSTINE, JAMES    5   ST  Steal (1)                       0   0   0   0   0   0   0
1             15       1        KHI Khimki Moscow Region    P005800 HONEYCUTT, TYLER    33  3FGM    Three Pointer (1/2 -  3 pt) 6   0   -690    319 0   0   0   0   0   0   0
1             16       1        KHI Khimki Moscow Region    P001438 AUGUSTINE, JAMES    5   AS  Assist (2)                      0   0   0   0   0   0   0   
1             17       1        MAD Real Madrid PBMT    FERNANDEZ, RUDY 5   RV  Foul Drawn (1)                                      0   0   0   0   0   0   0   
1             18       1        KHI Khimki Moscow Region    PBGV    VYALTSEV, EGOR  9   CM  Foul (1)                            0   0   0   0   0   0   0
1             19       1        MAD Real Madrid PJCC    MACIULIS, JONAS 8   2FGAB   Missed Two Pointer (0/1 -  0 pt)                6   0   18  232 0   0   0   0   0   0   0
1             20       1        MAD Real Madrid PJCC    MACIULIS, JONAS 8   AG  Shot Rejected (1)                                   0   0   0   0   0   0   0
1             21       1        KHI Khimki Moscow Region    P005800 HONEYCUTT, TYLER    33  FV  Block (2)                       0   0   0   0   0   0   0
1             22       1        KHI Khimki Moscow Region    P003367 RICE, TYRESE    0   D   Def Rebound (1)                     0   0   0   0   0   0   0
1             23       1        KHI Khimki Moscow Region    P003367 RICE, TYRESE    0   3FGA    Missed Three Pointer (0/1 -  0 pt)  6   0   -539    526 0   0   0   0   0   0   0
1             24       1        KHI Khimki Moscow Region    P005800 HONEYCUTT, TYLER    33  O   Off Rebound (1)                 0   0   0   0   0   0   0
1             25       1        KHI Khimki Moscow Region    P003367 RICE, TYRESE    0   2FGA    Missed Two Pointer (0/1 -  0 pt)    6   0   94  37  0   1   0   0   0   1   0

The formatting got a little messy. It's roughly 100,000 rows with a bunch of teams and names and games and everything. Not all of the columns have values. For example, there's no "x_coord" for assists, turnovers, steals... Another thing to note is that from column "Points off OREB" till the very last one are columns made by me in excel that just has a 1 or 0 depending on if it's true or false for that particular row.

THE END GOAL. I want to make a data plot or heat map or any visual with data from the "coord_x" and "coord_y" column values only for FG% for 2's and 3's for shots off offensive rebounds, all with a basketball court in the background. I imagine the plot to be a basketball court with green dots being makes and red misses. Or a cool heat map looking thing.

I feel like I'm close. I have a plot for green dots for made 3's, red dots for missed 3's, and a basketball court

#########################
MADE 3 SHOT MAP ATTEMPT
#########################
Team3Made<- subset(Practice_FGAM, gamecode >= 1 & THREE.FGM == 1)

x3M <- Team3Made$coord_x
y3M <- Team3Made$coord_y

p <- ggplot(data = Team3Made, aes(x3M, y3M))
p + geom_point(color='green')
Made3 <- p + geom_point(color='green')





###########################
MISSED 3 SHOT MAP ATTEMPT
###########################
Team3ATT<- subset(Practice_FGAM, gamecode >= 1 & THREE.FGA == 1)

x3A <- Team3ATT$coord_x
y3A <- Team3ATT$coord_y

m <- ggplot(data = Team3ATT, aes(x3A, y3A))
m + geom_point(color='red')
Missed3 <- m + geom_point(color='red')


############################
Basketball Court that's NOT TO SCALE
############################

library(grid)
library(jpeg)
library(RCurl)


courtImg.URL <- "https://thedatagame.files.wordpress.com/2016/03/nba_court.jpg"
court <- rasterGrob(readJPEG(getURLContent(https://thedatagame.files.wordpress.com/2016/03/nba_court.jpg)),
                width=unit(1,"npc"), height=unit(1,"npc"))

ggplot(Team3Made, aes(x3M, y3M)) + 
 annotation_custom(court, -250, 250, -50, 420) +
 xlim(-250, 250) +
 ylim(-50, 420) +
 geom_point(data = Team3Made, colour = "red", na.rm = TRUE)

My ideal end code doesn't work and leads to Error: Don't know how to add o to a plot

############################
Ideal End Code (ggplot = court, Missed3 = red dots, Made3 = green dots)
############################
ggplot(Team3Made, aes(x3M, y3M)) + 
 annotation_custom(court, -250, 250, -50, 420) +
 xlim(-250, 250) +
 ylim(-50, 420) +
 geom_point(data = Team3Made, colour = "red", na.rm = TRUE) + 
 plot(Missed3) +
 plot(Made3)

I would post pics but I'm new and it won't let me. Just imagine 1 plot with a bunch of red points behind an imaginary 3 point line, another one with green points, and a plot that looks just like the outline of a bball half court with the basket and backboard on the bottom side.

Now before I went on and did 2 pointers I wanted to try and overlay the Made3, Missed3, and the court onto one visual. I've read and tried everything and feel like I'm missing the simplest of steps somehow. So the question becomes what is the best way to accomplish this. I don't care if it's to scale right now because i can tweak with that later (although I won't turn down any help i can get)

Thank you so much and just wanted to say this is such a great community

Steven
  • 1
  • 1
  • This reminds me of something from http://blog.revolutionanalytics.com/2016/09/analyzing-nba-basketball-data-with-r.html I'm sure people who know how to do what you ask would be able to help if you gave them runnable code. Edit your post with the output of dput() on a subset of your data: dput(Practice_FGAM[1:30,]) # perhaps?? – Evan Friedland Apr 04 '17 at 18:45
  • I did the dput() code you gave me and it shortened my data. Now how do I put that into my question so people can have a runnable code? dput(Practice_FGAM[1:30,]) StackOverflow <- dput(Practice_FGAM[1:30,]) where StackOverflow gives the shortened code – Steven Apr 04 '17 at 22:57
  • Copy and paste the whole output (it will say structure) right into your question, similar to how you did before. You can try copy and pasting it in your own console and run it to make sure it runs as desired. – Evan Friedland Apr 04 '17 at 23:10
  • Did you try out your sample data? There are no x or y coordinates in the sections you pulled. I said 1:30 as an example but you should try and find parts of your dataset that have usable data for the bit you want to express. Perhaps dput(subset(Practice_FGAM, gamecode >= 1 & THREE.FGM == 1)) ?? – Evan Friedland Apr 05 '17 at 01:14

0 Answers0