Skip to contents

The goal of leaf.magic is to overcome the somewhat outdated “Font Awesome” implementation in vanilla R leaflet by constructing SVG markers on the fly using modern icon libraries such as fontawesome, bsicons and lucidr. There are a few issues leaf.magic sets out to overcome:

leaf.magic attempts to solve all of this:

  • magicIcons() uses up-to-date versions of Font Awesome, Bootstrap, Lucide, and Ionicons icons - or your own text and SVG icons ✅

  • magicIcons() draws its own vector markers, so they can be any colour, size or shape (including shapes you design yourself) and stay sharp at any size ✅

  • leaf.magic exports functions like iconFactor() and addIconLegend() to map data to icons and explain them to users, just like colours in leaflet ✅

Installation

You can install the development version of leaf.magic like so:

# install.packages("pak")
pak::pak("jack-davison/leaf.magic")

Example

Pass magicIcons() to the icon argument of leaflet::addMarkers(). Like leaflet::awesomeIcons(), its arguments can be formulas, which are evaluated using the map’s data.

library(leaflet)
library(leaf.magic)

iconPal <- iconFactor(c("city", "industry", "car"), domain = port_talbot$site_type)
colPal <- colorFactor(c("#12436D", "#28A197", "#801650"), domain = port_talbot$site_type)

leaflet(port_talbot) |>
  addTiles() |>
  addMarkers(
    icon = magicIcons(
      icon = ~ iconPal(site_type),
      markerColor = ~ colPal(site_type),
      marker = ~ ifelse(open, "marker", "circle")
    ),
    popup = ~site
  ) |>
  addIconLegend(
    pal = iconPal,
    values = ~site_type,
    colors = colPal,
    title = "Site Type"
  )
A map of air quality monitoring sites in Port Talbot, using {leaf.magic} markers which show the type of each site with both an icon and a colour.
A map of air quality monitoring sites in Port Talbot, using {leaf.magic} markers which show the type of each site with both an icon and a colour.

leaf.magic is thoroughly documented on the package website. Start with the getting started guide, then see the articles on customising markers and icon scales & legends.

Attributions

leaf.magic is licensed under the MIT License, and would not be possible without the following icon libraries:

Code of Conduct

Please note that the leaf.magic project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.