1. Introduction

This tutorial covers how to create interactive maps in R, focusing on two types of visualizations:

  1. Maps with markers, which are icons displayed at particular geographic locations
  2. Choropleth maps, sometimes called heat maps, where geographic units such as countries, states, or watersheds are colored according to a particular variable

There are many ways to create maps using R, but we will focus on leaflet, a popular open-source JavaScript library that is used by many professional organizations to create interactive maps.

In this tutorial we will require the following packages:

library(leaflet)    # The map-making package
library(geojsonio)  # A package for geographic and spatial data, requires the latest version of dplyr
library(dplyr)      # Used for data manipulation and merging
library(htmltools)  # Used for constructing map labels using HTML
library(plotly)
library(ggplot2)

In this tutorial we will use two datasets:

  1. The “quakes” dataset, which contains the locations of 1000 seismic events occuring near the Fiji islands since 1964.
data(quakes)
head(quakes)
##      lat   long depth mag stations
## 1 -20.42 181.62   562 4.8       41
## 2 -20.62 181.03   650 4.2       15
## 3 -26.00 184.10    42 5.4       43
## 4 -17.97 181.66   626 4.1       19
## 5 -20.42 181.96   649 4.0       11
## 6 -19.68 184.31   195 4.0       12

Questions:

The us_cities data set in the geojsonio package contains state capitals and all cities in the United States with populations larger than 40,000. The file located at “https://raw.githubusercontent.com/PublicaMundi/MappingAPI/master/data/geojson/us-states.json” contains spatial polygon boundaries for all 50 states (and the District of Columbia and Puerto Rico). The USArrests data set in the datasets package contains violant crime rates by state.

For this question you should create a map with the following features:

  1. Spatial polygons for each state filled according to colors from the “viridis” color scheme and based upon that state’s murder rate in the USArrests data set. done
  2. Black outlines for each spatial polygon. done
  3. A legend in the bottom left that depicts the color scheme with an informative title. done
  4. A highlight that outlines a state in white when a user hovers over it. done
  5. Popups that show each state’s name in bold, along with its UrbanPop below the name on a new line.
  6. Circle markers (with radius = 2) for each city in the us_cities data set where state capitals are colored “red” and all other cities are colored yellow
  7. Labels for each city that display the city’s name (it is okay to keep the state abbreviation) and the city’s population

Hints:

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)
USMap2 <- left_join(data.frame(Name = UnitedStates$name), USArrests, by = c("Name" ="State"))

myLabels <- paste(USMap2$Name)
myPopups <- paste("<strong>", USMap2$Name, "</strong>", "<br/> Urban Population:", USMap2$UrbanPop)

us_cities$color = ifelse(us_cities$capital == 2, "red", "yellow")

USMap <- leaflet(UnitedStates) %>% addTiles() %>% 
  addPolygons(
    fillColor = pal(USMap2$Murder),
    weight = 2,
    opacity = 1,
    color = "black",
    fillOpacity = 0.7,
    highlight = highlightOptions(weight = 3,
                   color = "white",
                   fillOpacity = 0.7,
                   bringToFront = FALSE),
    label = lapply(myLabels, HTML),
    popup = myPopups) %>%
  addLegend(pal = pal, values = us_cities$name,
            title = "Murder Rate, viridis", position = "bottomleft") %>%
  addCircleMarkers(us_cities, lng = us_cities$long, lat = us_cities$lat, radius = 2, color = us_cities$color, 
                   label = ~paste(us_cities$name, us_cities$pop))

USMap
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 = FALSE) %>%
  animation_opts(frame = 1000, easing = "elastic", redraw = FALSE)