This is the code and output for a Spring 2019 instruction session on how to use the leaflet R package to create interactive maps.

Load Packages

# Load in packages
library(tidyverse)
## ── Attaching packages ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.1.1       ✔ purrr   0.3.2  
## ✔ tibble  2.1.1       ✔ dplyr   0.8.0.1
## ✔ tidyr   0.8.3       ✔ stringr 1.4.0  
## ✔ readr   1.3.1       ✔ forcats 0.4.0
## ── Conflicts ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(leaflet)

Leaflet

# Take a look at Leaflet
leaflet()
# Boring without tiles, so let's add a base layer
leaflet() %>%
  addTiles()
# We could try provider tiles, using list from: http://leaflet-extras.github.io/leaflet-providers/preview/index.html
leaflet() %>%
  addProviderTiles(providers$Stamen.Toner)
leaflet() %>%
  addProviderTiles(providers$Esri.WorldTerrain)
# Try stacking them up:
leaflet() %>%
  addProviderTiles(providers$Esri.WorldShadedRelief) %>%
  addProviderTiles(providers$Stamen.TonerLabels)

Load in data1

# Let's try adding markers
# Get CitiBike Data
stands <- read_csv("../data/processed/stands.csv")
## Parsed with column specification:
## cols(
##   station_name = col_character(),
##   lon = col_double(),
##   lat = col_double(),
##   mean_trip_duration = col_double(),
##   mean_age = col_double(),
##   rides = col_double(),
##   main_user = col_character(),
##   subscriber_pct = col_double()
## )

Adding Markers

# Add markers to map as regular markers (this is a nightmare, so we'll skip rendering this)
leaflet() %>%
  addTiles() %>%
  addMarkers(data = stands)
# Let's try using circle markers instead
leaflet() %>%
  addTiles() %>%
  addCircleMarkers(data = stands)
## Assuming "lon" and "lat" are longitude and latitude, respectively
# Better, but we can customize those markers to do all kinds of stuff!
leaflet() %>%
  addTiles() %>%
  addCircleMarkers(data = stands,
                   stroke = FALSE
                   )
## Assuming "lon" and "lat" are longitude and latitude, respectively
leaflet() %>%
  addTiles() %>%
  addCircleMarkers(data = stands,
                   weight = 1,
                   color = "black",
                   fillColor = "steelblue"
  )
## Assuming "lon" and "lat" are longitude and latitude, respectively
leaflet() %>%
  addTiles() %>%
  addCircleMarkers(data = stands,
                   weight = 1,
                   color = "black",
                   fillColor = "steelblue",
                   fillOpacity = 1,
                   radius = 5
  )
## Assuming "lon" and "lat" are longitude and latitude, respectively

Adding popups

# Now that we've customized markers a little, let's try adding a popup!
leaflet() %>%
  addTiles() %>%
  addCircleMarkers(data = stands,
                   weight = 1,
                   color = "black",
                   fillColor = "steelblue",
                   fillOpacity = 1,
                   radius = 5,
                   popup = "Hello!"
  )
## Assuming "lon" and "lat" are longitude and latitude, respectively
# Better to use actual data for popup content
leaflet() %>%
  addTiles() %>%
  addCircleMarkers(data = stands,
                   weight = 1,
                   color = "black",
                   fillColor = "steelblue",
                   fillOpacity = 1,
                   radius = 5,
                   popup = ~main_user
  )
## Assuming "lon" and "lat" are longitude and latitude, respectively

Adding finishing touches

# We can use data to color the dots, as well - first things first, let's get a color palette
pal <- colorNumeric(palette = "viridis", domain = stands$mean_age)

# We'll use data to color the dots and populate the popup (using html), and we'll also add a legend
leaflet() %>%
  addTiles() %>%
  addCircleMarkers(data = stands,
                   weight = 1,
                   color = "black",
                   fillColor = ~pal(mean_age),
                   fillOpacity = 1,
                   radius = 5,
                   popup = ~paste0(
                     "<b>", station_name, "</b></br>",
                     "Main Users: ", main_user, "</br>",
                     "Number of Riders: ", rides, "</br>",
                     "Average Rider Age: ", mean_age, "</br>",
                     "Average Trip Duration (seconds): ", round(mean_trip_duration, 1)
                     )
  ) %>%
  addLegend("bottomleft", 
            title = "Average Age of Riders", 
            pal = pal, 
            values = stands$mean_age, 
            opacity = 1)
## Assuming "lon" and "lat" are longitude and latitude, respectively

  1. Data from CitiBike