A simple map

Show the quakes data, or a subset, on a map:

library(leaflet)
quakes %>% 
  ___ %>% 
  leaflet() %>% 
  addTiles() %>% 
  addCircles()

► Solution:

library(leaflet)
quakes %>% 
  head(50) %>% 
  leaflet() %>% 
  addTiles() %>% 
  addCircles()
## Assuming "long" and "lat" are longitude and latitude, respectively

A map with a tooltip

Add a popup to the tiles. Click on one of the circles

Hint: Use str_c() or paste0() to concatenate text and data.

library(leaflet)
___ %>% 
  addCircles()

► Solution:

library(leaflet)
quakes %>% 
  head(50) %>% 
  leaflet() %>% 
  addTiles() %>% 
  addCircles(popup = ~str_c("Magnitude: ", mag))
## Assuming "long" and "lat" are longitude and latitude, respectively

Other kinds of layer

Explore further options in ?addTiles.

More exercises

See section 4.2.3 in https://plotly-book.cpsievert.me/linking-views-without-shiny.html for linking a map to a plot.

Copyright © 2018 Kirill Müller. Licensed under CC BY-NC 4.0.