CommencerCommencer gratuitement

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.

Cet exercice fait partie du cours

Interactive Maps with leaflet in R

Afficher le cours

Instructions

  • 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.

Exercice interactif pratique

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

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

# Add private colleges to `m3` as a new layer
m3 <- m3 %>% 
        ___(data = private, radius = 2, label = ~htmlEscape(name),
                         color = ~pal(sector_label), group = "Private") %>% 
        ___(___ = c("Public", "Private"))

m3
Modifier et exécuter le code