This function is intended as a more up-to-date implementation of
leaflet::awesomeIcons(). Key benefits include having access to modern Font
Awesome, Bootstrap and Ionicons icons, and allowing any colour to be used the
marker and icon.
Usage
magicIcons(
icon = "circle",
markerColor = leaf.magic::awesomePalette$blue,
iconColor = leaf.magic::awesomePalette$white,
marker = "marker",
markerSize = 30L,
library = "fontawesome",
className = NULL,
dir = tempdir()
)Arguments
- icon
Name of the icon. A full list of available icons can be found using
fontawesome::fa_metadata()or the equivalent function for otherlibraryoptions.- markerColor
The color of the marker. Not used when
marker = "none".- iconColor
The color of the icon.
- marker
Defaults to
"marker", which uses the standard teardrop shaped marker, similar toleaflet::addMarkers(). Other options are"circle","square","star","heart", and"diamond", which place the icon inside of the respective shape. Also available is"none", which removes the marker entirely and places the icon directly on the map.- markerSize
The size of the marker. Defaults to
30, which is roughly the same size asleaflet::addMarkers().- library
One of
"fontawesome","bootstrap","ionicons", or"lucide"defining the icon library of interest. Defaults to"fontawesome".- className
a custom class name to assign to both icon and shadow images
- dir
The directory in which markers are saved. By default this is
tempdir(), which is a temporary directory after each session. Providing an alternative directory will allow markers to persist between R sessions.
Value
a leaflet::iconList(), to be passed to the icon argument of
leaflet::addMarkers()
Details
This function uses magick::image_composite() to knit together the icon and
marker symbol of choice, and saves it to a temporary directory. It is also
intelligent enough to not try to recreate a marker that already exists, so
you may find the use of magicIcons() speeds up the more you use it in a
single session.
Examples
# create map
library(leaflet)
port_talbot |>
dplyr::mutate(
icon = dplyr::recode_values(
site_type,
"Urban Industrial" ~ "industry",
"Urban Traffic" ~ "car",
"Urban Background" ~ "city",
default = "x"
),
color = dplyr::recode_values(
site_type,
"Urban Industrial" ~ "#12436D",
"Urban Traffic" ~ "#801650",
"Urban Background" ~ "#28A197",
default = "#3D3D3DFF"
)
) |>
leaflet() |>
addProviderTiles("CartoDB.Voyager") |>
addMarkers(
icon = ~ magicIcons(icon, color, "white"),
lat = ~ latitude,
lng = ~ longitude,
popup = ~site
)
