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.
Deze oefening maakt deel uit van de cursus
Interactive Maps with leaflet in R
Oefeninstructies
- Load the
htmltoolspackage. - Filter the
ipedsdata to create a data frame with only public colleges. - Create a
leafletmap of public colleges. - Add the
groupargument toaddCircleMarkers().
Praktische interactieve oefening
Probeer deze oefening eens door deze voorbeeldcode in te vullen.
# 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