The lbj data set contains information about the 1703 games Lebron James has played in the NBA through the 2023/2024 season, including regular season and playoff games.
While there are 29 columns in the data set, we’ll be primarily interested in only a few of them:
age: His age at the time of the game
team: The team James played for (abbreviated)
opp: The abbreviation of the opponent played against
game_score: And advanced metric used to measure how well a player has played
result: If the team James played on won or lost
season_game: The number of the game (out of 82) in the regular season
Create a histogram of James’ game_score . The hex codes of the colors used are #860038 and #041E42. See Brightspace for how the plot should appear
ggplot(
data = lbj,
mapping = aes(
x = game_score
)
) +
geom_histogram(
bins = 20,
color = "#041E42",
fill = "#860038"
) +
theme_bw() +
scale_y_continuous(
expand = c(0, 0, 0.05, 0)
) +
scale_x_continuous(
breaks = 0:5*10
) +
labs(
x = "Lebron James' Game Score"
)
Create a set of 3 separate density plots for game_score for each team James played for. Add the code at the bottom of the code chunk to have the area and lines of the density plots match the teams’ colors. Make sure the graphs appear as they do in Brightspace.
ggplot(
data = lbj,
mapping = aes(
x = game_score
)
) +
geom_density(
mapping = aes(
color = team,
fill = team
),
show.legend = F,
linewidth = 1
) +
facet_wrap(
facets = vars(team),
ncol = 1
) +
theme_bw() +
scale_y_continuous(
expand = c(0, 0, 0.05, 0)
) +
scale_color_manual(
values = c(
"Cleveland Cavaliers" = "#041E42",
"Los Angeles Lakers" = "#FDB927",
"Miami Heat" = "#98002E"
)
) +
scale_fill_manual(
values = c(
"Cleveland Cavaliers" = "#860038",
"Los Angeles Lakers" = "#552583",
"Miami Heat" = "#F9A01B"
)
) +
labs(
x = "Game Score"
)
Does there appear to be a difference in James’ performance for at least one team he played on?
**Redo the graph from 1b by graphing the counts on the y-axis instead
of the density by using y = after_stat(count)
ggplot(
data = lbj,
mapping = aes(
x = game_score
)
) +
geom_density(
mapping = aes(
color = team,
fill = team,
y = after_stat(count)
),
show.legend = F,
linewidth = 1
) +
facet_wrap(
facets = vars(team),
ncol = 1
) +
theme_bw() +
scale_color_manual(
values = c(
"Cleveland Cavaliers" = "#041E42",
"Los Angeles Lakers" = "#FDB927",
"Miami Heat" = "#98002E"
)
) +
scale_fill_manual(
values = c(
"Cleveland Cavaliers" = "#860038",
"Los Angeles Lakers" = "#552583",
"Miami Heat" = "#F9A01B"
)
) +
labs(
x = "Game Score"
)
What is the difference between the graphs in 1b vs 1c of displaying the density vs the counts?
Create a bar chart of the teams Lebron has played against
using the data set created at the top of the code chunk. Make sure the
graph matches what is in Brightspace. The colors used are the hex codes
for the NBA logo (you can find the hex codes using Google). Add the
theme()
to your graph to center the title and rotate the
x-axis labels
lbj_2a <-
lbj |>
# Removing the teams that no longer exist
filter(
opp != "NOK"
)
ggplot(
data = lbj_2a,
mapping = aes(
x = opp
)
) +
geom_bar(
#fill = c("#1D428A", "#C8102E", "#C8102E", "#C8102E", "#C8102E")
color = "#1D428A",
fill = "#C8102E"
) +
scale_y_continuous(
expand = c(0, 0, 0.05, 0)
) +
labs(
x = NULL,
y = "Number of Games Played",
title = "Number of Games Lebron James has played against each NBA Team"
) +
theme_classic() +
theme(
plot.title = element_text(hjust = 0.5),
axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)
)
Which team has Lebron played against the most?
Which team has Lebron played against the least?
Create a side-by-side bar chart that displays the game result for each game type. Have the color of the wins bar use the NBA blue and the loss color be NBA red (same colors as 2a). The graph should appear as it does on Brightspace.
ggplot(
data = lbj,
mapping = aes(
x = game_type,
fill = result
)
) +
geom_bar(
position = "dodge2"
) +
scale_y_continuous(
expand = c(0, 0, 0.05, 0)
) +
scale_fill_manual(
values = c("#C8102E", "#1D428A")
) +
labs(
x = "Game Type",
fill = NULL
) +
theme_bw() +
theme(
plot.title = element_text(hjust = 0.5),
legend.position = c(0.9, 0.8)
)
Which game type is Lebron more likely to lose than win?
Create a graph like the graph in 2b, but have the game type on the y-axis and the conditional proportions on the x-axis. See the results in Brightspace
ggplot(
data = lbj,
mapping = aes(
y = game_type,
fill = result
)
) +
geom_bar(
position = "fill"
) +
labs(
x = NULL,
y = "Game Type",
title = "Lebron James' win percentage by game series",
fill = NULL
) +
scale_x_continuous(
expand = c(0, 0),
labels = scales::label_percent()
) +
scale_fill_manual(
values = c("#C8102E", "#1D428A")
) +
theme_bw() +
theme(
plot.title = element_text(hjust = 0.5),
legend.position = "bottom"
)
Create a line graph using the lbj_sum created in the code chunk below of James’ game score by game in season season_game. It should match what is in Brightspace.
ggplot(
data = lbj_sum,
mapping = aes(
x = season_game,
y = game_score,
color = team
)
) +
geom_line() +
labs(x = "Game in Season",
y = "Lebron's Average Game Score",
color = "Team") +
theme_bw() +
theme(
legend.position = c(0.2, 0.15)
) +
scale_color_manual(
values = c(
"Cleveland Cavaliers" = "#041E42",
"Los Angeles Lakers" = "#FDB927",
"Miami Heat" = "#98002E"
)
)