CommencerCommencer gratuitement

Mapping Public Colleges

We can add each sector to our map as a layer providing users with the ability to select which sectors are displayed. To do this we will make use of a new argument to the addCircleMarkers() function, called a group.

leaflet() %>% 
        addProviderTiles("CartoDB") %>% 
        addCircleMarkers(data = public, 
                         label = ~htmlEscape(name), 
                         group = "Public")

We'll integrate another feature into our map from the htmltools library. Wrapping our labels with the htmlEscape() function will sanitize characters that may be interpreted as HTML. This will prevent any of the college names from appearing with unintended formatting.

The color palette pal we created in Chapter 2 has been loaded for you.

Cet exercice fait partie du cours

Interactive Maps with leaflet in R

Afficher le cours

Instructions

  • Load the htmltools package.
  • Filter the ipeds data to create a data frame with only public colleges.
  • Create a leaflet map of public colleges.
  • Add the group argument to addCircleMarkers().

Exercice interactif pratique

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

# Load the htmltools package
___

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

# Create a leaflet map of public colleges called m3 
m3 <- leaflet() %>% 
        addProviderTiles("CartoDB") %>% 
        addCircleMarkers(data = ___, radius = 2, label = ~htmlEscape(name),
                         color = ~pal(sector_label), ___ = "Public")

m3
Modifier et exécuter le code