Get startedGet started for free

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.

This exercise is part of the course

Interactive Maps with leaflet in R

View Course

Exercise 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().

Hands-on interactive exercise

Have a go at this exercise by completing this sample 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
Edit and Run Code