Get startedGet started for free

Final Map

Because our leaflet map is built in layers, we can add different types of information to the same base map (e.g., points and polygons). When adding new layers based on different data to an existing leaflet object we must specify the data argument with the function that creates the new layer to override the data that is piped through the chain of functions.

m4  %>% 
    addCircleMarkers(data = private)

Let's combine our point and polygon maps to add a layer highlighting America's wealthiest zip codes to our map of every college in America. To get us started, the last layered version of the college map m4 has been printed for you and the wealthy_zips SpatialPolygonsDataFrame is pre-loaded.

After you create the final map, take a few minutes to explore (and enjoy) your leaflet map!

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 polygons using wealthy_zips
final_map <- ___ %>% 
   addPolygons(data = ___, weight = 1, fillOpacity = .5, color = "Grey",  group = "Wealthy Zip Codes", 
              label = ~paste0("Mean Income: ", dollar(mean_income)),
              highlight = highlightOptions(weight = 5, color = "white", bringToFront = TRUE)) %>% 
    # Update layer controls including "Wealthy Zip Codes"
    addLayersControl(baseGroups = c("OSM", "Carto", "Esri"), 
                         overlayGroups = c("Public", "Private", "For-Profit", ___))
Edit and Run Code