Exercise

Creating a Color Palette using colorFactor

So far we have only used color to customize the style of our map. With colorFactor() we can create a color palette that maps colors the levels of a factor variable.

pal <- 
   colorFactor(palette = c("blue", "red", "green"), 
               levels = c("Public", "Private", "For-Profit"))

m %>% 
    addCircleMarkers(color = ~pal(sector_label))

Why might we not want to use this particular color palette?

If you are interested in using a continuous variable to color a map see colorNumeric().

pal <- colorNumeric(palette = "RdBu", domain = c(25:50))

ipeds %>% 
    leaflet() %>% 
        addProviderTiles("CartoDB")  %>% 
        addCircleMarkers(radius = 2, color = ~pal(lat))

Instructions

100 XP
  • Make a color palette called pal for the values of sector_label using colorFactor() using "red", blue", and "#9b4a11".
  • Add circle markers that color colleges using pal() and the values of sector_label.