In this post, we will learn how to make multicolored markers for interactive maps using the leaflet() package in R. This trick is used mostly for coloring categorical variables on an interactive map
Libraries
The only package we need is the leaflet() package. The code for this is below.
library(leaflet)
We will now create our palette, which will be used with our map.
Create Palette and Map
We will start by making our palette. The data for this example is the longitude and latitude of cities all across the US. One of the variables in this data set is whether a city is incorporated or not. Incorporation means that a city can govern itself and provide its own services, while an unincorporated city relies on the county to provide basic services. Our code for the palette is below.
# Colors should be: "red", "blue", incorporated or not incorporated
pal <- colorFactor(palette = c("red", "blue"),
levels = c("TRUE", "FALSE"))
We use the function colorFactor() and set incorporated cities to red and unincorporated cities to blue. We are now ready to create our map.
In the code below, we create our map. We call leaflet() first as this creates the map. Then we call addProviderTiles to select the map we want. The addCircleMarkers() function adds the markers we want. Our data is limited to 100 rows from the dataset, as using all the data would create a city of dots that are difficult to see. We set the radius of the dots and then set the color argument with our palette called “pal”. We also set labels for each marker that cannot be seen below.
# Add circle markers that color incorporated using pal() and the values of sector_label
leaflet() %>%
addProviderTiles("CartoDB") %>%
addCircleMarkers(data = uscities[12500:12600,], radius = 2,
color = ~pal(as.factor(incorporated)),
label = ~paste0(city, " (", incorporated, ")"))

Below, we will extend our code to include a legend
Make the Map with a Legend
The code below includes the use of the addLegend() function, which adds a legend. The code is mostly self-explanatory
#Add legend
# Customize the legend
pal <- colorFactor(palette = c("red", "blue"),
levels = c("TRUE", "FALSE"))
# Add circle markers that color colleges using pal() and the values of sector_label
leaflet() %>%
addProviderTiles("CartoDB") %>%
addCircleMarkers(data = uscities[12500:12600,], radius = 2,
color = ~pal(as.factor(incorporated)),
label = ~paste0(city, " (", incorporated, ")"))%>%
addLegend(pal = pal,
values = c("TRUE", "FALSE"),
# opacity of .5, title of Sector, and position of topright
opacity = 0.5, title = "Sector", position = "bottomleft")

You can see the legend in the lower left-hand corner of the picture above.
Search Maps
It is also possible to search a map. In the code below, we use two new functions addSearchOSM() and addReverseSearchOSM. These functions allow you to move around the map and find locations and their names. There is a video below showing the power of this technique.
#Search maps
library(leaflet)
library(leaflet.extras)
leaflet() %>%
addTiles() %>%
addSearchOSM() %>%
addReverseSearchOSM()
Conclusion
The leaflet package provides multiple tools to enhance and improve interactive maps. The techniques presented here are just a handful of the many ways you can design your own maps.

