LoslegenKostenlos loslegen

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.

Diese Übung ist Teil des Kurses

Interactive Maps with leaflet in R

Kurs anzeigen

Anleitung zur Übung

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

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

# 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
Code bearbeiten und ausführen