LoslegenKostenlos loslegen

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.

Diese Übung ist Teil des Kurses

Interactive Maps with leaflet in R

Kurs anzeigen

Anleitung zur Übung

  • Add the popup argument to addMarkers() to display the value in the hq column and store the leaflet map in an object called map.
  • Center the view of map on the Belgium HQ with a zoom of 5 and store it in map_zoom.
  • Print the map_zoom object.

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

# 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
___
Code bearbeiten und ausführen