#install.packages("plotly")
library(plotly)
library(dplyr)
library(dslabs)
library(ggplot2)


Note: on data sets

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).


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.

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(openxlsx)
scores <- read.xlsx("Exam_Scores.xlsx", sheet = 1)
high <- scores[scores$Parental_Involvement == "High",]
med <- scores[scores$Parental_Involvement == "Medium",]
low <- scores[scores$Parental_Involvement == "Low",]

# Compute density curves
d_high <- density(high$Exam_Score, na.rm = TRUE)
d_med <- density(med$Exam_Score, na.rm = TRUE)
d_low <- density(low$Exam_Score, na.rm = TRUE)

# Overlay density plots
plot_ly() %>%
  add_lines(x = ~d_high$x, y = ~d_high$y, 
            name = "High", fill = 'tozeroy',
            hoverinfo = "text",
            text = ~paste("Parental Involvement: High", "<br>",
                          "Exam Score: ", round(d_high$x, 2), "%<br>",
                          "Density: ", round(d_high$y, 3))) %>%
  add_lines(x = ~d_med$x, y = ~d_med$y, 
            name = "Medium", fill = 'tozeroy',
            hoverinfo = "text",
            text = ~paste("Parental Involvement: Medium", "<br>",
                          "Exam Score: ", round(d_high$x, 2), "%<br>",
                          "Density: ", round(d_high$y, 3))) %>%
  add_lines(x = ~d_low$x, y = ~d_low$y, 
            name = "Low", fill = 'tozeroy',
            hoverinfo = "text",
            text = ~paste("Parental Involvement: Low", "<br>",
                          "Exam Score: ", round(d_high$x, 2), "%<br>",
                          "Density: ", round(d_high$y, 5))) %>%
  layout(title = list(text = "Exam Scores by Parental Involvement Levels"),
         xaxis = list(title = 'Exam Score'),
         yaxis = list(title = 'Density'),
         legend = list(title = list(text = "Parental Involvement Levels")))

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 plotly graph above is an overlayed density plot displaying the distributions of exam scores based on parental involvement levels. It splits the parental involvement levels into three categories: low, medium, and high. It is interesting to see that medium involvement actually has a greater mean score than high involvement. However, high levels have more density in the better exam scores from 80 to 100%. The reader can gain valuable information by hovering over the graph by being able to see the rounded exam score percentage along with its corresponding density. Both of these are labeled, allowing the reader to hover over any part of the graph and see the resulting data. An issue that I had while making this plot was being able to get the legend title to appear. I had initially left out the second "list" function in the statement, leaving me with no title. After figuring out that I needed a "nested" list function for both title and text, I was able to successfully finish the graph with all necessary titles and labels.


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 or trend.

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

data <- read.xlsx("Apple_Tesla_Stock.xlsx", sheet = 1)

data %>%
  plot_ly(x = ~High, y = ~Company, color = ~Company, frame = ~Day,
          type = 'bar', showlegend = FALSE, hoverinfo = "text",
          text = paste("Company: ", data$Company, "<br>",
                       "High Price: $", data$High)) %>%
  layout(title = list(text = "Daily High Stock Prices (10/26)"),
      xaxis = list(title = 'High Stock Price ($)'),
      yaxis = list(title = 'Companies')) %>%
  add_text(x = 405, y = "Apple", text = ~Day, frame = ~Day,
           textfont = list(size = 60, color = toRGB("grey"))) %>%
  animation_slider(hide = TRUE)

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.

# This animated bar plot (using plotly) displays the high stock prices for Apple and Tesla over the month of October 2026. While the two companies are somewhat close in average stock value (mostly within $50), it can be seen that Tesla's high values have much greater variability than Apple's. Tesla appeared to have a strong close to October, as its prices soared up to over $400. The main issue that I needed to overcome when making this plot was figuring out where the frame label should be placed in order to make it visible while being out of the way of the bars "moving". At first, the label was appearing next to both Apple and Tesla's bars, but I had to change the y-axis to "Apple" to get the label where it would best fit.

What to turn in: