This document contains the code used to run a pie chart based on data from basketball-reference to outline a specific player’s performance throughout the entirety of the 2022-23 season. The player documented is Matthew Cleveland who played at Florida State during this time.
url = "https://www.sports-reference.com/cbb/players/matthew-cleveland-1.html"
page = read_html(url)
index <- 1:12
stat <- c("True Shot %", "Effective FG %", "Off. Rating", "Three Pt. %", "Ast. Total", "T/O Total", "Def. RPG.", "Steal Total", "Block Total", "Def. Rating", "Usage %", "Efficiency Rtg.")
values <- c(51, 48, 98.9, 35, 53, 66, 6.6, 25, 24, 100, 24, 16)
data <- as.data.frame(cbind(index, stat, values))
data <- data %>%
mutate(type = case_when(
index %in% 1:6 ~ "Offense",
index %in% 7:10 ~ "Defense",
index %in% 11:12 ~ "Overall"
))
data$index <- as.numeric(data$index)
data$values <- as.numeric(data$values)
data$type <- factor(data$type, levels = c("Offense", "Defense", "Overall"))
## The index is
# axis angle
temp <- (360/(length(data$index))/2)
# find difference in angle between labels & divide by two
myAng <- seq(-temp, -360+temp, length.out = length(data$index))
# get angle for each label
ang <- ifelse(myAng < -90, myAng+180, myAng)
# rotate label 180 degrees in some instances
ang <- ifelse(ang < -90, ang+180, ang)
color1 <- "#782F40"
color2 <- "#CEB888"
color3 <- "#2C2A29"
ang <- 45
ggplot(data = data, aes(x = reorder(stat, index), y = values, label = values, fill = type)) +
# adding colors to each piece
geom_bar(data = data, width = 1,
color = "grey",
stat = "identity") +
# wrap bar chart for pie chart
coord_polar(clip = "off") + # adjust clip argument to "off" to avoid clipping
# add background behind each bar ( alpha at .5 for a bit of transparency to emphasize them)
geom_bar(aes(y=100, fill=type), stat="identity", width=1, alpha=.5) +
# add $ to customize line to border whole pie
geom_hline(yintercept = seq(2, 100, by = 100),
color = "grey",
linewidth = 1) +
# add & customize lines between each slice
geom_vline(xintercept = seq(.5, 12, by =1),
color = "grey",
linewidth = 1) +
# add percentile labels (labels are choice of fill & color) - option 2
geom_label(color = "gray10", fill = "grey", size = 2, fontface = "bold", family = "sans", show.legend = FALSE,
position = position_stack(vjust = .95),hjust = .5,label.padding = unit(.25, "lines")) +
# manually set the colors of bars (3 here for each group of stats (scoring, possession, defending))
scale_fill_manual(values = c(color1, color2, color3)) +
# theme manipulation to customize plot (play around with this)
theme(legend.position = "top",
legend.direction = "horizontal",
legend.background = element_rect(fill = "grey", color="black"),
legend.title = element_blank(),
legend.text = element_text(color = "black", family = "sans", face = "bold"),
legend.key.size = unit(.4, "cm"),
legend.box.spacing = unit(0, "mm"),
plot.title = element_text(hjust = 0.5, color = "gray20", face = "bold", size = 20, family = "sans"),
plot.subtitle = element_text(hjust = 0.5, color = "gray33", size = 12, family = "sans"),
plot.background = element_rect(fill = "transparent", color = "transparent"),
panel.background = element_rect(fill = "transparent", color = "transparent"),
panel.grid = element_blank(),
axis.text = element_text(face = "bold", size = 1, color = "gray20", angle = 90),
axis.title = element_blank(),
axis.line = element_blank(),
axis.ticks.y = element_blank(),
axis.text.y = element_blank(),
axis.text.x = element_text(face = "bold", size = 6, family = "sans", angle = ang)) +
# add title & subtitle
# add title & subtitle
labs(title = "Matthew Cleveland 2022-23 Season Recap",
subtitle = "@davidk_digital (via: basketball-reference)", x = NULL, y = NULL)
## - Usage - An estimate of the percentage of team plays used by a player while they are on the floor
## - Efficiency Rating - Expresses a player's productivity per minute during active play-time (the average efficiency rating in college basketball is 15)
## - True Shot % - Factors in a player's shooting percentage adjusted for two-point field goals, three-point field goals, and free throws
## - Effective FG % - Adjusts for the fact that a three-point shot is worth more than a three-point shot
## - Offensive Rating - An estimate of points scored (for team) or points produced (for player) per 100 possessions
## - Three Point % - Three-point field goal percentage
## - Assist Total - Total assists accumulated in a given season
## - T/O Total - Total turnovers accumulated in a given season
## - Def. RPG - Defensive rebounds per game
## - Steal Total - Total steals accumulated in a given season
## - Block Total - Total blocks accumulated in a given season
## - Def. Rating - Defensive rating gives an estimate of points allowed per 100 possessions (In this case the exact figure is 110.4)