Get startedGet started for free

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")

This exercise is part of the course

Interactive Maps with leaflet in R

View Course

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Add a legend that displays the colors used in pal
m %>% 
    ___(pal = pal, 
              values = c("Public", "Private", "For-Profit"))
Edit and Run Code