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))
Diese Übung ist Teil des Kurses
Interactive Maps with leaflet in R
Anleitung zur Übung
- Make a color palette called
palfor the values ofsector_labelusingcolorFactor()using "red", blue", and "#9b4a11". - Add circle markers that color colleges using
pal()and the values ofsector_label.
Interaktive Übung
Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.
# Make a color palette called pal for the values of `sector_label` using `colorFactor()`
# Colors should be: "red", "blue", and "#9b4a11" for "Public", "Private", and "For-Profit" colleges, respectively
pal <- ___(palette = c("red", "blue", ___),
levels = c("Public", "Private", "For-Profit"))
# Add circle markers that color colleges using pal() and the values of sector_label
map2 <-
map %>%
addCircleMarkers(data = ca, radius = 2,
color = ~pal(sector_label),
label = ~paste0(name, " (", sector_label, ")"))
# Print map2
map2