This is the code and output for an 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 2.2.1.9000     ✔ purrr   0.2.4     
## ✔ tibble  1.4.2          ✔ dplyr   0.7.4     
## ✔ tidyr   0.8.0          ✔ stringr 1.3.0     
## ✔ readr   1.1.1          ✔ forcats 0.3.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$CartoDB.Positron)
# Try stacking them up:
leaflet() %>%
  addProviderTiles(providers$Esri.WorldShadedRelief) %>%
  addProviderTiles(providers$Stamen.TonerLabels)

Load in data1

# Let's try adding markers
# Get Airbnb Data
listings <- read_csv("../data/listings.csv")
## Parsed with column specification:
## cols(
##   .default = col_character(),
##   id = col_integer(),
##   scrape_id = col_double(),
##   last_scraped = col_date(format = ""),
##   host_id = col_integer(),
##   host_since = col_date(format = ""),
##   host_listings_count = col_integer(),
##   host_total_listings_count = col_integer(),
##   latitude = col_double(),
##   longitude = col_double(),
##   accommodates = col_integer(),
##   bathrooms = col_double(),
##   bedrooms = col_integer(),
##   beds = col_integer(),
##   square_feet = col_integer(),
##   guests_included = col_integer(),
##   minimum_nights = col_integer(),
##   maximum_nights = col_integer(),
##   availability_30 = col_integer(),
##   availability_60 = col_integer(),
##   availability_90 = col_integer()
##   # ... with 14 more columns
## )
## See spec(...) for full column specifications.
# Get rid of commas and dollar signs, make price numeric
listings$price <- as.numeric(gsub("[$,]", "", listings$price))

Adding Markers

# Add markers to map as regular markers (this is a nightmare, so we'll skip rendering this)
leaflet() %>%
  addTiles() %>%
  addMarkers(data = listings)
# Let's try using circle markers instead
leaflet() %>%
  addTiles() %>%
  addCircleMarkers(data = listings)
## Assuming 'longitude' and 'latitude' are longitude and latitude, respectively
# Better, but we can customize those markers to do all kinds of stuff!
leaflet() %>%
  addTiles() %>%
  addCircleMarkers(data = listings,
                   stroke = FALSE
                   )
## Assuming 'longitude' and 'latitude' are longitude and latitude, respectively
leaflet() %>%
  addTiles() %>%
  addCircleMarkers(data = listings,
                   weight = 1,
                   color = "black",
                   fillColor = "steelblue"
  )
## Assuming 'longitude' and 'latitude' are longitude and latitude, respectively
leaflet() %>%
  addTiles() %>%
  addCircleMarkers(data = listings,
                   weight = 1,
                   color = "black",
                   fillColor = "steelblue",
                   fillOpacity = 1,
                   radius = 5
  )
## Assuming 'longitude' and 'latitude' 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 = listings,
                   weight = 1,
                   color = "black",
                   fillColor = "steelblue",
                   fillOpacity = 1,
                   radius = 5,
                   popup = "Hello!"
  )
## Assuming 'longitude' and 'latitude' are longitude and latitude, respectively
# Better to use actual data for popup content
leaflet() %>%
  addTiles() %>%
  addCircleMarkers(data = listings,
                   weight = 1,
                   color = "black",
                   fillColor = "steelblue",
                   fillOpacity = 1,
                   radius = 5,
                   popup = ~host_name
  )
## Assuming 'longitude' and 'latitude' 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 <- colorFactor(topo.colors(3), listings$room_type)

# 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 = listings,
                   weight = 1,
                   color = "black",
                   fillColor = ~pal(room_type),
                   fillOpacity = 1,
                   radius = 5,
                   popup = ~paste0("<img src='",thumbnail_url,"'</img></br>",
                                   "<b><a href='",listing_url,"' target='_blank'>",name,"</a></b></br>",
                                   "Host: ",host_name,"</br>",
                                   "Bedrooms: ", bedrooms,"</br>",
                                   "Beds: ", beds,"</br>",
                                   "Bathrooms: ",bathrooms,"</br>",
                                   "Price: ",price, "</br>",
                                   "Rating: ", review_scores_rating
                                   )
                   ) %>%
  addLegend("bottomleft", pal = pal, values = listings$room_type, opacity = 1)
## Assuming 'longitude' and 'latitude' are longitude and latitude, respectively

  1. Data from Inside Airbnb