Problem 1 [20 points]

Create a plotly graph of your choosing that represents at least two variables, one of which must be a categorical variable.

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

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.

You can use any data of your choosing, just reference this.

The graph must include:

  1. customized hover text that is informative to the graphing elements in the plot

  2. separate color to represent groups

  3. labeled axes and appropriate title

Read in the data

nba_scoring <- read.csv("NBA_scoring.csv")

# Factor variable
nba_scoring$POS <- factor(nba_scoring$POS, levels = c("G", "F", "C"), 
                            labels = c("Guard", "Forward", "Center"))

Graph

nba_scoring %>% 
  # plot_ly
  plot_ly(x = ~FGM_PCT_AST, y = ~PCT_PTS_3PT, color = ~POS,
          
          # color for each group
          colors = c("#007A33", "#ffe200", "#e50000"),
          
          # hover over points for text
          hoverinfo = "text",
          
          # what text says (name, team, and % of points via free throws)
          text = ~paste0("Name: ", PLAYER,
                        "<br>Team: ", TEAM,
                        "<br>% of PTS FTs: ", PCT_PTS_FT, "%")) %>% 
  
  # scatterplot
  add_markers(marker = list(sizeref = 2, sizemode = "diameter")) %>% 
  
  # labels
  layout(xaxis = list(title = "% of Assisted FGM"),
         yaxis = list(title = "% of PTS 3PTs"),
         title = "% of PTS 3PTs by % of Assisted FGM")

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/challenges you had (if any) while making the plot, and you you dealt with or overcame them.

The plot above shows the percentage of points that are three-pointers by the percentage of assisted field goals made grouped by position (guard, forward, and center). Field goals made are any shot that goes in except for free throws. The graph shows that guards generally have a higher percentage of points that are three-pointers than forwards are centers. Centers have the lowest percentage of points that are threes among the positions. There is a general trend that as percentage of assisted field goals made increases, percentage of points being three-pointers increase. Most of the players who are assisted the most are post players (power forwards or centers) or 3-point specialists (typically shooting guards or small forwards) because shots in the paint and 3-point shots are hard to generate from one player alone. They are typically the result of a pass, which becomes an assist when the bucket goes in. For example, points in the paint can come from “pick-and-rolls,” which is when a post player screens for a guard and the defenders are forced to make a decision: switch and risk the mismatch or attempt to stay on your man. The post players on offense generally benefit by getting an open look and receiving a pass from a guard, which results in an assisted field goal. The same thing can be said for 3-point specialists. One play that teams use to get three-point specialists open is called a “floppy,” where everyone but the ball handler and three-point shooter set a screen to get the three-point shooter open behind the three-point line, which results in an assisted field goal made. For example, Trevor Ariza (small forward) and Eric Gordon (shooting guard) played in a heavy three-point offense, which resulted in them having a high percentage of points being three-pointers. Rudy Gobert is a center that is known to be an interior presence; therefore, he is not taking any threes.

The next thing to look at in the plot is hovering over a point. Each point shows you the player’s name, team, and their percentage of points that are free throws. Typically, post players and slashing guards (guards that will take shots in the paint) have the highest percentage of points that are free throws. This is meant to give the reader an idea about a player’s offensive profile and how often they draw fouls/contact and what their shot selection looks like.

I faced two challenges while creating this plot. The first one was about having a slider or not. I thought it would be interesting to show the distribution of points grouped by position; however, I realized the colors show the trends well enough. The second issue I had was about the paste function. I included the percent symbol after percentage of points that are free throws, but the paste function automatically adds a space between each object in the function. I discovered the paste0 function, which does not add a space between each object. This will be helpful for other projects where getting rid of a space is needed. I can use paste0 instead of gsub().

Problem 2 [20 points]

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.

This graph should include:

  1. Aside from the graphing variable, a separate categorical variable. For example, in our animated scatter plot we color grouped the points by continent.

  2. Appropriate axis labels and a title

  3. 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 till clearly visible and does not obstruct the view of your plot.

Graph

nba_scoring %>% 
  # plot_ly
  plot_ly(x = ~FGM_PCT_UAST, y = ~PCT_PTS_2PT, color = ~POS,
          
          # colors for each group
          colors = c("#00538C", "blueviolet", "#e50000"),
          
          # hover over points for text
          hoverinfo = "text",
          
          # what text says (name, team, and % of points via free throws)
          text = ~paste0("Name: ", PLAYER,
                        "<br>Team: ", TEAM,
                        "<br>% of PTS FTs: ", PCT_PTS_FT, "%")) %>% 
  
  # scatterplot
  add_markers(frame = ~POS,
    marker = list(sizeref = 2, sizemode = "diameter")) %>% 
  
  # labels
  layout(xaxis = list(title = "% of Unassisted FGM"),
         yaxis = list(title = "% of PTS 2PTs"),
         title = "% of PTS 2PTs by % of Unassisted FGM") %>%
  
  # adjust frame time to 6 seconds and transition time to 3 seconds with elastic easing
  animation_opts(frame = 6000, transition = 3000,
                 easing = "elastic")%>% 
  
  # change frame to position and adjust color and size
  animation_slider(currentvalue = list(prefix = "Position: ", 
                                font = list(color = "dodgerblue", size = 20)))

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.

The plot above shows the percentage of points that are two-pointers by the percentage of unassisted field goals made grouped by position (guard, forward, and center). Field goals made are any shot that goes in except for free throws. The graph shows that each position has a different distribution between the two statistics. There is a relatively constant mean and variance in percentage of points that are two-pointers for guards. Guards have a large range for percentage of unassisted field goals made. Some guards can create shots for themselves and other guards need teammates to create space for them, which are typically great three-point shooters such as Klay Thompson and JJ Reddick. There is a positive linear relationship between percentage of points that are two-pointers and percentage of unassisted field goals made for forwards. As percentage of unassisted field goals increase so does percentage of points that are two-pointers. Power forwards that are ball-handlers are mismatches for other power forwards or smaller forwards/guards. For example, LeBron James, DeMar DeRozan, and Giannis Antetokounmpo are primary ball-handlers for their team, but they can create space in the midrange or the paint, which results in a higher percentage of unassisted twos. Centers are primarily in the top left quadrant of the graph. They are mostly tall guys who score off passes from “pick-and-rolls” or post-moves. The majority of their shots are from close range because of their height advantage. The centers that have higher unassisted field goals made are typically “interior forces” that dominate their opponent with strength and finesse. They can slowly back down opponents and use tougher shots such as fadeaways or hooks to score points. Backing down opponents is time-consuming; therefore, there are often no assists are such plays. Additionally, they have the height advantage and ability to box out defenders to grab offensive rebounds and put them back up for a bucket.

The only issue I ran into was with the color of the currentvalue in animation_slider. Some R colors do not work in that function and will not show up. I am not sure why, but I discovered this after trying multiple colors that caused the frame to not change colors. One of the colors I tried was “goldenrod.” Finally, I tried “blue,” which changed the frame to “blue.” This issue is something I will explore more to understand why it happened.

What to turn in: