Skip to content

About

πŸ“ R package for more customisable marker icons in R Leaflet

Topics

Resources

Code of conduct

Contributing

Stars

10 stars

Watchers

1 watching

Forks

Latest commit

Β 

History

63 Commits

Folders and files

Repository files navigation

{leaf.magic} - Use Modern Icon Libraries in Leaflet Markers leaf.magic website

CRAN status R-CMD-check

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:

  • leaflet::awesomeIcons() is limited to Font Awesome 4 πŸ‘Ž

  • leaflet::awesomeIcons() is limited to a handful of discrete colours πŸ‘Ž

  • leaflet::awesomeIcons() is limited to a single marker size and shape πŸ‘Ž

  • Vanilla {leaflet} doesn't provide any additional features to construct legends, icon palettes, or such like πŸ‘Ž

{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.

{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.

About

πŸ“ R package for more customisable marker icons in R Leaflet

Topics

Resources

Code of conduct

Contributing

Stars

10 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages