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
library(plotly)
## Loading required package: ggplot2
##
## 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
library(dslabs)
library(ggplot2)
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.3.3
## Warning: package 'tidyr' was built under R version 4.3.3
## Warning: package 'readr' was built under R version 4.3.3
## Warning: package 'purrr' was built under R version 4.3.3
## Warning: package 'dplyr' was built under R version 4.3.3
## Warning: package 'lubridate' 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.1 ✔ stringr 1.5.1
## ✔ lubridate 1.9.4 ✔ tibble 3.2.1
## ✔ purrr 1.0.4 ✔ tidyr 1.3.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks plotly::filter(), stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dplyr)
data = read.csv("C:/Users/Dominik/Downloads/Team Stats Per Game.csv")
data = data[data$season > 2010,] #trimming dataset down to just the 2011-2026 seasons. This is because I want an easier time reading the hover data for specific points, and also three pointers per game didn't ramp up until the 2010 era of basketball.
data %>% plot_ly(x = ~x3p_per_game, y = ~pts_per_game, color = ~playoffs, type = "scatter", mode = "markers",
hoverinfo = "text",
text = ~paste("Points Per Game: ", pts_per_game, "<br>", "Three Point Made Per Game: ", x3p_per_game, "<br>", "Team: ", team, "<br>", "Season: ", season)) %>%
layout(title = "Points Per Game vs. Three Pointers Made Per Game", xaxis = list(title = "Three Pointers Made Per Game"), yaxis = list(title = "Points Per Game"))
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.
## I have plotted the points per game scored from every team in the 2011-2026 NBA seasons, compared to their three pointers made per game. The graph is organized by color, representing which teams made the playoffs and which teams did not. The hover information that I have included in this graph depicts the dot's corresponding statistics, team name, and what season it came from. I did this so that the reader could see which teams were making more threes, and to see if there was a trend of more points or three pointers made per game over time. A challenge that I faced was the over crowding of the graph, so I decided to shrink down the data set so there was less points (starting at the 2011 season). I also had an issue creating the titles and axis; I ended up using the data camp notes from the subplot section to create the title and axis for the graph above. I tried researching and finding a way to title the legend, but I couldn't find a way.
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.
data = read.csv("C:/Users/Dominik/Downloads/Team Stats Per Game.csv")
data = data[data$season > 2000,] #trimming to seasons after 2000
data %>% plot_ly(x = ~x3pa_per_game, y = ~pts_per_game, color = ~playoffs, frame = ~season, type = "scatter", mode = "markers",
hoverinfo = "text",
text = ~paste("Points Per Game: ", pts_per_game, "<br>", "Three Point Attempts Per Game: ", x3pa_per_game, "<br>", "Team: ", team))%>%
layout(title = "Points Per Game vs. Three Point Attempts Per Game", xaxis = list(title = "Three Point Attempts Per Game"), yaxis = list(title = "Points Per Game")) %>%
animation_slider(currentvalue = list(font = list(color = "black", size = 18)))
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.
##I plotted the same variables as in number one except I did three point ATTEMPTS per game rather than three pointers MADE per game; I did this because I wanted to clearly show how the game evolved over time. I included the seasons from 2001 to 2026. From 2000 until about 2010 the NBA was in what is called the Dead Ball Era. This era of basketball had low scoring games, with offenses that were quite stagnant. We can see through the animation that the games started off very low scoring in 2001, and slowly had higher points per game for each team as we progress further through to 2026. We can see that this is partly due to the amount of three pointers attempted being so low in 2001, with the team taking the highest three point shots being the Boston Celtics at 19.9. However in 2025 we can see that the team with the lowest three point attempts per game is the Denver Nuggets at 32 attempts per game. This clearly shows us that the meta of the NBA changed overtime, allowing for more three pointers attempted per game, which then leads to higher scoring seasons for every team. An issue that I ran into was the positions on the slider label. I made the text big enough to notice but not big enough to interfere with the X axis label.
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).