Adding Popups and Storing your Map
To make our map more informative we can add popups. To add popups that appear when a marker is clicked we need to specify the popup
argument in the addMarkers()
function. Once we have a map we would like to preserve, we can store it in an object. Then we can pipe this object into functions to add or edit the map's layers.
dc_nyc <-
leaflet() %>%
addTiles() %>%
addMarkers(lng = -73.98575, lat = 40.74856,
popup = "DataCamp - NYC")
dc_nyc %>%
setView(lng = -73.98575, lat = 40.74856,
zoom = 2)
Let's try adding popups to both DataCamp location markers and storing our map in an object.
This exercise is part of the course
Interactive Maps with leaflet in R
Exercise instructions
- Add the
popup
argument toaddMarkers()
to display the value in the hq column and store theleaflet
map in an object calledmap
. - Center the view of
map
on the Belgium HQ with a zoom of 5 and store it inmap_zoom
. - Print the
map_zoom
object.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Store leaflet hq map in an object called map
___ <- leaflet() %>%
addProviderTiles("CartoDB") %>%
# Use dc_hq to add the hq column as popups
addMarkers(lng = dc_hq$lon, lat = dc_hq$lat,
popup = ___$___)
# Center the view of map on the Belgium HQ with a zoom of 5
map_zoom <- ___ %>%
setView(lat = 50.881363, lng = 4.717863,
zoom = ___)
# Print map_zoom
___