What the plot is about

With the help of Chat GBT I made a plot that maps the journey of players, represented by points, showing how their performance (points per 100 possessions) progresses with their experience (years in the league). There’s a red dashed line, representing the average performance across all players. Each point reveals the player’s name, position, age, team, and games played when hovered over. Players are also color-coded by their positions. The information and dataset contains statistics about 812 player-team during the 2021-2022 NBA regular season. The dataset itself was from Score Sport Data Repository.

library(tidyverse)
## Warning: package 'ggplot2' was built under R version 4.3.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.0     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(plotly)
## 
## Attaching package: 'plotly'
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following object is masked from 'package:graphics':
## 
##     layout
data <- read_csv("nba stats.csv")
## Rows: 812 Columns: 30
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (3): player, pos, tm
## dbl (27): age, g, gs, mp, fg, fga, fgpercent, x3p, x3pa, x3ppercent, x2p, x2...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# Create scatter plot with an average line
p <- ggplot(data, aes(x = age, y = pts)) +
  geom_point() +
  geom_line(aes(y = mean(pts)), color = "blue") +
  geom_hline(yintercept = mean(data$pts), linetype = "dashed", color = "red") +  # Add average line
  labs(x = "Years in the League", y = "Points per 100 Possessions", title = "Points vs. Years in the League")

# Making the first plot better
p <- ggplot(data, aes(x = age, y = pts,text = paste("Name:", player, "<br>Position:", pos, "<br>Age:", age, "<br>Team:", tm, "<br>Games:", g, "<br>Points per 100 Possessions:", pts))) +
  geom_point() +
  geom_line(aes(y = mean(pts)), color = "blue") +
  geom_hline(yintercept = mean(data$pts), linetype = "dashed", color = "red") +  # Add average line
  labs(x = "Years in the League", y = "Points per 100 Possessions", title = "Points vs. Years in the League")


p <- ggplot(data, aes(x = age, y = pts, text = paste("Name:", player, "<br>Position:", pos, "<br>Age:", age, "<br>Team:", tm, "<br>Games:", g, "<br>Points per 100 Possessions:", pts))) +
  geom_point(aes(color = factor(pos)), size = 3, alpha = 0.7) +
  geom_smooth(aes(y = pts), method = "lm", color = "darkblue", se = FALSE) +
  geom_hline(yintercept = mean(data$pts), linetype = "dashed", color = "red") +  # Add average line
  scale_color_viridis_d() +
  labs(x = "Years in the League", y = "Points per 100 Possessions", title = "Points vs. Years in the League", color = "Position") +
  theme_minimal() +
  theme(plot.title = element_text(hjust = 0.5))

# Convert ggplot2 object to a plotly object
p_plotly <- ggplotly(p, tooltip = "text")
## `geom_smooth()` using formula = 'y ~ x'
# Print 
p_plotly

Convert ggplot2 object to a plotly object

p_plotly <- ggplotly(d, tooltip = “text”)

Print

p_plotly