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.

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

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.

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(ggplot2)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ lubridate 1.9.2     ✔ tibble    3.2.1
## ✔ purrr     1.0.2     ✔ tidyr     1.3.0
## ── 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)
library(nflfastR)

pbp_2023 <- load_pbp(2023)

Rush_2023 <- pbp_2023 |> 
  filter(play_type == "run") |> 
  filter(week != 19) |> 
  filter(week != 20) |> 
  filter(week != 21) |> 
  filter(week != 22) |> 
  group_by(week, posteam) |> 
  summarise(epa = sum(epa),
            rushes = sum(play_type == "run"))
## `summarise()` has grouped output by 'week'. You can override using the
## `.groups` argument.
Rush_2023 <- Rush_2023 |> 
  mutate(epa_rush = epa/rushes)

Colors <- teams_colors_logos |> 
  filter(team_abbr != "LAR") |>
  filter(team_abbr != "OAK") |>
  filter(team_abbr != "SD") |>
  filter(team_abbr != "STL")

Rushing_2023 <- left_join(Rush_2023, Colors, by=c("posteam" = "team_abbr"))

DET_2023 <- Rushing_2023 |> 
  filter(posteam == "DET")

GB_2023 <- Rushing_2023 |> 
  filter(posteam == "GB")

CHI_2023 <- Rushing_2023 |> 
  filter(posteam == "CHI")

MIN_2023 <- Rushing_2023 |> 
  filter(posteam == "MIN")

DET <- density(DET_2023$epa_rush, na.rm = TRUE)
GB <- density(GB_2023$epa_rush, na.rm = TRUE)
CHI <- density(CHI_2023$epa_rush, na.rm = TRUE)
MIN <- density(MIN_2023$epa_rush, na.rm = TRUE)

plot_ly() |> 
  add_lines(x = ~DET$x, y = ~DET$y, name = "DET", fill = "tozeroy") |> 
  add_lines(x = ~GB$x, y = ~GB$y, name = "GB", fill = "tozeroy") |> 
  add_lines(x = ~CHI$x, y = ~CHI$y, name = "CHI", fill = "tozeroy") |> 
  add_lines(x = ~MIN$x, y = ~MIN$y, name = "MIN", fill = "tozeroy") |> 
  layout(xaxis = list(title = "EPA per Rush"),
         yaxis = list(title = "Density"),
         title = "2023 NFC North EPA/Rush Densities")

#In this plot, I took the nflfastR library, and created a density plot of the EPAs per rush of each of the four NFC North teams over the course of the 2023 NFL Regular Season. I did this because I wanted to see the consistencies of each team’s rushing abilities over the course of the season. From this, it is apparent that Minnesota had a wildly inconsistent season rushing the ball (showing more bad than good), Chicago was consistently solid running the ball, Green Bay was consistently average running the ball, and Detroit was boom/bust at running the ball, but tended to have the best performances of the year amongst any of the teams in the division. One issue I ran into with this graph were that I could not fill the plots with the team’s colors in plotly. As an alternative, I used the fill = “tozeroy”, which just had a random color filled in (but the viewer can still hover over the plot and tell what team they are analyzing as long as they follow the line). The other issue I came across was that the density plot came up in whole numbers and not a decimal. This I assume was because the larger numbers represented the number of games instead of the percentage of the season.

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.

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.

library(plotly)
library(ggplot2)
library(tidyverse)
library(dplyr)
library(nflfastR)

pbp_2023 <- load_pbp(2023)

Rush_2023 <- pbp_2023 |> 
  filter(play_type == "run") |> 
  filter(week != 19) |> 
  filter(week != 20) |> 
  filter(week != 21) |> 
  filter(week != 22) |> 
  group_by(week, posteam) |> 
  summarise(epa = sum(epa),
            rushes = sum(play_type == "run"))
## `summarise()` has grouped output by 'week'. You can override using the
## `.groups` argument.
Rush_2023 <- Rush_2023 |> 
  mutate(epa_rush = epa/rushes)

Colors <- teams_colors_logos |> 
  filter(team_abbr != "LAR") |>
  filter(team_abbr != "OAK") |>
  filter(team_abbr != "SD") |>
  filter(team_abbr != "STL")

Rushing_2023 <- left_join(Rush_2023, Colors, by=c("posteam" = "team_abbr"))

Rushing_2023 |> 
  plot_ly(x = ~rushes, y = ~epa_rush,
          hoverinfo = "text",
          text = ~paste("Team:", posteam,
                        "<br> EPA/Rush:", epa_rush,
                        "<br> Rushes:", rushes)) |> 
  add_markers(frame = ~ week,
              ids = ~posteam,
              size = ~rushes, color = ~team_division,
              marker = list(sizemode = "diameter",
                            sizeref = 2)) |> 
  layout(xaxis = list(title = "Rushes"),
         yaxis = list(title = "EPA/Rush"),
         title = "2023 EPA/Rush by Week") |> 
  animation_slider(currentvalue = list(font = list(color="white")))
## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

#In this animation plot with plotly, I plotted the EPA/Rush over the course of the 2023 NFL Regular Season from the nflfastR library. Within the plot, you can see EPA/Rush by team progress week by week. This shows a lot of variability, as many teams did not consistently put up a good EPA/Rush on a week-to-week basis. When making this plot, my biggest issue was with grouping the teams by color. In the nflfastR library, there is a table that you can join in called “teams_logos_colors” that has each team’s HEX color. When I joined this table in with my data, and tried to group by team color, it came up with an error saying the palette I asked for had too many colors (since there were 32 teams). I combatted this by grouping by conference, which ended up being too few groups to group by (2), so then I grouped it by division, which was perfect since the maximum to group by was 8 values, and there are 8 divisions in the NFL. So while this is not colored the way I wanted, it still gets the point across, as when you hover over each team, you see the team’s abbreviation, their EPA/rush in a given week, and their rushes in a given week.

What to turn in: