Setup
library(leaflet)
library(geojsonio)
## 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)
##
## 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)
library(readr)
us_states <- read_csv("https://raw.githubusercontent.com/PublicaMundi/MappingAPI/master/data/geojson/us-states.json")
## Warning: One or more parsing issues, see `problems()` for details
## Rows: 52 Columns: 2
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): {"type":"FeatureCollection", features:[
##
## ℹ 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.
require(datasets)
Interactive Map
shapeurl <- "https://raw.githubusercontent.com/PublicaMundi/MappingAPI/master/data/geojson/us-states.json"
UnitedStates <- geojson_read(shapeurl, what = "sp")
data("USArrests")
data("us_cities")
USArrests$State = row.names(USArrests)
pal <- colorBin("viridis", domain = USArrests$Murder)
myLabels <- paste("<strong>", USArrests$State, "</strong>", "<br/>",
"Urban Population:", USArrests$UrbanPop)
myPopups <- paste( USArrests$State)
us_cities$col <- ifelse(us_cities$capital > 0, "red", "yellow")
Map1 <- leaflet(UnitedStates) %>%
addTiles() %>%
addPolygons(
fillColor = pal(USArrests$Murder),
color = "black",
fillOpacity = 0.8,
highlight = highlightOptions(
color = "white",
fillOpacity = 0.8
),
label = lapply(myLabels, HTML),
popup = myPopups) %>%
addLegend(pal = pal, values = USArrests$Murder,
title = "Murder Rate", position = "bottomleft") %>%
addCircleMarkers(data = us_cities,
lng = ~long, lat = ~lat,
label = ~paste(us_cities$name, "City"),
color = ~col,
radius = 2,
fillOpacity = 0.25,
stroke = FALSE,
)
Map1
Setup
library(plotly)
## 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)
library(tidyr)
library(stringr)
library(forcats)
Mshoot <- read.csv("https://raw.githubusercontent.com/skuiper/datascience/master/datasets/MassShootings.csv")
plot_ly(data = Mshoot, type = "scatter", mode = "markers", x = ~Year, y = ~Fatalities, color = ~Race, text = ~paste0("Location: ", Location, "<br> Shooter Sex: ", Gender ))