Awesome Markers

These examples demonstrate the use of addAwesomeMarkers to easily add customizable markers, using the Leaflet.awesome-markers plugin.

Simple markers

Use makeAwesomeIcon with addAwesomeMarkers to specify a single icon that should be used for all markers.

library(leaflet)

# An icon using the Font Awesome library
icon.fa <- makeAwesomeIcon(icon = "flag", markerColor = "red",
  library="fa", iconColor = "black")

leaflet() %>% addTiles() %>%
  addAwesomeMarkers(
    lng=-118.456554, lat=34.078039,
    label="This is a label",
    icon = icon.fa)
# An icon using the ionicons library
icon.ion <- makeAwesomeIcon(icon = "home", markerColor = "green",
  library="ion")

# Marker + static label using custom label options
leaflet() %>% addTiles() %>%
  addAwesomeMarkers(
    lng=-118.456554, lat=34.078039,
    label="This is a static label",
    labelOptions = labelOptions(noHide = T),
    icon = icon.ion)

Lists of icons with awesomeIcons()

Use awesomeIcons instead of makeAwesomeIcon to create icons that are identical in most aspects but vary by one or a couple of parameters.

cities <- read.csv(textConnection("
City,Lat,Long,Pop
Boston,42.3601,-71.0589,645966
Hartford,41.7627,-72.6743,125017
New York City,40.7127,-74.0059,8406000
Philadelphia,39.9500,-75.1667,1553000
Pittsburgh,40.4397,-79.9764,305841
Providence,41.8236,-71.4222,177994
"))

icon.pop <- awesomeIcons(icon = "users",
  markerColor = ifelse(cities$Pop<500000,"blue","red"),
  library = "fa",
  iconColor = "black"
)

leaflet(cities) %>% addTiles() %>%
  addAwesomeMarkers(lng = ~Long, lat = ~Lat,
    label = ~City,
    icon = icon.pop)

Lists of icons with awesomeIconList()

Finally, you may want to use awesomeIconList instead of awesomeIcons or makeAwesomeIcon if you have two or more very different icons, that you want to select from using a column from your data.

# Make a list of icons (from two different icon libraries).
# We'll index into it based on name.
popIcons <- awesomeIconList(
  blue = makeAwesomeIcon(icon="person", library="ion", markerColor="blue"),
  red = makeAwesomeIcon(icon="users", library="fa", markerColor="red")
)

library(dplyr)
cities <- cities %>% mutate(PopCat = ifelse(Pop<500000,"blue","red"))

leaflet(cities) %>% addTiles() %>%
  addAwesomeMarkers(lng = ~Long, lat = ~Lat,
    label = ~City,
    labelOptions = labelOptions(noHide = TRUE),
    icon = ~popIcons[PopCat])

The "leaflet" R package is copyright © 2014-2016 RStudio, Inc.
The Leaflet JavaScript library is © 2010–2016 Vladimir Agafonkin, 2010–2011 CloudMade.
Maps © OpenStreetMap contributors unless otherwise noted.

Fork me on GitHub