
Create a set of Font Awesome, Bootstrap, Ionicons or Lucide Markers
Source:R/magicIcons.R
magicIcons.RdThis function is intended as a more up-to-date implementation of
leaflet::awesomeIcons(). Key benefits include having access to modern Font
Awesome, Bootstrap, Ionicons and Lucide icons, and allowing any colour to be
used for the marker and icon.
Usage
magicIcons(
icon = "circle",
markerColor = leaf.magic::awesomePalette$blue,
iconColor = "auto",
marker = "marker",
markerSize = 30L,
outlineColor = NULL,
outlineWidth = NULL,
library = "fontawesome",
className = NULL,
dir = tempdir(),
shadow = TRUE
)Arguments
- icon
Name of the icon. A full list of available icons can be found using
fontawesome::fa_metadata(), https://icons.getbootstrap.com/, https://ionic.io/ionicons or https://lucide.dev/icons/, depending onlibrary. Whenlibrary = "text", the text (or numbers) to show in each marker instead, and whenlibrary = "svg", a string of SVG markup. UseNAfor a marker with no icon, e.g., when the marker shape alone conveys the meaning.- markerColor
The color of the marker. Not used when
marker = "none".- iconColor
The color of the icon.
"auto"chooses black or white for each marker, whichever has the greater contrast withmarkerColor. Whenmarker = "none","auto"instead gives the icon the colourmarkerColor, as there is no marker for it to contrast with.- marker
The shape of the marker. Defaults to
"marker", which uses the standard teardrop shaped pin, similar toleaflet::addMarkers(). The other built-in shapes are:Pointed shapes, which sit on their location with their tip:
"marker-square"(a pin with a square head) and"callout"(a speech bubble).Shapes centred on their location:
"circle","square","diamond","triangle","hexagon","star"and"heart"."none", which removes the marker entirely and places the icon directly on the map.
Custom shapes can be created using
markerShape().markermay be a character vector of shape names, a singlemarkerShape(), or a list ofmarkerShape()s and/or shape names to use a different shape for each marker. Missing (NA) shapes are drawn as"none".- markerSize
The width of the marker in pixels. Defaults to
30, which is roughly the same size asleaflet::addMarkers().- outlineColor
The colour of the outline around each marker. Defaults to a slightly darker shade of
markerColor. A contrasting colour (e.g.,"white") helps markers stand out against similarly coloured basemaps.- outlineWidth
The width of the outline in pixels. Defaults to a subtle outline which scales with
markerSize(around half a pixel at the default size). Markers are not shrunk to make room for wider outlines, so they grow by the extra width.- library
One of
"fontawesome","bootstrap","ionicons", or"lucide"defining the icon library of interest. Defaults to"fontawesome". Alternatively,"text"showsiconas text, which is useful for numbered or lettered markers (e.g.,icon = 1:10). Text is bold and shrinks to fit, so is best kept to around three characters."svg"usesiconas raw SVG markup (e.g.,'<svg viewBox="0 0 24 24">...</svg>'), so any icon can be used. Parts of the SVG which usecurrentColoror have no fill of their own take oniconColor. Only use SVG markup you trust, as it is inserted directly into legends.- 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 deleted at the end of each R session. Providing an alternative directory will allow markers to persist between R sessions.diris created if it does not already exist.- shadow
Should markers have a shadow? Defaults to
TRUE. As with the defaultleaflet::addMarkers()marker, pointed shapes such as"marker"cast a shadow up and to the right, while other shapes have a soft shadow just beneath them."none"never has a shadow. The shadow of a custom shape is set using theshadowargument ofmarkerShape().
Value
a leaflet::iconList(), to be passed to the icon argument of
leaflet::addMarkers(). If any argument is a formula, a formula which
builds that leaflet::iconList() from the layer's data is returned
instead.
Details
Each marker is built as a single SVG image, with the icon nested inside of
the marker shape, and saved to dir. As markers are vector images, they
stay sharp at any markerSize and on high resolution screens. Markers are
only built once for each combination of icon, colours, marker shape and
library, so you may find the use of magicIcons() speeds up the more you use
it in a single session.
Each icon library is provided by a separate package which must be installed
to use it: {fontawesome}, {bsicons} or {lucidr}. Ionicons are
downloaded from https://unpkg.com the first time each icon is used in a
session, so require an internet connection.
Using formulas
As with leaflet::awesomeIcons(), icon,
markerColor, iconColor, marker, markerSize, outlineColor and
outlineWidth may be one-sided
formulas (e.g., ~site_type), which are evaluated against the data of the
layer the markers are added to. Markers are then built when the layer is
added, so magicIcons() must be passed directly to the icon argument of
leaflet::addMarkers(). Formulas and constants can be mixed freely, and
formulas may call other functions, such as an iconFactor() scale or a
leaflet::colorFactor() palette (e.g., markerColor = ~pal(site_type)).
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() |>
addTiles() |>
addMarkers(
icon = magicIcons(icon = ~icon, markerColor = ~color, iconColor = "white"),
lat = ~latitude,
lng = ~longitude,
popup = ~site
)