This tutorial covers how to create interactive maps in R, focusing on two types of visualizations:
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:
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:
USArrests data set. doneUrbanPop below the name on a new line.radius = 2) for each city in the us_cities data set where state capitals are colored “red” and all other cities are colored yellowHints:
bringToFront = FALSE in your highlight to avoid masking the city labels when hovering over a stateshapeurl <- "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)