#install.packages("dslabs")
#install.packages("dplyr")
#install.packages("ggbeeswarm")
#install.packages("gapminder")
#install.packages("trelliscopejs")
#install.packages("tidyverse")
#install.packages("plotly")
library(dslabs)
library(ggplot2)
library(dplyr)
library(purrr)
library(trelliscopejs)
library(tidyverse)
library(plotly)
You may use any data of your choosing in the following problems, but I would suggest you choose a data set you find interesting or would give an interesting graph (so, don’t use something like the old iris data set). You will get more out of the project in the end, and it will look better to those in the future you are showing it to. If the data set comes from an R package then reference this. If the data set is from elsewhere, then upload a copy to blackboard (.csv format).
Create a plotly graph of your choosing that represents at least two variables, one of which must be a categorical variable.
This plot can be a scatter plot, overlayed density plots (graphing variable is continuous, separate densities grouped by categorical variable), etc. choropleth maps could also be on the list…you have to admit they look kinda cool.
The graph must include:
customized hover text that is informative to the graphing elements in the plot
separate color to represent groups
labeled axes and appropriate title
Include at least a 1-paragraph discussion about the graph. Discuss what is being plotted and what information is being displayed in the graph. Discuss any information that the reader may gain from hovering the cursor over graphing elements. Discuss any issues/chalenges you had (if any) while making the plot, and you you dealt with or overcame them.
#setwd and load dataset
setwd("~/RStudioFiles")
nbaplayerstats = read.csv('nbaplayerstats2026.csv', header = TRUE, sep=",")
#create new df
#mutate to add a column for Points/Game called PPG
#filter for players who played more than 5 games & group by team
nbaplayerstats %>%
filter(GP>=5) %>%
mutate(PPG=PTS/GP,
eFG=(FGM+0.5*FG3M)/FGA) %>%
group_by(TEAM) %>%
plot_ly(x = ~eFG, y = ~PPG,
color=~Position,
frame=~TEAM,
hoverinfo = "text",
text = ~paste0("<b>",PLAYER,"<br>",Position)
) %>%
add_markers() %>%
#add titles
layout(
title= "NBA 2026 Offensive Visualization",
xaxis= list(title="Effective Field Goal %"),
yaxis=list(title="Avg Points/Game")
)
The dataset that I chose to graph is 2025-2026 NBA player statistics. The data is mostly made up of summary statistics for individual players (ie.total field goal attempts, total turnovers). The categorical variables in the dataset are Team and Position.
I created 2 additional variables. The first is avg Points per game (PPG) and the second is Effective Field Goal Percent (eFG). These are both offensive related statistics and I thought it would be interesting broken out in frames for each team. I chose to add a hover label text of the players and their positions. Someone who is using this graph would want to know the name of offensively effective players that could appear to be outliers from the rest of their team. One of the challenges I had was feeling like I had offensive statistics that were representative of the players overall offensive ability, this is why I created the new variable eFG%.
Create an animated plotly graph with a data set of your choosing. This can be, but does not have to be a scatter plot. Also, the animation does not have to take place over time. As mentioned in the notes, the frame can be set to a categorical variable. However, the categories the frames cycle through should be organized (if needs be) such that the progression through them shows some pattern or trend.
This graph should include:
Aside from the graphing variable, a separate categorical variable. For example, in our animated scatter plot we color grouped the points by continent.
Appropriate axis labels and a title
Augment the frame label to make it more visible. This can include changing the font size and color to make it stand out more, and/or moving the frame label to a new location in the plotting region. Note, if you do this, make sure it is still clearly visible and does not obstruct the view of your plot.
Include at least a 1-paragraph discussion about the plot. Discuss what you are plotting and what trends can be seen throughout the animation. Discuss any issues, if any, you ran into in making the plot and how you overcame them.
nbaplayerstats %>%
filter(GP>=5) %>%
mutate(DRBPG=DREB/GP) %>%
group_by(TEAM) %>%
plot_ly(x = ~DRBPG, y = ~STL_TOV,
color=~TEAM,
frame=~Position,
hoverinfo = "text",
text = ~paste0("<b>",PLAYER,"<br>",TEAM)
) %>%
add_markers() %>%
#add titles
layout(
title= "NBA 2026 Defensive Visualization",
xaxis= list(title="Defensive Rebounds Per Game"),
yaxis=list(title="Steal to Turnover Ratio")
)%>%
animation_slider(
currentvalue=list(
visible=TRUE,
prefix="Position:",
font=list(size=10,color="darkblue"),
xanchor="left"
)
)
## Warning in RColorBrewer::brewer.pal(max(N, 3L), "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors
## Warning in RColorBrewer::brewer.pal(max(N, 3L), "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors
I used the same NBA player data set from problem 1. However, this time I chose to focus on defensive statistics and comparison within each position rather than for each team. I chose to do the frame by position because each position has different offensive/defensive functions within a team, so it felt like a more even comparison to group by position
the “defensive star outliers” that I noticed for each position is Jalen Johnson from ATL for small forwards, Jason Tatum for power forwards, and Luka Doncic/Josh Giddey for point guards. I have heard of several of these players being known for being good “well rounded” all-star players and their defensive statistics show that they are more effective on defense.
What to turn in:
knit your final assignment to an html document and publish it to an RPubs page.
submit (1) the rmd file and (2) the link to this page in Blackboard (this can be in a word document or some other form to submit the link).