Loading relevant libraries
library(rvest)
library(ggplot2)
library(tidyr)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
Fetching player stats from Basketball Reference
url <- 'https://www.basketball-reference.com/leagues/NBA_2025_per_game.html'
webpage <- read_html(url)
tables <- html_table(webpage, fill = TRUE)
player_stats <- tables[[1]]
Cleaning the data
player_stats <- player_stats %>%
filter(!is.na(Player)) %>%
mutate_at(vars(STL, BLK, `DRB`), as.numeric)
Filtering relevant stats for Pacers and the selected player (Cam
Johnson example)
pacers_stats <- player_stats %>% filter(Team == 'IND')
cam_johnson_stats <- player_stats %>% filter(Player == 'Cameron Johnson')
Combining Pacers stats with the selected player.
combined_stats <- bind_rows(pacers_stats, cam_johnson_stats)
Selecting relevant columns for visualization
selected_stats <- combined_stats %>% select(Player, STL, BLK, `DRB`)
Melting the data for ggplot using tidyr
melted_stats <- selected_stats %>%
pivot_longer(cols = -Player, names_to = "Statistic", values_to = "Value")
Plotting the graph
ggplot(melted_stats, aes(x = Player, y = Value, fill = Player)) +
geom_bar(stat = 'identity', position = 'dodge') +
facet_wrap(~Statistic, scales = 'free') +
theme_minimal() +
labs(title = 'Cam Johnson vs. Indiana Pacers: Defensive Statistics Comparison',
x = 'Player',
y = 'Value',
fill = 'Player')
