-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanthony_gill.R
379 lines (333 loc) · 12.7 KB
/
anthony_gill.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
###############################################
# 15th man of the year
# Session Info:
# R version 4.0.3 (2020-10-10)
# Platform: x86_64-w64-mingw32/x64 (64-bit)
# Running under: Windows 10 x64 (build 19042)
###############################################
# Load packages
library(tidyverse)
library(nbastatR)
library(extrafont)
library(gt)
# set seed
set.seed(20483789)
# for downloads
Sys.setenv(VROOM_CONNECTION_SIZE = 131072*3)
# get team stats
test <- teams_rosters(seasons = 2022, nest_data = F, return_message = T)
# defensive points saved, offensive points added
# offensive, denfensive rating
#TPA by minutes
# players_tables(seasons = 2022, players = c("Anthony Gill"), tables = c("game logs", ) modes = c("PerGame", "Per36", "Per100Possessions"), measures = c("Base", "Advanced"), assign_to_environment = TRUE)
bref_players_stats(seasons = 2022, tables = c("advanced", "totals", "per_game", "per_minute", "per_poss"), only_totals = FALSE)
gill_advanced <- dataBREFPlayerAdvanced %>%
filter(namePlayer %in% c(
"Anthony Gill" # 1 Wizards
, "Jevon Carter" # 2 Nets
, "Charles Bassey" # 3 Sixers
, "Dalano Banton" # 4 Raptors
, "Miles McBride" # 5 Knicks
, "Bruno Fernando" # 6 Celtics
, "Matt Thomas" # 7 Bulls
, "Rodney Hood" # 8 Bucks
, "Dylan Windler" # 9 Cavs
, "Goga Bitadze" # 10 Pacers
, "Saben Lee" # 11 Pistons
, "KZ Okpala" # 12 Heat
, "Kai Jones" # 13 Hornets
, "Timothe Luwawu-Cabarrot" # 14 Hawks
, "Moritz Wagner" # 15 Magic
, "Elijah Hughes" # 16 Jazz
, "Bol Bol" # 17 Nuggets
, "Leandro Bolmaro" # 18 Timberwolves
, "CJ Elleby" # 19 Trailblazers
, "Isaiah Roby" # 20 Thunder
, "Elfrid Payton" # 21 Suns, Payton is actually 14 but 15 has been injured since November
, "Moses Moody" # 22 Warriors
, "DeAndre Jordan" # 23 Lakers
, "Justise Winslow" # 24 Clippers
, "Jahmi'us Ramsey" # 25 Kings
, "Jarrett Culver" # 26 Grizzlies
, "Marquese Chriss" # 27 Mavericks Just signed a two year deal after several 10 days with the Mavs
, "Didi Louzada" # 28 Pelicans
, "Jock Landale" # 29 Spurs
, "Usman Garuba" # 30 Rockets
)) %>%
mutate(bref_url = glue::glue("https://www.basketball-reference.com/players/{stringr::str_sub(idPlayerNBA, 1, 1)}/{idPlayerNBA}.html")
# , bref_link = glue::glue('<a href="{bref_url}">{namePlayer}</a>')
# , stat = "Advanced"
)
gill_pergame <- dataBREFPlayerPerGame %>%
filter(namePlayer %in% c(
"Anthony Gill" # 1 Wizards
, "Jevon Carter" # 2 Nets
, "Charles Bassey" # 3 Sixers
, "Dalano Banton" # 4 Raptors
, "Miles McBride" # 5 Knicks
, "Bruno Fernando" # 6 Celtics
, "Matt Thomas" # 7 Bulls
, "Rodney Hood" # 8 Bucks
, "Dylan Windler" # 9 Cavs
, "Goga Bitadze" # 10 Pacers
, "Saben Lee" # 11 Pistons
, "KZ Okpala" # 12 Heat
, "Kai Jones" # 13 Hornets
, "Timothe Luwawu-Cabarrot" # 14 Hawks
, "Moritz Wagner" # 15 Magic
, "Elijah Hughes" # 16 Jazz
, "Bol Bol" # 17 Nuggets
, "Leandro Bolmaro" # 18 Timberwolves
, "CJ Elleby" # 19 Trailblazers
, "Isaiah Roby" # 20 Thunder
, "Elfrid Payton" # 21 Suns, Payton is actually 14 but 15 has been injured since November
, "Moses Moody" # 22 Warriors
, "DeAndre Jordan" # 23 Lakers
, "Justise Winslow" # 24 Clippers
, "Jahmi'us Ramsey" # 25 Kings
, "Jarrett Culver" # 26 Grizzlies
, "Marquese Chriss" # 27 Mavericks Just signed a two year deal after several 10 days with the Mavs
, "Didi Louzada" # 28 Pelicans
, "Jock Landale" # 29 Spurs
, "Usman Garuba" # 30 Rockets
)) %>%
mutate(bref_url = glue::glue("https://www.basketball-reference.com/players/{stringr::str_sub(idPlayerNBA, 1, 1)}/{idPlayerNBA}.html")
# , bref_link = glue::glue('<a href="{bref_url}">{namePlayer}</a>')
# , stat = "Per Game"
)
gill_perminute <- dataBREFPlayerPerMinute %>%
filter(namePlayer %in% c(
"Anthony Gill" # 1 Wizards
, "Jevon Carter" # 2 Nets
, "Charles Bassey" # 3 Sixers
, "Dalano Banton" # 4 Raptors
, "Miles McBride" # 5 Knicks
, "Bruno Fernando" # 6 Celtics
, "Matt Thomas" # 7 Bulls
, "Rodney Hood" # 8 Bucks
, "Dylan Windler" # 9 Cavs
, "Goga Bitadze" # 10 Pacers
, "Saben Lee" # 11 Pistons
, "KZ Okpala" # 12 Heat
, "Kai Jones" # 13 Hornets
, "Timothe Luwawu-Cabarrot" # 14 Hawks
, "Moritz Wagner" # 15 Magic
, "Elijah Hughes" # 16 Jazz
, "Bol Bol" # 17 Nuggets
, "Leandro Bolmaro" # 18 Timberwolves
, "CJ Elleby" # 19 Trailblazers
, "Isaiah Roby" # 20 Thunder
, "Elfrid Payton" # 21 Suns, Payton is actually 14 but 15 has been injured since November
, "Moses Moody" # 22 Warriors
, "DeAndre Jordan" # 23 Lakers
, "Justise Winslow" # 24 Clippers
, "Jahmi'us Ramsey" # 25 Kings
, "Jarrett Culver" # 26 Grizzlies
, "Marquese Chriss" # 27 Mavericks Just signed a two year deal after several 10 days with the Mavs
, "Didi Louzada" # 28 Pelicans
, "Jock Landale" # 29 Spurs
, "Usman Garuba" # 30 Rockets
)) %>%
mutate(bref_url = glue::glue("https://www.basketball-reference.com/players/{stringr::str_sub(idPlayerNBA, 1, 1)}/{idPlayerNBA}.html")
# , bref_link = glue::glue('<a href="{bref_url}">{namePlayer}</a>')
# , stat = "Per 36"
)
# code inspired by Mara Averick
# https://rpubs.com/maraaverick/479895
gill_advanced %>%
left_join(select(gill_perminute, namePlayer, ptsPerMinute, astPerMinute, pfPerMinute)) %>%
left_join(select(gill_pergame, namePlayer, minutesPerGame)) %>%
select(namePlayer
, urlPlayerHeadshot
, slugTeamBREF
, slugPosition
, minutesPerGame
, ptsPerMinute
, astPerMinute
, ratioBPM
# , "bref_url" = urlPlayerStats
) %>%
arrange(desc(ratioBPM)) %>%
# gt::gt(rowname_col = "namePlayer") %>%
gt() %>%
tab_header(
title = md("**15th best player on each team**")
) %>%
cols_label(
namePlayer = md("**Player**")
, urlPlayerHeadshot = md("")
, slugTeamBREF = md("**Team**")
, slugPosition = md("**Position**")
, minutesPerGame = md("**Minutes per game**")
, ptsPerMinute = md("**Points per 36**")
, astPerMinute = md("**Assists per 36**")
, ratioBPM = md("**+/-**")
# , bref_url = md("**Link**")
) %>%
# text_transform(
# locations = cells_body(vars(bref_url)),
# fn = function(x) {
# sprintf("<a href=%s>profile</a>", x)
# }
# ) %>%
text_transform(
locations = cells_body(vars(urlPlayerHeadshot)),
fn = function(x) {
web_image(url = x)
}
) %>%
tab_source_note(
md("source: [basketball-reference.com](https://www.basketball-reference.com)")
) %>%
tab_source_note(
md("[wizardspoints.substack.com](wizardspoints.substack.com)")
) %>%
gtsave(
"table.png", expand = 10,
)
gill_advanced_rank <- gill_advanced %>%
left_join(gill_perminute) %>%
column_to_rownames(., var = "namePlayer") %>%
select(agePlayer
, countGames
, ratioPER
, pctTrueShooting
, pctUSG
, ratioWSPer48
, ratioVORP
, pfPerMinute
, ptsPerMinute
, stlPerMinute
, blkPerMinute
, astPerMinute
, trbPerMinute
, slugPosition
)
gill_advanced_ranks <- mutate_all(gill_advanced_rank[1:22], funs(rank(., ties.method="first")))
gill_long <- gill_advanced_rank %>%
rownames_to_column(., var = "name") %>%
rename("Age" = agePlayer
, "Games Played" = countGames
, "PER" = ratioPER
, "True Shooting" = pctTrueShooting
, "Usage" = pctUSG
, "Win Shares per 48" = ratioWSPer48
, "VORP" = ratioVORP
, "Personal Fouls per 36 min." = pfPerMinute
, "Points per 36 min." = ptsPerMinute
, "Steals per 36 min." = stlPerMinute
, "Blocks per 36 min." = blkPerMinute
, "Assists per 36 min." = astPerMinute
, "Total rebounds per 36 min." = trbPerMinute
, "Position" = slugPosition
) %>%
pivot_longer(
cols = 2:14,
names_to = "var"
, values_to = "values") %>%
mutate(stat = ifelse(var %in% c("VORP", "Win Shares per 48", "Usage", "True Shooting")==T, "Advanced"
, ifelse(var %in% c("Age", "Games Played")==T, "Count", "Per 36")))
p1 <- gill_long %>%
filter(var %in% c("VORP", "Win Shares per 48", "Usage", "True Shooting")) %>%
mutate(Gill = ifelse(name == "Anthony Gill", "Gill", "Not Gill")) %>%
ggplot(aes(x = values, y = var)) +
geom_point(aes(fill = Gill, col = Gill
, alpha = ifelse(name == "Anthony Gill", 1, 0.8)
), size = 5, shape = 21) +
scale_fill_manual(values = c("#BA0C2F", "black")) +
scale_color_manual(values = c("#BA0C2F", "black")) +
theme_minimal() +
theme(legend.position = "NA"
, text = element_text(size = 20)) +
labs(x = "", y = ""
, title = "15th Ranked Players Across Multiple Stats"
, subtitle = "Anthony Gill is highlighted in red"
# , caption = "Source: basketball-reference.com\nwizardspoints.substack.com"
)
p2 <- gill_long %>%
filter(!var %in% c("VORP", "Win Shares per 48", "Usage", "True Shooting", "Age", "Games Played")) %>%
mutate(Gill = ifelse(name == "Anthony Gill", "Gill", "Not Gill")) %>%
ggplot(aes(x = values, y = var)) +
geom_point(aes(fill = Gill, col = Gill
, alpha = ifelse(name == "Anthony Gill", 1, 0.8)
), size = 5, shape = 21) +
scale_fill_manual(values = c("#BA0C2F", "black")) +
scale_color_manual(values = c("#BA0C2F", "black")) +
theme_minimal() +
theme(legend.position = "NA"
, text = element_text(size = 20)) +
labs(x = "", y = "", caption = "Source: basketball-reference.com\nwizardspoints.substack.com")
p1_p1 <- cowplot::plot_grid(plotlist = list(p1, p2)
, nrow = 2
, ncol = 1
, label_size = 14
, label_fontface = "plain"
, label_fontfamily = "Corbel")
ggsave("Gill Stats.png", p1_p1, width = 10, height = 8, dpi = 300, type = 'cairo')
gill_advanced %>%
mutate(Gill = ifelse(namePlayer == "Anthony Gill", "Gill", "Not Gill")) %>%
ggplot(aes(x = ratioOBPM, y = ratioDBPM)) +
geom_point(aes(fill = Gill, col = Gill
, alpha = ifelse(namePlayer == "Anthony Gill", 1, 0.8)
), size = 5, shape = 21)
# Get each team's Net Rating from basketball-reference
url <- "https://www.basketball-reference.com/leagues/NBA_2022_per_poss.html"
bref_tables <- url %>%
read_html() %>%
html_table()
net_df <- bref_tables[[1]]
net_df2 <- net_df[net_df$Player %in% c(
"Anthony Gill" # 1 Wizards
, "Jevon Carter" # 2 Nets
, "Charles Bassey" # 3 Sixers
, "Dalano Banton" # 4 Raptors
, "Miles McBride" # 5 Knicks
, "Bruno Fernando" # 6 Celtics
, "Matt Thomas" # 7 Bulls
, "Rodney Hood" # 8 Bucks
, "Dylan Windler" # 9 Cavs
, "Goga Bitadze" # 10 Pacers
, "Saben Lee" # 11 Pistons
, "KZ Okpala" # 12 Heat
, "Kai Jones" # 13 Hornets
, "Timothé Luwawu-Cabarrot" # 14 Hawks
, "Moritz Wagner" # 15 Magic
, "Elijah Hughes" # 16 Jazz
, "Bol Bol" # 17 Nuggets
, "Leandro Bolmaro" # 18 Timberwolves
, "CJ Elleby" # 19 Trailblazers
, "Isaiah Roby" # 20 Thunder
, "Elfrid Payton" # 21 Suns, Payton is actually 14 but 15 has been injured since November
, "Moses Moody" # 22 Warriors
, "DeAndre Jordan" # 23 Lakers
, "Justise Winslow" # 24 Clippers
, "Jahmi'us Ramsey" # 25 Kings
, "Jarrett Culver" # 26 Grizzlies
, "Marquese Chriss" # 27 Mavericks Just signed a two year deal after several 10 days with the Mavs
, "Didi Louzada" # 28 Pelicans
, "Jock Landale" # 29 Spurs
, "Usman Garuba" # 30 Rockets
)
, ]
net_df2 <- net_df2[,-30]
net_df2 <- net_df2 %>%
mutate(Gill = ifelse(Player == "Anthony Gill", "Gill", "Not Gill")
, ORtg = as.numeric(ORtg)
, DRtg = as.numeric(DRtg)
)
net_df3 <- net_df2 %>% left_join(select(gill_advanced, "Player" = namePlayer, urlPlayerHeadshot))
g1 <- subset(net_df2, Player == "Anthony Gill")
p4 <- net_df2 %>%
ggplot(aes(x = ORtg, y = DRtg)) +
geom_point(aes(fill = Gill, col = Gill
), size = 5, shape = 21) +
scale_fill_manual(values = c("#BA0C2F", "grey")) +
scale_color_manual(values = c("#BA0C2F", "grey")) +
geom_smooth(method = "lm", se = F, col = "dark grey") +
ggrepel::geom_text_repel(data=g1, label="Anthony Gill") +
theme_minimal() +
theme(legend.position = "NA"
, text = element_text(size = 20)) +
labs(x = "Offensive Rating", y = "Defensive Rating", title = "15th Ranked Players by\nOffensive and Defensive Rating"
, caption = "Source: basketball-reference.com\nwizardspoints.substack.com")
ggsave("Net Rating.png", p4, width = 10, height = 8, dpi = 300, type = 'cairo')