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.


Data


The data used in the analysis in this task is the New York crimes data containing information on the crimes committed in the state of New York from the year 1990 to the year 2018. The observations in this data were by the county and police agency to which the crime was reported. The data was sourced from https://www.kaggle.com/datasets/new-york-state/new-york-state-index-crimes. The motor vehicle theft variable is a variable with information on the number of vehicle theft crimes that have been reported to the various agencies in the various counties in the state of New York. The murder variable provides information on the number of murder crimes reported in the various agencies in the various counties in the state of New York.


# Data and Data Preparation
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
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
NY_Crimes <- read_csv("index-crimes-by-county-and-agency-beginning-1990.csv")
## Rows: 19956 Columns: 15
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (3): County, Agency, Region
## dbl (12): Year, Months Reported, Index Total, Violent Total, Murder, Rape, R...
## 
## ℹ 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.
NY_Crimes <- na.omit(NY_Crimes)


Plotly Scatterplot


The plotting in this task was developed for the scatterplot of the motor vehicle theft and the murder incidents in the state of New York. The investigation of the association between the two variables will provide information on whether the motor vehicles crimes are related to murder crimes in the state. The scatterplot also provides information on the region allowing for each of the two regions to be evaluated to determine the association between the two variables for the respective region.


plot_ly(data = NY_Crimes, x = ~`Motor Vehicle Theft`, y = ~Murder, color = ~Region, hoverinfo = "text",
        hovertext = paste("Region :", NY_Crimes$Region,
                          "<br> Crime Index :", NY_Crimes$`Index Total`)) %>%
  layout(
    xaxis = list(title = 'Motor Vehicle Theft'),
    yaxis = list(title = 'Murder'),
    title = 'Motor Vehicle Theft against Murder Scatterplot'
  )
## 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


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.


An animated version of the scatterplot above is presented below with the animation component being the counties in the state of New York. Therefore, the association between the two variables (the motor vehicle theft and murder) for the respective region is now observable for each county in the state of New York.


plot_ly(data = NY_Crimes, x = ~`Motor Vehicle Theft`, y = ~Murder, color = ~Region, hoverinfo = "text",
        frame = ~County,
        hovertext = paste("Region :", NY_Crimes$Region,
                          "<br> Crime Index :", NY_Crimes$`Index Total`)) %>%
  layout(
    xaxis = list(title = 'Motor Vehicle Theft'),
    yaxis = list(title = 'Murder'),
    title = 'Motor Vehicle Theft against Murder Scatterplot'
  )%>%
  animation_button(
    x = 1, xanchor = "right", y = 0, yanchor = "bottom"
  )
## 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


RPubs Link: https://rpubs.com/Ghana___/1173517


What to turn in: