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

library(readxl)
library(ggplot2)
library(tidyverse)
library(plotly)

#Read in data
nhl_data <- read_excel("C:/Users/dalla/OneDrive/Desktop/Classes/Data Visualization/nhl 500 data.xlsx")

#Regular Plot
nhl <- nhl_data %>%
  ggplot(aes(x = PTS, y = S, color = Pos, text = Player)) + 
  geom_point() +
  ggtitle("NHL Points vs. Shots on Goal by Position (2022-23)") + 
  xlab("Points") + 
  ylab("Shots on Goal")

#Interactive Plot
ggplotly(nhl)

This graph was created to show the relationship between an NHL players points and their shots on goal throughout a season. The data points are categorized by the position of the player. As you can see from the graph there seems to be a linear relationship between the amount of shots on goal and the number of points that a player gets throughout a season. Hovering over each data point you can see the exact number of points, shots on goal, position, and the players name. Normally there are only 5 different skater positions in hockey, but the “w” position is listed on there as well. This position indicated that the player plays both right and left wing throughout the season. If you double click on the positions in the legend you can easily see the correlations on the graph sorted by position.

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.

library(readxl)
library(ggplot2)
library(tidyverse)
library(plotly)

#Read in Data
country <- read_excel("C:/Users/dalla/OneDrive/Desktop/Classes/Data Visualization/countries2.xlsx")

#Round TL to two decimal places
country$`Telephone lines` <- round(country$`Telephone lines`, 2)

#Animated Graph
country %>%
  plot_ly(x = ~Population, y = ~`Telephone lines`, 
          color = ~Continent, hoverinfo = "text", 
          text = ~paste("Country:",`Country Name`,
                        "<br> Pop:", Population,
                        "<br> Tele Lines:", `Telephone lines`)) %>%
  add_text(x = 300000000, y = 60, text = ~Year, frame = ~Year, 
           textfont = list(size = 70, color = toRGB("grey90")),
           showlegend = FALSE) %>%
  add_markers(frame = ~Year,
              ids = ~`Country Name`,
              marker = list(size = 15),
              showlegend = TRUE)%>% 
  layout(title = "Telephone Lines vs. Population (1990-2016)",
         xaxis = list(type = 'log', title = 'Population'),
         yaxis = list(title = 'Telephone Lines per 100 people')) %>%
  animation_slider(currentvalue = list(font = list(color="white")))


This chart shows how telephone lines per 100 people and population size relate in 16 major countries from 1990 to 2016. It’s animated to display changes over time and colors each country based on its continent. From 1990 to 2009, telephone lines generally increased. There was a big spike in 2009, but after that most of the countries seemed to steadily decline in the number of telephone lines per 100 people that they had. The population for each country also steadily rose for the most part throughout the span of 1990-2016. I encountered a problem where the animated year label was hiding some data points. This happened because of how Plotly adds elements to the chart. By changing the order of commands, I fixed it. Also, the year label was too big and in the middle of the graph, which was distracting. To fix this, I made it smaller and moved it to the top right corner to avoid covering the data.

What to turn in: