22  openairmaps primer

An introduction to openairmaps and the leaflet package


Jack Davison

The openairmaps package has been designed to create interactive HTML air quality maps. These are useful to understand the geospatial context of openair-type analysis, and to present data in an engaging way in dashboards and websites. This page gives a broad overview of openairmaps as well as leaflet, the package on which it is built.

22.1 Installation

openairmaps is on CRAN, so can be simply downloaded using install.packages().


If you would like to access new features and bug fixes before they’re pushed to CRAN, the development version of openairmaps can be downloaded directly from GitHub.

# install.packages("pak")

If you would like to use openairmaps in your analysis, don’t forget to load it using the library() function.

This chapter was produced using R version 4.3.0 and openairmaps version 0.8.0.

22.2 Background

As the R ecosystem developed, rmarkdown and, more recently, Quarto) emerged as capable tools for combining data analysis with document preparation. While these approaches can render typical .docx and .pdf outputs, one of their most common output formats is the HTML document. This format has many strengths, but a key one is interactivity; HTML widgets allow documents to be more informative and engaging. Numerous packages have been developed to easily develop these interactive widgets, such as plotly for plots, DT for tables, and leaflet for maps. The openairmaps package concerns itself with making leaflet maps.

Air quality data analysis — particularly as it pertains to long term monitoring data — naturally lends itself to being visualised spatially on a map. Monitoring networks are geographically distributed, and ignoring their geographical context may lead to incomplete insights at best and incorrect conclusions at worst! Furthermore, many air quality analysis tools are directional, asking questions of the data along the lines of “do elevated concentrations come from the North, South, East or West?” The natural question that follows is “well, what actually is it to the North/South/East/West that could be causing elevated concentrations?” — a map can help answer that question straightforwardly.

22.3 leaflet Primer

While openairmaps can be used without a thorough understanding of the leaflet package, some knowledge can definitely help! Figure 22.1 shows an example leaflet map. Try the following:

  • Click and drag to move the map around — you can go anywhere in the world!

  • Use your scroll wheel or the +/- options (top left) to zoom in and out.

  • Hover over the marker to see its label.

  • Click on the marker to see a popup.

  • Use the “layer control” menu (top right) to show and hide the marker, and swap between base maps.


leaflet() %>%
  addTiles(group = "OSM") %>%
  addProviderTiles("CartoDB.Positron", group = "CartoDB.Positron") %>%
    lat = 51.5034,
    lng = -0.1276,
    label = "10 Downing Street",
    popup = "This is where the UK Prime Minister lives!",
    group = "Marker"
  ) %>%
    baseGroups = c("CartoDB.Positron", "OSM"), 
    overlayGroups = "Marker"
Figure 22.1: An example of a leaflet map.

The anatomy of a leaflet map may look a little unusual, so to unpack:

  • Every leaflet map starts with the leaflet() function.

  • Different base maps can be added using addTiles() and addProviderTiles(). There are many of these base maps — some are minimalist and some are busy, some are colourful and some are black-and-white, some are plain and some are highly stylised!

  • Markers can be added using addMarkers() and other similar functions. These markers can have different features such as hover labels and popups.

  • The addLayersControl() function creates a menu which allows users to show/hide or swap between certain features of the map.

Don’t worry if you don’t fully understand all of this — the “all-in-one” functions in openairmaps deal with much of this for you! The only leaflet awareness you’ll need to use these functions is knowledge of the available base map providers, which can be accessed using leaflet::providers. These are shown in the collapsible below (the list is very long!).

[1] "OpenStreetMap"

[1] "OpenStreetMap.Mapnik"

[1] "OpenStreetMap.DE"

[1] "OpenStreetMap.CH"

[1] "OpenStreetMap.France"

[1] "OpenStreetMap.HOT"

[1] "OpenStreetMap.BZH"

[1] "OpenSeaMap"

[1] "OpenPtMap"

[1] "OpenTopoMap"

[1] "OpenRailwayMap"

[1] "OpenFireMap"

[1] "SafeCast"

[1] "Thunderforest"

[1] "Thunderforest.OpenCycleMap"

[1] "Thunderforest.Transport"

[1] "Thunderforest.TransportDark"

[1] "Thunderforest.SpinalMap"

[1] "Thunderforest.Landscape"

[1] "Thunderforest.Outdoors"

[1] "Thunderforest.Pioneer"

[1] "Thunderforest.MobileAtlas"

[1] "Thunderforest.Neighbourhood"

[1] "OpenMapSurfer"

[1] "OpenMapSurfer.Roads"

[1] "OpenMapSurfer.Hybrid"

[1] "OpenMapSurfer.AdminBounds"

[1] "OpenMapSurfer.ContourLines"

[1] "OpenMapSurfer.Hillshade"

[1] "OpenMapSurfer.ElementsAtRisk"

[1] "Hydda"

[1] "Hydda.Full"

[1] "Hydda.Base"

[1] "Hydda.RoadsAndLabels"

[1] "MapBox"

[1] "Stamen"

[1] "Stamen.Toner"

[1] "Stamen.TonerBackground"

[1] "Stamen.TonerHybrid"

[1] "Stamen.TonerLines"

[1] "Stamen.TonerLabels"

[1] "Stamen.TonerLite"

[1] "Stamen.Watercolor"

[1] "Stamen.Terrain"

[1] "Stamen.TerrainBackground"

[1] "Stamen.TerrainLabels"

[1] "Stamen.TopOSMRelief"

[1] "Stamen.TopOSMFeatures"

[1] "TomTom"

[1] "TomTom.Basic"

[1] "TomTom.Hybrid"

[1] "TomTom.Labels"

[1] "Esri"

[1] "Esri.WorldStreetMap"

[1] "Esri.DeLorme"

[1] "Esri.WorldTopoMap"

[1] "Esri.WorldImagery"

[1] "Esri.WorldTerrain"

[1] "Esri.WorldShadedRelief"

[1] "Esri.WorldPhysical"

[1] "Esri.OceanBasemap"

[1] "Esri.NatGeoWorldMap"

[1] "Esri.WorldGrayCanvas"

[1] "OpenWeatherMap"

[1] "OpenWeatherMap.Clouds"

[1] "OpenWeatherMap.CloudsClassic"

[1] "OpenWeatherMap.Precipitation"

[1] "OpenWeatherMap.PrecipitationClassic"

[1] "OpenWeatherMap.Rain"

[1] "OpenWeatherMap.RainClassic"

[1] "OpenWeatherMap.Pressure"

[1] "OpenWeatherMap.PressureContour"

[1] "OpenWeatherMap.Wind"

[1] "OpenWeatherMap.Temperature"

[1] "OpenWeatherMap.Snow"

[1] "HERE"

[1] "HERE.normalDay"

[1] "HERE.normalDayCustom"

[1] "HERE.normalDayGrey"

[1] "HERE.normalDayMobile"

[1] "HERE.normalDayGreyMobile"

[1] "HERE.normalDayTransit"

[1] "HERE.normalDayTransitMobile"

[1] "HERE.normalDayTraffic"

[1] "HERE.normalNight"

[1] "HERE.normalNightMobile"

[1] "HERE.normalNightGrey"

[1] "HERE.normalNightGreyMobile"

[1] "HERE.normalNightTransit"

[1] "HERE.normalNightTransitMobile"

[1] "HERE.reducedDay"

[1] "HERE.reducedNight"

[1] "HERE.basicMap"

[1] "HERE.mapLabels"

[1] "HERE.trafficFlow"

[1] "HERE.carnavDayGrey"

[1] "HERE.hybridDay"

[1] "HERE.hybridDayMobile"

[1] "HERE.hybridDayTransit"

[1] "HERE.hybridDayGrey"

[1] "HERE.hybridDayTraffic"

[1] "HERE.pedestrianDay"

[1] "HERE.pedestrianNight"

[1] "HERE.satelliteDay"

[1] "HERE.terrainDay"

[1] "HERE.terrainDayMobile"

[1] "FreeMapSK"

[1] "MtbMap"

[1] "CartoDB"

[1] "CartoDB.Positron"

[1] "CartoDB.PositronNoLabels"

[1] "CartoDB.PositronOnlyLabels"

[1] "CartoDB.DarkMatter"

[1] "CartoDB.DarkMatterNoLabels"

[1] "CartoDB.DarkMatterOnlyLabels"

[1] "CartoDB.Voyager"

[1] "CartoDB.VoyagerNoLabels"

[1] "CartoDB.VoyagerOnlyLabels"

[1] "CartoDB.VoyagerLabelsUnder"

[1] "HikeBike"

[1] "HikeBike.HikeBike"

[1] "HikeBike.HillShading"

[1] "BasemapAT"

[1] "BasemapAT.basemap"

[1] "BasemapAT.grau"

[1] "BasemapAT.overlay"

[1] "BasemapAT.highdpi"

[1] "BasemapAT.orthofoto"

[1] "nlmaps"

[1] "nlmaps.standaard"

[1] "nlmaps.pastel"

[1] "nlmaps.grijs"

[1] "nlmaps.luchtfoto"


[1] "NASAGIBS.ModisTerraTrueColorCR"

[1] "NASAGIBS.ModisTerraBands367CR"

[1] "NASAGIBS.ViirsEarthAtNight2012"

[1] "NASAGIBS.ModisTerraLSTDay"

[1] "NASAGIBS.ModisTerraSnowCover"

[1] "NASAGIBS.ModisTerraAOD"

[1] "NASAGIBS.ModisTerraChlorophyll"

[1] "NLS"

[1] "JusticeMap"

[1] "JusticeMap.income"

[1] "JusticeMap.americanIndian"

[1] "JusticeMap.asian"

[1] "JusticeMap.black"

[1] "JusticeMap.hispanic"

[1] "JusticeMap.multi"

[1] "JusticeMap.nonWhite"

[1] "JusticeMap.white"

[1] "JusticeMap.plurality"

[1] "Wikimedia"

[1] "GeoportailFrance"

[1] "GeoportailFrance.parcels"

[1] "GeoportailFrance.ignMaps"

[1] "GeoportailFrance.maps"

[1] "GeoportailFrance.orthos"

[1] "OneMapSG"

[1] "OneMapSG.Default"

[1] "OneMapSG.Night"

[1] "OneMapSG.Original"

[1] "OneMapSG.Grey"

[1] "OneMapSG.LandLot"

If you would like to use openairmaps in a more advanced way (for example, using the “marker” class of functions), you may find it useful to learn some leaflet. Its authors have written an excellent free guide to help you get started.

22.4 Functions

openairmaps currently has three main families of functions — those being network visualisation, directional analysis, and trajectory analysis — plus a handful of “utility” functions. These are summarised in Table 22.1. The functions are further divided into two categories:

  • “All-in-one” functions are most like openair functions in that they will construct a map from the ground-up. These functions provide the quickest route from data to a HTML map.

  • “Marker” functions are most like leaflet functions in that they add layers onto pre-existing leaflet maps. These are more complicated to use, but are more powerful if you are more familiar with how leaflet works.

Table 22.1:

The openairmaps toolkit.

Family Description All-in-one Markers

Network Visualisation

Visualises any of the networks made available by importMeta().

Directional Analysis

Uses any of the openair directional analysis plots (e.g., polarPlot()) as markers, allowing them to be viewed in their geospatial context.

Trajectory Analysis

Creates interactive visualisations of HYSPLIT trajectories, allowing for finer investigations when compared with their static openair counterparts.

The next few pages will discuss each of these function families in turn, starting with network visualisation.