CommencerCommencer gratuitement

Layers of a leaflet map

As you saw earlier, leaflet maps are created in layers, not unlike a ggplot plot. The sequence of layers placed is important, since each new layer is stacked on top of existing ones.

In this exercise, you will need to complete the code below to produce the map seen in the picture.

Map for exercise.

There are four sf objects stored here as:

  • london_poly
  • london_loop
  • london_capital
  • listings_geo

The sf and tidyverse libraries have been loaded.

Cet exercice fait partie du cours

Building Dashboards with shinydashboard

Afficher le cours

Instructions

  • Import the leaflet library.
  • Use the default map provider in this map.
  • Add two polylines, one for loop_geo and one for capital_geo.
  • Add regular markers with clustered points, so that sf objects of type POINT are grouped together on the rendered map.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

# Import the leaflet library
____
loop_geo <- london_loop$geometry[[1]]; capital_geo <- london_capital$geometry[[1]]
leaflet(london_poly) %>% 
  # Use the default map provider
  ____ %>% 
  # Add two polylines
  ____(loop_geo[,1], loop_geo[,2],
               color = "red") %>%
  ____(____,
               color = "green") %>%
  # Add regular markers with clustered points
  ____(data = listings_geo,
             ____) %>% 
  addPolygons(label=~Name)
Modifier et exécuter le code