How to Analyze NBA Player Stats in R Using hoopR and ggplot2

[This article was first published on Blog - R Programming Books, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Ever wondered how to pull and visualize NBA player stats using R?
With just a few lines of code and the {hoopR} package, you can load real NBA data directly from ESPN and create professional-looking visualizations in minutes.

Here’s how to build a quick chart of the Top 10 NBA Scorers (2023–24) using tidyverse and hoopR.

#Load libraries

library(tidyverse)
library(hoopR)

#Load NBA player box scores

nba_data <- hoopR::load_nba_player_box(seasons = 2024)

#Calculate games played and average points

top_scorers <- nba_data %>%
filter(is.na(did_not_play) | did_not_play == FALSE | did_not_play == “FALSE”) %>%
group_by(athlete_display_name, team_abbreviation) %>%
summarise(
games_played = n(),
avg_points = mean(points, na.rm = TRUE),
.groups = “drop”
) %>%
filter(games_played >= 30) %>%
arrange(desc(avg_points)) %>%
slice_head(n = 10)

#Visualization

ggplot(top_scorers, aes(
x = reorder(athlete_display_name, avg_points),
y = avg_points,
fill = team_abbreviation
)) +
geom_col(fill = “steelblue”) +
geom_text(aes(label = round(avg_points, 1)),
hjust = -0.2, size = 4, color = “black”) +
coord_flip() +
scale_y_continuous(expand = expansion(mult = c(0, 0.15))) +
labs(
title = “Top 10 NBA Scorers (2023–24)”,
x = “Player”,
y = “Avg Points per Game”
) +
theme_minimal(base_size = 14) +
theme(
plot.title = element_text(face = “bold”, hjust = 0.5, size = 16),
axis.title = element_text(face = “bold”),
axis.text = element_text(size = 12),
axis.text.x = element_text(size = 10),
legend.position = “none”
)

📊 Output Example

The resulting chart displays the top 10 NBA players by average points per game, calculated directly from ESPN’s box scores for the 2023–24 season.

top nba scorers r programming

🧠 How It Works

  • hoopR::load_nba_player_box() loads real NBA game-level statistics.
  • dplyr (from the tidyverse) groups and summarizes the averages for each player.
  • ggplot2 turns those summaries into a polished, professional visualization.

You can easily modify:

  • The minimum number of gamesfilter(games_played >= 30)
  • The season yearseasons = 2024
  • The statistic → Replace points with assists, rebounds, or any other metric.

🔗 More Resources

If you’re into sports analytics with R, check out

Mastering NBA Analytics with R – Data Science for Basketball Performance and Strategy

$7.99

Analyze NBA data like a pro. This ebook teaches R programming for player stats, performance analysis, and basketball strategy.

The post How to Analyze NBA Player Stats in R Using hoopR and ggplot2 appeared first on R Programming Books.

To leave a comment for the author, please follow the link and comment on their blog: Blog - R Programming Books.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)