#INTERACTIVE MAPS
#setup
library(leaflet)    # The map-making package
library(geojsonio)  # A package for geographic and spatial data, requires the latest version of dplyr
## Registered S3 method overwritten by 'geojsonsf':
##   method        from   
##   print.geojson geojson
## 
## Attaching package: 'geojsonio'
## The following object is masked from 'package:base':
## 
##     pretty
library(dplyr)      # Used for data manipulation and merging
## 
## 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(htmltools)  # Used for constructing map labels using HTML

#getting the spatial polygons
shapeurl = "https://raw.githubusercontent.com/PublicaMundi/MappingAPI/master/data/geojson/us-states.json"
usstates <- geojson_read(shapeurl, what = "sp")

#creating the color palette based on murder rate
pal <- colorBin("viridis", domain = USArrests$Murder)

#getting the state names from the rows of USArrests to use as Popups
USArrests$State = row.names(USArrests)
myPopups <- paste("<strong>", USArrests$State, "</strong>", "<br/>", 
                   "Urban Population:", USArrests$UrbanPop)

#coloring city markers
us_cities$col <- ifelse(us_cities$capital == 2, "red", "yellow")

Map1 <- leaflet(usstates) %>% addTiles() %>% addPolygons(
  fillColor = pal(USArrests$Murder),
  weight = 2,
  opacity = 1,
  color = "black",
  fillOpacity = 0.7,
  highlight = highlightOptions(weight = 3,
                   color = "white",
                   fillOpacity = 0.7,
                   bringToFront = FALSE),
  popup = myPopups) %>%
  addCircleMarkers(lng = us_cities$long, lat = us_cities$lat, 
                   label = ~paste(us_cities$name, us_cities$pop),
                   #label = ~paste(us_cities$pop),
                   color = us_cities$col,
                   fillOpacity = 0.25,
                   radius = 2,
                   stroke = FALSE) %>%
addLegend(pal = pal, values = USArrests$Murder,
            title = "Murder Rate", position = "bottomleft")

Map1
#PLOTLY

#setup
library(plotly)   # for interactive visuals
## 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
library(ggplot2)  # for static visuals
library(tidyr)    # for data tidying
library(stringr)  # to process character strings
library(forcats)  # to process categorical data

#data
Mshoot <- read.csv("https://raw.githubusercontent.com/skuiper/datascience/master/datasets/MassShootings.csv")

plot_ly(data = Mshoot, type = "scatter",  mode = "markers", 
        x= ~Age, y= ~Fatalities, frame= ~Year, showlegend= F,
       hoverinfo = 'text',
        text = ~paste0("Age: ", Age, "<br>", "Race: ", Race, "<br>", "Fatalities: ", Fatalities, "<br>", "Injured: ", Injured))%>%
   animation_opts(frame = 1000, easing = "elastic", redraw = FALSE)