Exercise

A Legendary Map

Adding information to our map using color is great, but it is only helpful if we remember what the colors represent. With addLegend() we can add a legend to remind us.

There are several arguments we can use to custom the legend to our liking, including opacity, title, and position. To create a legend for our colorNumeric() example, we would do the following.

pal <- colorNumeric(palette = "RdBu", domain = c(25:50))

ipeds %>% 
    leaflet() %>% 
        addProviderTiles("CartoDB")  %>% 
        addCircleMarkers(radius = 2,
                         color = ~pal(lat)) %>% 
         addLegend(pal = pal,
                   values = c(25:50),
                   opacity = 0.75,
                   title = "Latitude",
                   position = "topleft")

Instructions 1/2

undefined XP
  • 1

    Add a legend that displays the colors used in pal to m.

  • 2

    Customize the legend to have an opacity of .5, a title of "Sector", and a position of "topright".