-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.R
1826 lines (1403 loc) · 67.8 KB
/
app.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
library(tidyverse)
library(shiny)
library(gtools)
library(rintrojs)
library(plotly)
library(viridis)
# # Step 1: Let's get Rank players by court region-Step 3 in empirical-lpl-demo.Rmd
load("shot_data_step_2.rda")
load("full_lineups_w_mins.rda")
# For plotting court
source("discrete_court_regions-ggplot2.R")
# For Player Photos
available_players <- read_csv("available_players.csv")
find_player_by_name = function(n) {
filter(available_players, lower_name == tolower(n))
}
player_photo_url = function(player_id) {
paste0("https://stats.nba.com/media/players/230x185/", player_id, ".png")
}
# Notes for player_photo.R
# find_player_by_name() function outputs df of person_id for each player, which you'll use for
# grabbing NBA photos using urls
# Find unique lineup codes
lineup_code <- shot_data_step_2 %>%
select(lineup_code) %>%
filter(!is.na(lineup_code)) %>%
unique() %>%
pull()
# Western Teams
lineup_code_western <- c("NOP", "DAL", "DEN", "GSW", "HOU", "LAC", "LAL", "MIN", "OKC", "OKC", "UTA", "MEM", "SAC", "SAS", "POR", "PHX")
# Eastern Teams
lineup_code_eastern <- c("ATL", "BOS", "CLE", "CHI", "MIA", "MIL", "BKN", "NYK", "ORL", "IND", "PHI", "TOR", "DET", "CHA", "WAS")
# Divide Teams into Conferences
lineup_code_df <- data.frame(lineup_code = as.character(lineup_code))
# Eastern
east <- lineup_code_df %>%
mutate(lineup_code_team = sub("^([[:alpha:]]*).*", "\\1", lineup_code)) %>%
mutate(conference = if_else(lineup_code_team %in% lineup_code_western, "WEST", "EAST")) %>%
filter(conference == "EAST") %>%
pull(lineup_code) %>%
as.character()
# Sort
east <- mixedsort(east)
# Western
west <- lineup_code_df %>%
mutate(lineup_code_team = sub("^([[:alpha:]]*).*", "\\1", lineup_code)) %>%
mutate(conference = if_else(lineup_code_team %in% lineup_code_western, "WEST", "EAST")) %>%
filter(conference == "WEST") %>%
pull(lineup_code) %>%
as.character()
# Sort
west <- mixedsort(west)
# Beginning of Shiny App----
# Define UI for application that draws a histogram
ui <- fluidPage(
introjsUI(),
# Application title
titlePanel(tagList(
img(src = "nba-logo.png", height = 60, width = 100),
span("CHUCKERS: 16/17 LINEUP SHOT DISTRIBUTION EFFICIENCY",
span(
introBox(
actionButton("help",
label = "Help",
icon = icon("question"),
style="color: #fff; background-color: #B21212; border-color: #B21212"),
actionButton("Twitter",
label = "Twitter",
icon = icon("twitter"),
width = "80px",
onclick ="window.open(`https://twitter.com/howard_baek`, '_blank')",
style="color: #fff; background-color: #00acee; border-color: #00acee"),
actionButton("github",
label = "Code",
icon = icon("github"),
width = "80px",
onclick ="window.open(`https://github.com/howardbaek/nba-chuckers`, '_blank')",
style="color: #fff; background-color: #767676; border-color: #767676"),
data.step = 4,
data.intro = "Read more about these plots on the GitHub README by clicking `Code`"),
style = "position:absolute;right:2em;"
)
)
),
windowTitle = "NBA Chuckers App"),
# Sidebar with a slider input for number of bins
fluidRow(
column(2,
# Lineup Code
introBox(
selectInput(inputId = "choose_lineup_code",
label = "LINEUP CODE",
choices = list(
Eastern = east,
Western = west
),
selected = "SAC_1"),
data.step = 1,
data.intro = "Search for Lineup Combination"),
# Num of Shots Taken
h4(div(img(src="shot.png", width = 30), "NUM OF SHOTS")),
textOutput("num_shot"),
tags$head(tags$style("#num_shot{color: black;
font-size: 20px;
font-style: italic;
}"
)
),
# % of Shots Made
h4(div(img(src="basketball.png", width = 30), "FG%")),
textOutput("p_shot_made"),
tags$head(tags$style("#p_shot_made{color: red;
font-size: 18px;
font-style: italic;
}"
)
),
br(),
# Information about App
uiOutput("app_info"),
textOutput("app_info_3"),
uiOutput("app_info_4"),
uiOutput("app_info_5"),
h5("Built with",
img(src = "https://www.rstudio.com/wp-content/uploads/2014/04/shiny.png", height = "30px"),
"by",
img(src = "https://www.rstudio.com/wp-content/uploads/2014/07/RStudio-Logo-Blue-Gray.png", height = "30px")
),
introBox(
h4("GLOSSARY"),
data.step = 3,
data.intro = "Brief Description of Metrics used"
)
),
column(10,
tabsetPanel(
tabPanel("Rank", plotlyOutput("rank_plot")),
tabPanel("Rank Correspondence", plotOutput("rank_corr")),
tabPanel("Lineup Points Lost (LPL)", plotOutput("lpl")),
tabPanel("Player LPL (PLC)", plotOutput("plc")))
)
),
fluidRow(
column(2,
textOutput("tab_info_1"),
textOutput("tab_info_2"),
textOutput("tab_info_3"),
textOutput("tab_info_4"),
textOutput("tab_info_5")),
column(2,
introBox(
uiOutput("player_photo_first"),
data.step = 2,
data.intro = "Visualize Lineup Combination with Player Photos")
),
column(2,
uiOutput("player_photo_second")),
column(2,
uiOutput("player_photo_third")),
column(2,
uiOutput("player_photo_fourth")),
column(2,
uiOutput("player_photo_fifth"))
)
)
# Define server logic required to draw a histogram
server <- function(input, output, session) {
# Choose Lineup Code
my_lineup_code <- reactive({input$choose_lineup_code})
# NUM of Shots Made
output$num_shot <- renderText({
# Filter out for lineup shot data with Lineup Code
lineup_shot_data <- shot_data_step_2 %>%
filter(lineup_code == my_lineup_code())
lineup_shot_data %>%
nrow()
})
# Help
observeEvent(input$help,
introjs(session, options = list("nextLabel"="Next",
"prevLabel"="Previous",
"skipLabel"="Skip")
)
)
# % of Shots Made
output$p_shot_made <- renderText({
# Filter out for lineup shot data with Lineup Code
lineup_shot_data <- shot_data_step_2 %>%
filter(lineup_code == my_lineup_code())
# P Shot Made
p_shot_made <- lineup_shot_data %>%
summarise(isShotMade = mean(isShotMade)) %>%
pull(isShotMade)
p_shot_made <- 100 * round(p_shot_made, 3)
paste0(p_shot_made, "%")
})
# App Info
howard_baek <- a("Howard Baek", href="http://insidethetv.rbind.io/")
paper_url <- a("Chuckers: Measuring Lineup Shot Distribution
Optimality Using Spatial Allocative Efficiency Models",
href = "http://www.sloansportsconference.com/wp-content/uploads/2019/02/Chuckers-1.pdf")
ballr_url <- a("Todd Schneider's BallR", href = "https://github.com/toddwschneider/ballr")
output$app_info <- renderUI({
tagList("- App Developed by ", howard_baek)
})
output$app_info_3 <- renderText({
"- As Used in MIT Sloan Sports Analytics Conference Paper:"
})
output$app_info_4 <- renderUI({
tagList(paper_url)
})
output$app_info_5 <- renderUI({
tagList("- Player Photos Grabbed with ", ballr_url)
})
output$tab_info_1 <- renderText({
"- FG% = % of shots that player X made."
})
output$tab_info_2 <- renderText({
"- FGA = the number of shot attempts per 36 minutes by player X"
})
output$tab_info_3 <- renderText({
"- Rank Correspondence = Rank of FGA - Rank of FG%"
})
output$tab_info_4 <- renderText({
"- LPL is defined as the difference in expected points between the actual distribution of shot attempts from a given lineup and the expected points had those same shots been taken according to the optimal redistribution."
})
output$tab_info_5 <- renderText({
"- PLC is each player's contribution to LPL"
})
output$tab_info_2 <- renderText({
"- FGA = the number of shot attempts per 36 minutes by player X"
})
output$tab_info_2 <- renderText({
"- FGA = the number of shot attempts per 36 minutes by player X"
})
# Rank Plot--------------------------------------------------
output$rank_plot <- renderPlotly({
# Filter out for lineup shot data with Lineup Code
lineup_shot_data <- shot_data_step_2 %>%
filter(lineup_code == my_lineup_code())
player_codes <- c(lineup_shot_data$lineup_player_1[1],
lineup_shot_data$lineup_player_2[1],
lineup_shot_data$lineup_player_3[1],
lineup_shot_data$lineup_player_4[1],
lineup_shot_data$lineup_player_5[1])
player_names <- lineup_shot_data$namePlayer[match(player_codes,
lineup_shot_data$idPlayer)]
lineup_fgp <- matrix(0,10,5)
rownames(lineup_fgp) <- colnames(shot_data_step_2)[19:28]
colnames(lineup_fgp) <- player_codes
for(j in 1:10) {
for(i in 1:length(player_codes)) {
player_region_data <- shot_data_step_2[shot_data_step_2$idPlayer == player_codes[i] &
shot_data_step_2[,18+j] == 1, ]
lineup_fgp[j,i] <- sum(player_region_data$isShotMade)/nrow(player_region_data)
}
}
# Impute a FG% of 0 for regions where no shots were taken:
lineup_fgp[is.na(lineup_fgp)] <- 0
# Output: lineup_fgp
# FGA
lineup_mins <- full_lineups_w_mins$MIN[which(full_lineups_w_mins$lineup_code == my_lineup_code())]
lineup_fga <- matrix(0,10,5)
rownames(lineup_fga) <- colnames(shot_data_step_2)[19:28]
colnames(lineup_fga) <- player_codes
for(j in 1:10) {
for(i in 1:length(player_codes)) {
player_region_lineup_data <- shot_data_step_2[shot_data_step_2$idPlayer == player_codes[i] &
shot_data_step_2[,18+j] == 1 &
shot_data_step_2$lineup_code == my_lineup_code(), ]
lineup_fga[j,i] <- nrow(player_region_lineup_data) / lineup_mins * 36
}
}
# Output: lineup_fga
# Rank fgp and fga of 5 players for each basis
fgp_rank = t(apply(-1 * lineup_fgp, 1, rank, ties.method = "random"))
fga_rank = t(apply(-1 * lineup_fga, 1, rank, ties.method = "random"))
# Plot
rank_colors <- RColorBrewer::brewer.pal(5, "RdYlBu")
gg_court <- ggplot2::fortify(court_regions())
gg_lineup_fgp <- NULL
gg_lineup_fga <- NULL
for(i in 1:dim(fgp_rank)[2]){
gg_player_fgp <- gg_court
gg_player_fgp$rank <- NA
gg_player_fga <- gg_court
gg_player_fga$rank <- NA
for (j in 1:nrow(fgp_rank)){
gg_player_fgp$rank[gg_player_fgp$id == row.names(fgp_rank)[j]] <- fgp_rank[j,i]
gg_player_fga$rank[gg_player_fga$id == row.names(fga_rank)[j]] <- fga_rank[j,i]
}
gg_player_fgp$rank <- factor(gg_player_fgp$rank, levels = 1:5)
gg_player_fgp$player <- player_names[i]
gg_lineup_fgp <- rbind(gg_lineup_fgp, gg_player_fgp)
gg_player_fga$rank <- factor(gg_player_fga$rank, levels = 1:5)
gg_player_fga$player <- player_names[i]
gg_lineup_fga <- rbind(gg_lineup_fga, gg_player_fga)
}
# FGP Plotly Graph-------------------------------
lineup_fgp <- lineup_fgp %>%
as.data.frame()
lineup_fgp_colnames <- lineup_fgp %>%
as.data.frame() %>%
colnames()
# First player name
first_player <- lineup_shot_data %>%
filter(idPlayer == lineup_fgp_colnames[1]) %>%
pull(namePlayer) %>%
unique()
# Second player
second_player <- lineup_shot_data %>%
filter(idPlayer == lineup_fgp_colnames[2]) %>%
pull(namePlayer) %>%
unique()
# Third player
third_player <- lineup_shot_data %>%
filter(idPlayer == lineup_fgp_colnames[3]) %>%
pull(namePlayer) %>%
unique()
# Fourth player
fourth_player <- lineup_shot_data %>%
filter(idPlayer == lineup_fgp_colnames[4]) %>%
pull(namePlayer) %>%
unique()
# Fifth player
fifth_player <- lineup_shot_data %>%
filter(idPlayer == lineup_fgp_colnames[5]) %>%
pull(namePlayer) %>%
unique()
# Change column names to player names
lineup_fgp_processed <- lineup_fgp %>%
rownames_to_column() %>%
rename(basis = 1) %>%
magrittr::set_colnames(c("basis", first_player, second_player, third_player, fourth_player, fifth_player)) %>%
gather(first_player, second_player, third_player, fourth_player, fifth_player, key = "player", value = "fgp") %>%
spread(key = basis, value = fgp)
# First Player FGP Basis 10
first_player_fgp_10 <- lineup_fgp_processed %>%
filter(player == first_player) %>%
pull(basis_10)
# First Player FGP Basis 11
first_player_fgp_11 <- lineup_fgp_processed %>%
filter(player == first_player) %>%
pull(basis_11)
# First Player FGP Basis 20
first_player_fgp_20 <- lineup_fgp_processed %>%
filter(player == first_player) %>%
pull(basis_20)
# First Player FGP Basis 21
first_player_fgp_21 <- lineup_fgp_processed %>%
filter(player == first_player) %>%
pull(basis_21)
# First Player FGP Basis 22
first_player_fgp_22 <- lineup_fgp_processed %>%
filter(player == first_player) %>%
pull(basis_22)
# First Player FGP Basis 30
first_player_fgp_30 <- lineup_fgp_processed %>%
filter(player == first_player) %>%
pull(basis_30)
# First Player FGP Basis 31
first_player_fgp_31 <- lineup_fgp_processed %>%
filter(player == first_player) %>%
pull(basis_31)
# First Player FGP Basis 32
first_player_fgp_32 <- lineup_fgp_processed %>%
filter(player == first_player) %>%
pull(basis_32)
# First Player FGP Basis 33
first_player_fgp_33 <- lineup_fgp_processed %>%
filter(player == first_player) %>%
pull(basis_33)
# First Player FGP Basis 34
first_player_fgp_34 <- lineup_fgp_processed %>%
filter(player == first_player) %>%
pull(basis_34)
# Second Player FGP Basis 10
second_player_fgp_10 <- lineup_fgp_processed %>%
filter(player == second_player) %>%
pull(basis_10)
# Second Player FGP Basis 11
second_player_fgp_11 <- lineup_fgp_processed %>%
filter(player == second_player) %>%
pull(basis_11)
# Second Player FGP Basis 20
second_player_fgp_20 <- lineup_fgp_processed %>%
filter(player == second_player) %>%
pull(basis_20)
# Second Player FGP Basis 21
second_player_fgp_21 <- lineup_fgp_processed %>%
filter(player == second_player) %>%
pull(basis_21)
# Second Player FGP Basis 22
second_player_fgp_22 <- lineup_fgp_processed %>%
filter(player == second_player) %>%
pull(basis_22)
# Second Player FGP Basis 30
second_player_fgp_30 <- lineup_fgp_processed %>%
filter(player == second_player) %>%
pull(basis_30)
# Second Player FGP Basis 31
second_player_fgp_31 <- lineup_fgp_processed %>%
filter(player == second_player) %>%
pull(basis_31)
# Second Player FGP Basis 32
second_player_fgp_32 <- lineup_fgp_processed %>%
filter(player == second_player) %>%
pull(basis_32)
# Second Player FGP Basis 33
second_player_fgp_33 <- lineup_fgp_processed %>%
filter(player == second_player) %>%
pull(basis_33)
# Second Player FGP Basis 34
second_player_fgp_34 <- lineup_fgp_processed %>%
filter(player == second_player) %>%
pull(basis_34)
# Third Player FGP Basis 10
third_player_fgp_10 <- lineup_fgp_processed %>%
filter(player == third_player) %>%
pull(basis_10)
# Third Player FGP Basis 11
third_player_fgp_11 <- lineup_fgp_processed %>%
filter(player == third_player) %>%
pull(basis_11)
# Third Player FGP Basis 20
third_player_fgp_20 <- lineup_fgp_processed %>%
filter(player == third_player) %>%
pull(basis_20)
# Third Player FGP Basis 21
third_player_fgp_21 <- lineup_fgp_processed %>%
filter(player == third_player) %>%
pull(basis_21)
# Third Player FGP Basis 22
third_player_fgp_22 <- lineup_fgp_processed %>%
filter(player == third_player) %>%
pull(basis_22)
# Third Player FGP Basis 30
third_player_fgp_30 <- lineup_fgp_processed %>%
filter(player == third_player) %>%
pull(basis_30)
# Third Player FGP Basis 31
third_player_fgp_31 <- lineup_fgp_processed %>%
filter(player == third_player) %>%
pull(basis_31)
# Third Player FGP Basis 32
third_player_fgp_32 <- lineup_fgp_processed %>%
filter(player == third_player) %>%
pull(basis_32)
# Third Player FGP Basis 33
third_player_fgp_33 <- lineup_fgp_processed %>%
filter(player == third_player) %>%
pull(basis_33)
# Third Player FGP Basis 34
third_player_fgp_34 <- lineup_fgp_processed %>%
filter(player == third_player) %>%
pull(basis_34)
# Fourth Player FGP Basis 10
fourth_player_fgp_10 <- lineup_fgp_processed %>%
filter(player == fourth_player) %>%
pull(basis_10)
# Fourth Player FGP Basis 11
fourth_player_fgp_11 <- lineup_fgp_processed %>%
filter(player == fourth_player) %>%
pull(basis_11)
# Fourth Player FGP Basis 20
fourth_player_fgp_20 <- lineup_fgp_processed %>%
filter(player == fourth_player) %>%
pull(basis_20)
# Fourth Player FGP Basis 21
fourth_player_fgp_21 <- lineup_fgp_processed %>%
filter(player == fourth_player) %>%
pull(basis_21)
# Fourth Player FGP Basis 22
fourth_player_fgp_22 <- lineup_fgp_processed %>%
filter(player == fourth_player) %>%
pull(basis_22)
# Fourth Player FGP Basis 30
fourth_player_fgp_30 <- lineup_fgp_processed %>%
filter(player == fourth_player) %>%
pull(basis_30)
# Fourth Player FGP Basis 31
fourth_player_fgp_31 <- lineup_fgp_processed %>%
filter(player == fourth_player) %>%
pull(basis_31)
# Fourth Player FGP Basis 32
fourth_player_fgp_32 <- lineup_fgp_processed %>%
filter(player == fourth_player) %>%
pull(basis_32)
# Fourth Player FGP Basis 33
fourth_player_fgp_33 <- lineup_fgp_processed %>%
filter(player == fourth_player) %>%
pull(basis_33)
# Fourth Player FGP Basis 34
fourth_player_fgp_34 <- lineup_fgp_processed %>%
filter(player == fourth_player) %>%
pull(basis_34)
# Fifth Player FGP Basis 10
fifth_player_fgp_10 <- lineup_fgp_processed %>%
filter(player == fifth_player) %>%
pull(basis_10)
# Fifth Player FGP Basis 11
fifth_player_fgp_11 <- lineup_fgp_processed %>%
filter(player == fifth_player) %>%
pull(basis_11)
# Fifth Player FGP Basis 20
fifth_player_fgp_20 <- lineup_fgp_processed %>%
filter(player == fifth_player) %>%
pull(basis_20)
# Fifth Player FGP Basis 21
fifth_player_fgp_21 <- lineup_fgp_processed %>%
filter(player == fifth_player) %>%
pull(basis_21)
# Fifth Player FGP Basis 22
fifth_player_fgp_22 <- lineup_fgp_processed %>%
filter(player == fifth_player) %>%
pull(basis_22)
# Fifth Player FGP Basis 30
fifth_player_fgp_30 <- lineup_fgp_processed %>%
filter(player == fifth_player) %>%
pull(basis_30)
# Fifth Player FGP Basis 31
fifth_player_fgp_31 <- lineup_fgp_processed %>%
filter(player == fifth_player) %>%
pull(basis_31)
# Fifth Player FGP Basis 32
fifth_player_fgp_32 <- lineup_fgp_processed %>%
filter(player == fifth_player) %>%
pull(basis_32)
# Fifth Player FGP Basis 33
fifth_player_fgp_33 <- lineup_fgp_processed %>%
filter(player == fifth_player) %>%
pull(basis_33)
# Fifth Player FGP Basis 34
fifth_player_fgp_34 <- lineup_fgp_processed %>%
filter(player == fifth_player) %>%
pull(basis_34)
# Add FGP column----------------------------------
gg_lineup_fgp <- gg_lineup_fgp %>%
mutate(fgp = case_when(
player == first_player & id == "basis_10" ~ first_player_fgp_10,
player == first_player & id == "basis_11" ~ first_player_fgp_11,
player == first_player & id == "basis_20" ~ first_player_fgp_20,
player == first_player & id == "basis_21" ~ first_player_fgp_21,
player == first_player & id == "basis_22" ~ first_player_fgp_22,
player == first_player & id == "basis_30" ~ first_player_fgp_30,
player == first_player & id == "basis_31" ~ first_player_fgp_31,
player == first_player & id == "basis_32" ~ first_player_fgp_32,
player == first_player & id == "basis_33" ~ first_player_fgp_33,
player == first_player & id == "basis_34" ~ first_player_fgp_34,
player == second_player & id == "basis_10" ~second_player_fgp_10,
player == second_player & id == "basis_11" ~second_player_fgp_11,
player == second_player & id == "basis_20" ~second_player_fgp_20,
player == second_player & id == "basis_21" ~second_player_fgp_21,
player == second_player & id == "basis_22" ~second_player_fgp_22,
player == second_player & id == "basis_30" ~second_player_fgp_30,
player == second_player & id == "basis_31" ~second_player_fgp_31,
player == second_player & id == "basis_32" ~second_player_fgp_32,
player == second_player & id == "basis_33" ~second_player_fgp_33,
player == second_player & id == "basis_34" ~second_player_fgp_34,
player == third_player & id == "basis_10" ~ third_player_fgp_10,
player == third_player & id == "basis_11" ~ third_player_fgp_11,
player == third_player & id == "basis_20" ~ third_player_fgp_20,
player == third_player & id == "basis_21" ~ third_player_fgp_21,
player == third_player & id == "basis_22" ~ third_player_fgp_22,
player == third_player & id == "basis_30" ~ third_player_fgp_30,
player == third_player & id == "basis_31" ~ third_player_fgp_31,
player == third_player & id == "basis_32" ~ third_player_fgp_32,
player == third_player & id == "basis_33" ~ third_player_fgp_33,
player == third_player & id == "basis_34" ~ third_player_fgp_34,
player == fourth_player & id == "basis_10" ~fourth_player_fgp_10,
player == fourth_player & id == "basis_11" ~fourth_player_fgp_11,
player == fourth_player & id == "basis_20" ~fourth_player_fgp_20,
player == fourth_player & id == "basis_21" ~fourth_player_fgp_21,
player == fourth_player & id == "basis_22" ~fourth_player_fgp_22,
player == fourth_player & id == "basis_30" ~fourth_player_fgp_30,
player == fourth_player & id == "basis_31" ~fourth_player_fgp_31,
player == fourth_player & id == "basis_32" ~fourth_player_fgp_32,
player == fourth_player & id == "basis_33" ~fourth_player_fgp_33,
player == fourth_player & id == "basis_34" ~fourth_player_fgp_34,
player == fifth_player & id == "basis_10" ~ fifth_player_fgp_10,
player == fifth_player & id == "basis_11" ~ fifth_player_fgp_11,
player == fifth_player & id == "basis_20" ~ fifth_player_fgp_20,
player == fifth_player & id == "basis_21" ~ fifth_player_fgp_21,
player == fifth_player & id == "basis_22" ~ fifth_player_fgp_22,
player == fifth_player & id == "basis_30" ~ fifth_player_fgp_30,
player == fifth_player & id == "basis_31" ~ fifth_player_fgp_31,
player == fifth_player & id == "basis_32" ~ fifth_player_fgp_32,
player == fifth_player & id == "basis_33" ~ fifth_player_fgp_33,
player == fifth_player & id == "basis_34" ~ fifth_player_fgp_34,
TRUE ~ 0
))
gg_lineup_fgp <- gg_lineup_fgp %>%
mutate(player = factor(player, levels=c(player_names[1],
player_names[2],
player_names[3],
player_names[4],
player_names[5]))
)
# FGP Rank Plot---------------------------
fgp_rank_plot <- ggplot2::ggplot(data = gg_lineup_fgp, ggplot2::aes(x=long,
y=lat,
group = group,
fill = rank,
text = paste0("Rank : ", rank,
"<br>FG%: ", round(100 * fgp, 2), "%"))) +
ggplot2::facet_grid(. ~ player) +
ggplot2::geom_polygon(color = "black") +
ggplot2::coord_equal() +
scale_fill_viridis(discrete = TRUE, direction = -1) +
ggplot2::theme(axis.line=ggplot2::element_blank(),
axis.text.x=ggplot2::element_blank(),
axis.text.y=ggplot2::element_blank(),
axis.ticks=ggplot2::element_blank(),
axis.title.x=ggplot2::element_blank(),
axis.title.y=ggplot2::element_blank(),
panel.background=ggplot2::element_blank(),
panel.border=ggplot2::element_blank(),
panel.grid.major=ggplot2::element_blank(),
panel.grid.minor=ggplot2::element_blank(),
strip.text = ggplot2::element_text(size = 16),
strip.background = ggplot2::element_blank(),
legend.position = "none",
# legend.margin = ggplot2::margin(0.1, 0.1, 0.1, 0, "npc"),
plot.margin = ggplot2::unit(c(0, 0, 0, 0), "cm"),
panel.spacing.y = ggplot2::unit(0, "cm"),
text=element_text(size=16, family="Avenir"))
# Plotly Version
p1 <- ggplotly(fgp_rank_plot, tooltip = "text")
# FGA Plotly Graph --------------------------------------
lineup_fga <- lineup_fga %>%
as.data.frame()
lineup_fga_colnames <- lineup_fga %>%
as.data.frame() %>%
colnames()
# Change column names to player names
lineup_fga_processed <- lineup_fga %>%
rownames_to_column() %>%
rename(basis = 1) %>%
magrittr::set_colnames(c("basis", first_player, second_player, third_player, fourth_player, fifth_player)) %>%
gather(first_player, second_player, third_player, fourth_player, fifth_player, key = "player", value = "fga") %>%
spread(key = basis, value = fga)
first_player_fga_10 <- lineup_fga_processed %>%
filter(player == first_player) %>%
pull(basis_10)
first_player_fga_11 <- lineup_fga_processed %>%
filter(player == first_player) %>%
pull(basis_11)
first_player_fga_20 <- lineup_fga_processed %>%
filter(player == first_player) %>%
pull(basis_20)
first_player_fga_21 <- lineup_fga_processed %>%
filter(player == first_player) %>%
pull(basis_21)
first_player_fga_22 <- lineup_fga_processed %>%
filter(player == first_player) %>%
pull(basis_22)
first_player_fga_30 <- lineup_fga_processed %>%
filter(player == first_player) %>%
pull(basis_30)
first_player_fga_31 <- lineup_fga_processed %>%
filter(player == first_player) %>%
pull(basis_31)
first_player_fga_32 <- lineup_fga_processed %>%
filter(player == first_player) %>%
pull(basis_32)
first_player_fga_33 <- lineup_fga_processed %>%
filter(player == first_player) %>%
pull(basis_33)
first_player_fga_34 <- lineup_fga_processed %>%
filter(player == first_player) %>%
pull(basis_34)
second_player_fga_10 <- lineup_fga_processed %>%
filter(player == second_player) %>%
pull(basis_10)
second_player_fga_11 <- lineup_fga_processed %>%
filter(player == second_player) %>%
pull(basis_11)
second_player_fga_20 <- lineup_fga_processed %>%
filter(player == second_player) %>%
pull(basis_20)
second_player_fga_21 <- lineup_fga_processed %>%
filter(player == second_player) %>%
pull(basis_21)
second_player_fga_22 <- lineup_fga_processed %>%
filter(player == second_player) %>%
pull(basis_22)
second_player_fga_30 <- lineup_fga_processed %>%
filter(player == second_player) %>%
pull(basis_30)
second_player_fga_31 <- lineup_fga_processed %>%
filter(player == second_player) %>%
pull(basis_31)
second_player_fga_32 <- lineup_fga_processed %>%
filter(player == second_player) %>%
pull(basis_32)
second_player_fga_33 <- lineup_fga_processed %>%
filter(player == second_player) %>%
pull(basis_33)
second_player_fga_34 <- lineup_fga_processed %>%
filter(player == second_player) %>%
pull(basis_34)
third_player_fga_10 <- lineup_fga_processed %>%
filter(player == third_player) %>%
pull(basis_10)
third_player_fga_11 <- lineup_fga_processed %>%
filter(player == third_player) %>%
pull(basis_11)
third_player_fga_20 <- lineup_fga_processed %>%
filter(player == third_player) %>%
pull(basis_20)
third_player_fga_21 <- lineup_fga_processed %>%
filter(player == third_player) %>%
pull(basis_21)
third_player_fga_22 <- lineup_fga_processed %>%
filter(player == third_player) %>%
pull(basis_22)
third_player_fga_30 <- lineup_fga_processed %>%
filter(player == third_player) %>%
pull(basis_30)
third_player_fga_31 <- lineup_fga_processed %>%
filter(player == third_player) %>%
pull(basis_31)
third_player_fga_32 <- lineup_fga_processed %>%
filter(player == third_player) %>%
pull(basis_32)
third_player_fga_33 <- lineup_fga_processed %>%
filter(player == third_player) %>%
pull(basis_33)
third_player_fga_34 <- lineup_fga_processed %>%
filter(player == third_player) %>%
pull(basis_34)
fourth_player_fga_10 <- lineup_fga_processed %>%
filter(player == fourth_player) %>%
pull(basis_10)
fourth_player_fga_11 <- lineup_fga_processed %>%
filter(player == fourth_player) %>%
pull(basis_11)
fourth_player_fga_20 <- lineup_fga_processed %>%
filter(player == fourth_player) %>%
pull(basis_20)
fourth_player_fga_21 <- lineup_fga_processed %>%
filter(player == fourth_player) %>%
pull(basis_21)
fourth_player_fga_22 <- lineup_fga_processed %>%
filter(player == fourth_player) %>%
pull(basis_22)
fourth_player_fga_30 <- lineup_fga_processed %>%