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


The Aviation data is a dataset that provides information on the various aviation accidents across the world in terms of fatality. The data was sourced from https://www.kaggle.com/datasets/khsamaha/aviation-accident-database-synopses?rvi=1 and provides information from the US transport accidents database with specific focus on aviation accidents world. The data for this case centered around the aviation accidents that have taken place in the various states in the United States and the territories under the United States. Two variables are plotted against each other in a scatterplot; these variables are the total uninjured and the total fatal injuries. The former relates to the total number of people that survived an aviation accident without any injuries while the latter relates to the total number of fatalities that resulted from the aviation accident. The plotly scatterplots allows for the observation of the relationship between the survival (total uninjured) and fatality (total fatal injuries) for each of the two investigation types.


# Data
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.0     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
AviationData <- read_csv("AviationData.csv")
## Rows: 88889 Columns: 31
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (25): Event.Id, Investigation.Type, Accident.Number, Location, Country,...
## dbl   (5): Number.of.Engines, Total.Fatal.Injuries, Total.Serious.Injuries, ...
## date  (1): Event.Date
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# Filtering for data for the United States
AviationData <- AviationData %>% filter(Country == "United States")

#Getting State Initials
library(stringr)
AviationData$State1 <- str_split_fixed(AviationData$Location, ", ", 2)[,2]
AviationData$State2 <- str_split_fixed(AviationData$State1, ", ", 2)[,2]
AviationData$State <- ifelse(AviationData$State2 == "", AviationData$State1, AviationData$State2)

#Variable selection 
AviationData <- AviationData %>% select(State, Investigation.Type, Total.Uninjured, Total.Fatal.Injuries)

#Removing Missing Entries
AviationData <- na.omit(AviationData)
AviationData <- AviationData %>% filter(State != "")


library(plotly)
## 
## 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
plot_ly(data = AviationData, 
        x = ~Total.Uninjured, y = ~Total.Fatal.Injuries, 
        color = ~Investigation.Type, 
        hoverinfo = "text",
        hovertext = paste("State:", AviationData$State,
                          "<br> Total.Uninjured:", AviationData$Total.Uninjured,
                          "Total.Fatal.Injuries:",AviationData$Total.Fatal.Injuries)) %>%
  layout(
    title = 'Survival and Fatality Relationship (Aviation Accidents)'
  )
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels

## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels


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.


The animated scatterplot similarly shows the relationship between the survival (total uninjured) and fatality (total fatal injuries) for each of the two investigation types. Additionally, the animation is based on the states across the US with the relationship for each of the investigation types displayed for each of the states in the US. A challenge experienced with the plotting relates to adjusting elements of the slider where a variety of functions are available for each of the elements instead of a single function with parameters for each of the elements.


plot_ly(data = AviationData, 
        x = ~Total.Uninjured, y = ~Total.Fatal.Injuries, 
        color = ~Investigation.Type, 
        frame = ~State,
        hoverinfo = "text",
        hovertext = paste("State:", AviationData$State,
                          "<br> Total.Uninjured:", AviationData$Total.Uninjured,
                          "Total.Fatal.Injuries:",AviationData$Total.Fatal.Injuries)) %>%
  layout(
    title = 'Survival and Fatality Relationship (Aviation Accidents)'
  ) %>% 
  animation_slider(
    currentvalue = list(font = list(color="blue"))
  )
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels

## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels


What to turn in:

R-Pubs Link: https://rpubs.com/Kruthik_/1173508