Exercise

Mapping Public and Private Colleges

We can add private colleges exactly how we added public colleges. Then using the addLayersControl() function with the overlayGroups argument we can give our users the ability to display public and/or private colleges. The overlayGroups argument takes a vector of groups that we have defined while creating our layers (i.e., public and private). As a reminder, here is how we can add public colleges to our map as a layer:

# Create data frame called public with only public colleges
public <- filter(ipeds, sector_label == "Public")  

# Add public colleges as a layer and save map as `m3`
m3 <- leaflet() %>% 
        addProviderTiles("CartoDB") %>% 
        addCircleMarkers(data = public, radius = 2, 
                         label = ~htmlEscape(name),
                         color = ~pal(sector_label),
                         group = "Public") %>% 
        addLayersControl(overlayGroups = c("Public"))

The htmltools library, color palette pal and the m3 map with public colleges have been loaded for you.

Instructions

100 XP
  • Filter the ipeds data to create a data frame with only private colleges.
  • Add private colleges to m3 as a new layer.
  • Use the addLayersControl() function and overlayGroups argument to allow users to toggle the public and private layers.