library(plotly)   # for interactive visuals
## Warning: package 'plotly' was built under R version 4.1.2
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 4.1.2
## 
## 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
library(ggplot2)  # for static visuals
library(tidyr)    # for data tidying
## Warning: package 'tidyr' was built under R version 4.1.2
library(stringr)  # to process character strings
## Warning: package 'stringr' was built under R version 4.1.2
library(forcats)# to process categorical data
## Warning: package 'forcats' was built under R version 4.1.2
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.1.2
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
#library(MASS)    # used for 3D surface plots
#library(splines) # for more complex models
#library(mgcv)    # for more complex models

Question 1

Create an R Markdown document and delete the existing sections/code chunks, then add a section (defined by ## Question 1). In a code chunk within this section, load the AmesHousing data and use plotly to create a violin plot displaying the distribution of sale prices for each different house style in the Ames housing data. (Hint: you should use the reference page to learn how to add a violin plot trace).

AmesHousing <- read.csv("https://raw.githubusercontent.com/ds4stats/r-tutorials/master/data-viz/data/AmesHousing.csv")
plot_ly(data = AmesHousing, type = "violin", x = ~HouseStyle, y = ~SalePrice, color = ~HouseStyle)

Question 2

For Question #2, modify this code to include custom hovertext displaying the house style and that style’s median sale price on separate lines. (Hint: you should use the group_by and summarize functions in the dplyr package to get the information needed for your labels)

prop = table(AmesHousing$HouseStyle)/nrow(AmesHousing)
style = names(prop)


housing_by_style <- group_by(AmesHousing, HouseStyle)
housingsum <- summarize(housing_by_style, 
          median = median(SalePrice, na.rm = TRUE))
housingsum
## # A tibble: 8 x 2
##   HouseStyle median
##   <chr>       <dbl>
## 1 1.5Fin     129675
## 2 1.5Unf     113000
## 3 1Story     155000
## 4 2.5Fin     194000
## 5 2.5Unf     160950
## 6 2Story     189000
## 7 SFoyer     143000
## 8 SLvl       165000
plot_ly(data = AmesHousing) %>%
  add_trace(type = "pie", labels = ~style, values = ~prop, hoverinfo = 'text',
        text = ~paste("House Style:", housingsum$HouseStyle, "<br>", "Median Sale Price:", housingsum$median)
  )

Question 3

model <- lm(SalePrice ~ LotArea + GrLivArea, data = AmesHousing)
xs <- seq(0, 2000000, by = 200)
ys <- seq(0,5000, by = 50)
grid <- expand.grid(xs,ys)
names(grid) <- c("LotArea", "GrLivArea")
z <- predict(model, newdata = grid)
m <- matrix(z, nrow = length(unique(grid$LotArea)), ncol = length(unique(grid$GrLivArea)))

plot_ly() %>% add_surface(x = ~xs, y = ~ys, z = ~m, colors = c("#d1d1d1", "#000000")) %>% 
  add_markers(x = ~AmesHousing$LotArea, y = ~AmesHousing$GrLivArea, z = ~AmesHousing$SalePrice, colors = I("green"))

Question 4

Mshoot <- read.csv("https://raw.githubusercontent.com/skuiper/datascience/master/datasets/MassShootings.csv")
head(Mshoot)
##   X                                       Case           Location Year Type Age
## 1 1     Dayton entertainment district shooting         Dayton, OH 2019 Mass  24
## 2 2              El Paso Walmart mass shooting        El Paso, TX 2019 Mass  21
## 3 3 Virginia Beach municipal building shooting Virginia Beach, VA 2019 Mass  40
## 4 4         Harry Pratt Co. warehouse shooting         Aurora, IL 2019 Mass  45
## 5 5                     SunTrust bank shooting        Sebring, FL 2019 Mass  21
## 6 6           Thousand Oaks nightclub shooting  Thousand Oaks, CA 2018 Mass  28
##            Race Gender Fatalities Injured Victims     Place Legally
## 1         white   male          9      27      36     Other     yes
## 2         white   male         22      26      48 Workplace     yes
## 3         black   male         12       4      16 Workplace     yes
## 4         black   male          5       6      11 Workplace      no
## 5         white   male          5       0       5 Workplace     yes
## 6 other/unknown   male         12      22      34     Other     yes
##                Mental
## 1 unclear/unknown/tbd
## 2 unclear/unknown/tbd
## 3 unclear/unknown/tbd
## 4                 yes
## 5                 yes
## 6                 yes
##                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           Sources
## 1                                                                                                                                                          https://www.nytimes.com/2019/08/04/us/dayton-ohio-shooting.html; https://www.nytimes.com/2019/08/04/us/mass-shootings-dayton-el-paso.html; https://www.washingtonpost.com/nation/2019/08/04/nine-fatally-shot-dayton-including-suspect-day-after-mass-shooting-texas/; https://www.washingtonpost.com/national/police-chief-it-seems-to-defy-believability-that-dayton-shooter-would-kill-his-own-sister/2019/08/05/920a895c-b79e-11e9-b3b4-2bb69e8c4e39_story.html; https://www.cnn.com/2019/08/05/us/connor-betts-dayton-shooting-profile/index.html
## 2                                                                                                                                                                                                                                                                                                                                                                                                                                                                    https://www.washingtonpost.com/nation/2019/08/04/investigators-search-answers-after-gunman-kills-el-paso/; https://www.nytimes.com/2019/08/04/us/mass-shootings-dayton-el-paso.html; https://www.apnews.com/5fb2144947974631912d1efa889f932b
## 3 https://www.washingtonpost.com/local/public-safety/virginia-beach-officials-expect-to-release-additional-details-of-mass-shooting/2019/06/02/a001b856-852f-11e9-a870-b9c411dc4312_story.html; https://pilotonline.com/news/local/virginia-beach-mass-shooting/article_e15ea290-853f-11e9-aa50-93751bba3c3b.html; https://pilotonline.com/news/local/virginia-beach-mass-shooting/article_c8353020-84a1-11e9-aa12-d3bf967368c4.html; https://www.reuters.com/article/us-virginia-shooting-guns/virginia-beach-shooter-bought-guns-he-used-legally-federal-agent-idUSKCN1T23EZ; https://www.usatoday.com/story/news/nation/2019/06/02/virginia-beach-shooter-dewayne-craddock-puzzles-law-enforcement/1319550001/
## 4                                                                                                                                                                                                                                                      https://www.washingtonpost.com/nation/2019/02/16/man-kills-five-warehouse-shooting-spree-shortly-after-being-fired-illinois-police-say/?utm_term=.9863ec4ffc41; https://www.cnn.com/2019/02/17/us/aurora-shooting-termination/index.html; https://www.wsj.com/articles/companies-roll-out-gunshot-detectors-at-the-office-11550575801; https://www.cbsnews.com/news/gary-martin-aurora-illinois-plant-gunman-threatened-violence-if-fired-prosecutors-say/
## 5                                                                                   https://wsbt.com/news/local/classmate-says-florida-bank-shooter-who-allegedly-killed-5-attended-bremen-high-school; https://www.washingtonpost.com/nation/2019/01/23/several-people-shot-after-gunman-barricades-himself-florida-bank-authorities-say/; https://www.wptv.com/news/state/accused-bank-shooter-zephen-xaver-fascinated-with-violence-ex-girlfriend-says; https://www.npr.org/2019/01/24/688183605/man-charged-with-murder-after-allegedly-killing-5-women-at-suntrust-bank-in-flor; https://www.wesh.com/article/sebring-bank-shooting-latest/26012533; https://www.apnews.com/c3523d282b2942739240731daaf797c2
## 6                                                                                                                                                                                                                                        http://www.latimes.com/local/lanow/la-me-ln-thousand-oaks-20181107-story.html; https://www.washingtonpost.com/nation/2018/11/08/multiple-injuries-reported-bar-shooting-thousand-oaks-calif/; https://www.nytimes.com/2018/11/08/us/shooting-california-thousand-oaks.html; https://www.nytimes.com/2018/11/08/us/ian-david-long-california-shooter.html; https://www.wsj.com/articles/multiple-injuries-reported-in-bar-shooting-in-thousand-oaks-california-1541666907
#Make Graph of Data
plot_ly(data = Mshoot, type = "scatter", mode = "markers", 
        x = ~Age, y = ~Fatalities, frame = ~round(Year),
        showlegend = FALSE) %>%
   animation_opts(frame = 1500, easing = "elastic", redraw = FALSE)