Loading Required Libraries

library(plotly)
## Warning: package 'plotly' was built under R version 4.3.3
## 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

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

Plotting mpg (continuous), hp (continuous), and # of cylinders

# making the plotly graph
# using plotly for this one to practice making graphs without ggplot
plot_ly(data = mtcars, x = ~hp, y = ~mpg, color = ~as.factor(cyl), colors = c("#4793AF","#FFC470","#DD5746"), type = "scatter", mode = "markers") %>% 
  
  # adding titles and axis labels
  layout(title = "Car MPG Based on Horse Power and Number of Cylinders", xaxis = list(title = "Horse Power (HP)"), yaxis = list(title = "Miles Per Gallon (MPG)"), legend = list(title = list(text = "# Cylinders"))) %>% 
  
  # creating custom hover elements
  add_trace(type = "scatter", mode = "markers", showlegend = FALSE, x = mtcars$hp, marker = list(size = 10), y = mtcars$mpg, text = rownames(mtcars), hovertemplate = paste(
      "<b>%{text}</b><br>",
      "MPG: %{y:,.0f}<br>",
      "HP: %{x:,}",
      "<extra></extra>"
      ))

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.

This graph shows the relationship between a car’s horse power and number of cylinders and its corresponding MPG. Based on the graph, you can see that as the horsepower of a car increases, its corresponding mpg decreases. We can also see that as the number of cylinders increases, so does the horse power. However the mpg decreases when the number of cylinders increases. When viewers hover over the points on the graph, they are able to see the exact horse power and mpg values as well as the type of car that corresponds to the point. This helps to give a more personal meaning to each point on the graph, making it easier to interpret. For example, not many people can picture a car with mpg = 30 and hp = 52, but many people know the Honda Civic and can visualize what a car with those features looks like.

My biggest challenge came from my limited experience with plotly. To help with this, I looked back to the notes, the data camp course, and some helpful code on the internet. For example, I had a clear picture in my head regarding my custom hover elements and when researching how to do it, I found a code template that helped me to layout my hover text in the way I wanted. I used part of that code and made it my own to fit my specific data set and the needs of my graph.

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.

# sorting the data frame so it graphs nice
mtcars$ind <- c(1:nrow(mtcars))
mtsort <- mtcars[order(mtcars$hp, decreasing = TRUE),]

# making the plotly graph
# using plotly for this one to practice making graphs without ggplot
plot_ly(data = mtsort, x = ~hp, y = ~mpg, color = ~as.factor(cyl), 
        colors = c("#4793AF","#FFC470","#DD5746")) %>% 
  
  # Adding the scatter points to the graph
  add_markers(frame = ~as.factor(cyl), showlegend = FALSE, size=2,) %>% 
  
  # slowing the time between frames
  animation_opts(frame = 1500, transition = 1000) %>% 

  # adding titles and axis labels
  layout(title = "Car MPG Based on Horse Power and Number of Cylinders", 
         xaxis = list(title = "Horse Power (HP)"), 
         yaxis = list(title = "Miles Per Gallon (MPG)")) %>% 
  
  # Adding the label on th graph to make it stand out more
  add_text(x = 75, y = 15, text = ~as.factor(cyl), 
           frame = ~as.factor(cyl), textfont = list(size=40, color = toRGB("grey40"))) %>%
  
  # changing the wording on the current label
  animation_slider(currentvalue = list(prefix = "Cylinders: "))
## Warning in p$x$data[firstFrame] <- p$x$frames[[1]]$data: number of items to
## replace is not a multiple of replacement length

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.

Once again I decided to use the mtcars data set and plot the horse power of a car and the number of cylinders to determine a car’s mpg. From the last graph I made, we know that as the horsepower and the number of cylinders increases, the mpg of a car decreases. This animation specifically shows how the number of cylinders impacts the mpg and also the relationship between the number of cylinders and horsepower.

One challenge I had was finding the data. Many times, you have the data and need to select a graph that works the best, but the assignment required me to find data to create the picture. In an attempt to save time and avoid heavy data cleaning that might be useless, I used the mtcars data set. Another challenge that I had was adding text to my plot. I wanted to change the text to correspond to the number of cylinders, but the “8” cylinders notation is animated as well and moves. I do not know why this happens and after doing research, it appears to have a long and complicated solution. I was not able to solve this problem, but hopefully I will be able to someday.

What to turn in: