NBAloveR
I'm really excited to introduce my first R package called "NBAloveR". My brand new R package "NBAloveR" is an R package that helps users analyze online basketball data easily. Currently the package only focused on deliverying users easy and simple accesses to online basketball data scraped from Basketball Reference.
For more information, visit my GitHub repo: NBAlover. Please let me know your opinions. Your feedbacks are always welcome.
Thanks
This post covers how to install and use "NBAloveR" packages and shows some examples using the package.
Installation
This report focuses on intdroducing you to NBAloveR package, showing usage exapmles.
devtools::install_github("koki25ando/NBAloveR") library(NBAloveR)
getPlayers(): Function for getting all NBA players' information: name, heigh/weight, college, birth year, birth place
library(tidyverse) players <- getPlayers() players %>% head()
Player height weight collage born birth_city birth_state 1 Curly Armstrong 180 77 Indiana University 1918 2 Cliff Barker 188 83 University of Kentucky 1921 Yorktown Indiana 3 Leo Barnhorst 193 86 University of Notre Dame 1924 4 Ed Bartels 196 88 North Carolina State University 1925 5 Ralph Beard 178 79 University of Kentucky 1927 Hardinsburg Kentucky 6 Gene Berce 180 79 Marquette University 1926
library(plotly) plot_ly( data = players, x = ~height, y = ~ weight, hoverinfo = 'text', text = paste0( "Name: ", players$Player, "<br>Weight: ", players$weight, "<br>Height: ", players$height ) ) %>% layout( title = "Weight vs Height per each NBA player" )
Explore by yourself:
RPubs - Weight vs Height per each NBA player
getFranchise(): Function for getting team list
franchise <- getFranchise() franchise %>% head()
Franchise Lg From To Yrs G W L W/L% Plyfs Div Conf Champ 1 Atlanta Hawks NBA 1950 2019 70 5470 2717 2753 0.497 46 11 0 1 2 Boston Celtics NBA/BAA 1947 2019 73 5642 3329 2313 0.590 55 31 9 17 3 Brooklyn Nets NBA/ABA 1968 2019 52 4140 1782 2358 0.430 26 5 2 2 4 Charlotte Hornets NBA 1989 2019 29 2248 988 1260 0.440 10 0 0 0 5 Chicago Bulls NBA 1967 2019 53 4215 2183 2032 0.518 35 9 6 6 6 Cleveland Cavaliers NBA 1971 2019 49 3888 1829 2059 0.470 22 7 5 1
franchise %>% ggplot(aes(x = Champ, y = reorder(Franchise, Champ))) + geom_point(colour = "red") + geom_segment(aes(yend=reorder(Franchise, Champ)), xend = 0, colour="red", alpha = .5) + labs(x = "Number of Champions", y = "", title = "Numbber of Champions per NBA Team")
getStats(): Function for getting stats data for each player divided by season
stats <- getStats() stats %>% head()
# A tibble: 6 x 34 # Groups: Player, Year [4] Year Player Pos Age Tm G MP PER FTr FG FGA `FG%` `3P` `3PA` `3P%` `2P` `2PA` `2P%` FT FTA `FT%` ORB DRB TRB AST STL BLK TOV PF <int> <chr> <chr> <int> <chr> <int> <int> <dbl> <dbl> <int> <int> <dbl> <int> <int> <dbl> <int> <int> <dbl> <int> <int> <dbl> <int> <int> <int> <int> <int> <int> <int> <int> 1 1950 Curly … G-F 31 FTW 63 NA NA 0.467 144 516 0.279 NA NA NA 144 516 0.279 170 241 0.705 NA NA NA 176 NA NA NA 217 2 1950 Cliff … SG 29 INO 49 NA NA 0.387 102 274 0.372 NA NA NA 102 274 0.372 75 106 0.708 NA NA NA 109 NA NA NA 99 3 1950 Leo Ba… SF 25 CHS 67 NA NA 0.259 174 499 0.349 NA NA NA 174 499 0.349 90 129 0.698 NA NA NA 140 NA NA NA 192 4 1950 Ed Bar… F 24 TOT 15 NA NA 0.395 22 86 0.256 NA NA NA 22 86 0.256 19 34 0.559 NA NA NA 20 NA NA NA 29 5 1950 Ed Bar… F 24 DNN 13 NA NA 0.378 21 82 0.256 NA NA NA 21 82 0.256 17 31 0.548 NA NA NA 20 NA NA NA 27 6 1950 Ed Bar… F 24 NYK 2 NA NA 0.750 1 4 0.250 NA NA NA 1 4 0.250 2 3 0.667 NA NA NA 0 NA NA NA 2 # ... with 5 more variables: PTS <int>, PPG <dbl>, RPG <dbl>, APG <dbl>, SPG <dbl>
stats2017 <- stats %>% select(Player, PPG:APG) %>% filter(Year == 2017) %>% na.omit() plot_ly(data = stats2017, x = ~PPG, y = ~RPG, z = ~APG, hoverinfo = "text", text = paste0( "Player: ", stats2017$Player, "<br>PPG: ", stats2017$PPG, "<br>RPG: ", stats2017$RPG, "<br>APG: ", stats2017$APG ), marker = list(size = 5) ) %>% layout( title = "2017 NBA Players' Stats: PPG vs RPG vs APG", scene = list( xaxis = list(title = "Points per Game"), yaxis = list(title = "Rebounds per Game"), zaxis = list(title = "Assists per Game") ) )
Explore by yourself:
RPubs - 2017 NBA Players' Stats: PPG vs RPG vs APG
getStatsSummary(): Function for getting given player's career summary stats
Kobe <- getStatsSummary("Kobe Bryant") Kobe
# A tibble: 1 x 7 # Groups: Player [1] Player Career Pos PPG RPG APG SPG <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> 1 Kobe Bryant 1997-2016 SG 25.0 5.24 4.68 1.44
getTeamSalary(): Function that allows you to get salary data of each team
WarriorsSalary <- getTeamSalary("gsw") WarriorsSalary %>% head()
Player Age Salary1819 2019-20 2020-21 2021-22 2022-23 2023-24 Signed Using Guaranteed 1 Stephen Curry 30 37457154 $40,231,758 $43,006,362 $45,780,966 Bird Rights $166,476,240 2 Kevin Durant 29 30000000 $31,500,000 $30,000,000 3 Klay Thompson 28 18988725 1st Round Pick $18,988,725 4 Draymond Green 28 17469565 $18,539,130 Bird Rights $36,008,695 5 Andre Iguodala 34 16000000 $17,185,185 Bird Rights $33,185,185 6 Shaun Livingston 32 8307692 $7,692,308 Bird Rights $10,307,692
WarriorsSalary$"2018-19" <- WarriorsSalary$"2018-19" %>% str_remove("\\$") %>% str_remove_all("\\,") WarriorsSalary$"2018-19" <- as.numeric(WarriorsSalary$"2018-19") names(WarriorsSalary)[3] <- "Salary1819" WarriorsSalary <- WarriorsSalary %>% filter(Player != "Team Totals") plot_ly(data = WarriorsSalary, labels = ~Player, values = ~Salary1819, type = "pie", hoverinfo = "text", text = paste0( "Player: ", WarriorsSalary$Player, "<br>Salary: $", format(round(WarriorsSalary$Salary1819), big.mark = ",") ) ) %>% layout( title = "Salary of Golden State Warriors' Players (2018-19 Season)" )
Explore by yourself:
RPubs - Salary of Golden State Warriors' Players (2018-19 Season)
getTeamHistory(): Function that allows you to get team history data
ClevelandHistory <- getTeamHistory("cle") ClevelandHistory %>% head()
Season Lg Team W L W/L% Finish SRS Pace Rel Pace ORtg Rel ORtg DRtg Rel DRtg Playoffs Coaches Top WS 1 2018-19 NBA Cleveland Cavaliers NA NA NA NA NA NA NA NA NA NA NA T. Lue (0-0) 2 2017-18 NBA Cleveland Cavaliers 50 32 0.610 1 0.59 98.0 0.7 112.9 4.3 111.9 3.3 Lost Finals T. Lue (50-32) L. James (14.0) 3 2016-17 NBA Cleveland Cavaliers 51 31 0.622 1 2.87 96.2 -0.2 113.6 4.8 110.3 1.5 Lost Finals T. Lue (51-31) L. James (12.9) 4 2015-16 NBA Cleveland Cavaliers 57 25 0.695 1 5.45 93.3 -2.5 110.9 4.5 104.5 -1.9 Won Finals D. Blatt (30-11), T. Lue (27-14) L. James (13.6) 5 2014-15 NBA Cleveland Cavaliers 53 29 0.646 1 4.08 92.3 -1.6 111.1 5.5 106.3 0.7 Lost Finals D. Blatt (53-29) K. Irving (10.4) 6 2013-14 NBA Cleveland Cavaliers 33 49 0.402 3 -3.86 93.1 -0.8 104.2 -2.5 107.7 1.0 M. Brown (33-49) K. Irving (6.7)
ClevelandHistory$Team <- ClevelandHistory$Team %>% str_remove("\\*") ClevelandHistory %>% ggplot(aes(Season, W, group = Team)) + geom_point() + geom_line() + theme(axis.text.x=element_text(angle=40)) + labs(y = "Number of Wins", title = "Winning History of Cleveland Cavaliers") + annotate("rect", xmin = "2003-04", xmax = "2009-10", ymin = 0, ymax = 85, fill = "red", alpha = .3) + annotate("rect", xmin = "2014-15", xmax = "2017-18", ymin = 0, ymax = 85, fill = "red", alpha = .3) + annotate("text", label = "LeBron's Era", x = "2011-12", y = 85, colour = "red")
Conclusion
In this report, I have shown a few exapmle using NBAloveR package. But remember this package was designed to help you analyze easily basketball data.
Without further ado, use this package and please let me know your opinions. Your feedbacks are always welcome! Thanks,