Package 'maplore'

Title: Context Components that Help Readers Understand 'mapgl' Maps
Description: Adds components to 'mapgl' maps ('MapLibre GL JS' and 'Mapbox GL JS') that put what the reader sees in context: a live summary of the features in view compared with the whole map, a ranking of the features in view, a tooltip that shows where each value stands among all of them, a search box over the map's own data, notes on the data and its sources, and an animation that lifts the map's shapes into a grid sorted by their values. Everything runs in the browser, so the components work in static HTML, 'R Markdown' and 'Quarto' documents without a 'Shiny' server.
Authors: Juan Manuel Tocho [aut, cre, cph]
Maintainer: Juan Manuel Tocho <[email protected]>
License: MIT + file LICENSE
Version: 0.1.0
Built: 2026-12-04 20:03:37 UTC
Source: https://github.com/cran/maplore

Help Index


Add a tooltip that puts each value in context

Description

Adds a tooltip to a layer that shows, on hover (or tap on touch screens), a feature's value together with where it stands among all features: its rank, the share of features with a lower value, the difference with the average, and a small distribution strip with the feature marked.

Usage

add_context_tooltip(
  map,
  layer_id,
  column,
  name_column = NULL,
  label = column,
  weight = NULL,
  unit = "features",
  locale = "en-US",
  digits = 1,
  prefix = "",
  suffix = "",
  diff_suffix = NULL,
  color = NULL,
  highlight = TRUE,
  highlight_color = "#1f2328",
  labels = context_labels()
)

Arguments

map

A map created with mapgl::maplibre() or mapgl::mapboxgl().

layer_id

ID of the layer to summarize: a polygon layer (such as mapgl::add_fill_layer()) or a point layer (such as mapgl::add_circle_layer() or mapgl::add_symbol_layer()). Its source must be an sf object or inline GeoJSON added in R; vector tile sources are not supported.

column

Name of the numeric column to show.

name_column

Optional column with feature names, shown as the tooltip's title.

label

Name of the variable shown in the panel. Defaults to column.

weight

Optional numeric column used to weight the average, for example population.

unit

Name for the features, shown as "14 of 528 unit".

locale

Locale for number formatting, such as "en-US" or "es-AR".

digits

Number of decimals for values. Values of a million or more are abbreviated, such as ⁠12.4M⁠.

prefix, suffix

Text added before and after each value, such as "$" or "%".

diff_suffix

Text added after the difference between the view and the overall average. NULL (the default) uses suffix, except when suffix contains "%": a difference between percentages is in percentage points, so the points label is used instead.

color

Color for the feature's marker on the distribution strip. NULL (the default) uses the layer's own color scale when it is an interpolate() or step_expr() on column, and blue otherwise.

highlight

If TRUE, outlines the feature under the pointer.

highlight_color

Color of that outline.

labels

Panel texts, as returned by context_labels().

Details

Use it instead of the layer's own tooltip argument: with both, two tooltips are shown.

Value

The modified map object.

Examples

library(mapgl)

maplibre(center = c(-64, -38.5), zoom = 3.4) |>
  add_fill_layer(
    id = "deptos",
    source = ar_departments,
    fill_color = interpolate(
      column = "growth",
      values = c(-10, 0, 10),
      stops = c("#b2182b", "#f7f7f7", "#2166ac"),
      na_color = "#d9d9d9"
    )
  ) |>
  add_context_tooltip(
    layer_id = "deptos",
    column = "growth",
    name_column = "department",
    label = "Population growth",
    unit = "departments",
    suffix = "%"
  )

Add notes about the whole map

Description

Adds a small "Notes" button that opens a panel with context about the map and its data: where the data come from, what the figures do not include, and a link to learn more. Unlike a tooltip, the notes are about the whole map, not about one feature. They start collapsed, so they do not cover the map, and open with a click.

Usage

add_map_notes(
  map,
  source = NULL,
  notes = NULL,
  link = NULL,
  link_label = NULL,
  title = NULL,
  position = "bottom-left",
  width = 280,
  collapsed = TRUE,
  fade = c("none", "moving"),
  labels = context_labels()
)

Arguments

map

A map created with mapgl::maplibre() or mapgl::mapboxgl().

source

Where the data come from, such as "INDEC, 2022 census".

notes

One or more notes about the map or its data, each shown as a bullet, such as what the figures do not include.

link

Optional web address to learn more, starting with ⁠http://⁠ or ⁠https://⁠. It opens in a new tab.

link_label

Text of the link. NULL (the default) uses the notes_link label, "More information".

title

Title of the panel. NULL (the default) uses the notes_title label, "Notes".

position

Corner for the notes: one of "bottom-left" (the default), "bottom-right", "top-left" or "top-right".

width

Width of the open panel in pixels.

collapsed

If TRUE (the default), the notes start collapsed to a small button. FALSE shows them open.

fade

"moving" fades the panel while the map is panned or zoomed. "none" (the default) keeps it always solid.

labels

Panel texts, as returned by context_labels().

Details

Texts are shown as plain text, never as HTML.

Value

The modified map object.

Examples

library(mapgl)

maplibre(center = c(-64, -38.5), zoom = 3.4) |>
  add_fill_layer(id = "departments", source = ar_departments, fill_color = "#9ecae1") |>
  add_map_notes(
    source = "INDEC, population projections 2022-2035.",
    notes = c(
      "Growth is the total change between 2022 and 2035, not per year.",
      "Islas del Atlantico Sur (Islas Malvinas) has no estimates."
    ),
    link = "https://www.indec.gob.ar"
  )

Turn the map into a ranking of shapes

Description

Adds a button that lifts every polygon of a layer out of the map and lines them up in a grid, sorted by a column, all at the same size and with their rank, name and value below. Another click sends them back to their place. Shapes keep their form and color, so readers see at a glance both where each one is and how it ranks.

Usage

add_rank_morph(
  map,
  layer_id,
  column,
  name_column,
  order = c("desc", "asc"),
  n = 30,
  title = NULL,
  duration = 1.6,
  locale = "en-US",
  digits = 1,
  prefix = "",
  suffix = "",
  position = "top-right",
  labels = context_labels()
)

Arguments

map

A map created with mapgl::maplibre() or mapgl::mapboxgl().

layer_id

ID of a fill layer whose source is an sf object or inline GeoJSON.

column

Numeric column to sort by.

name_column

Column with the name shown under each shape.

order

"desc" (the default) puts the highest value first, "asc" the lowest.

n

Largest number of shapes in the grid, from 1 to 50. With more than 30, shapes become small and hard to recognize.

title

Optional title shown above the grid while it is visible.

duration

Length of the animation, in seconds.

locale, digits, prefix, suffix

Number formatting for the values, as in add_view_summary().

position

Corner for the button: one of "top-right" (the default), "top-left", "bottom-right" or "bottom-left".

labels

Texts, as returned by context_labels(): morph_sort and morph_back for the button, morph_highest and morph_lowest for the line under the title.

Details

While the shapes are out of the map, panning and zooming are paused, and the grid scrolls with the mouse wheel or a finger when it is taller than the map. On a globe the map turns flat during the animation and back to a globe at the end. Features without a value in column are left out of the grid. For small islands far from the main part of a feature (more than about twice its size away), the grid shows the main part only.

The animation is meant for a few shapes, such as the provinces of a country. When the layer has more than n features, only the first n (the highest, or the lowest with order = "asc") move to the grid, and the rest fade under it; a line under the title tells readers, such as "The 30 highest of 528". To show them all, group the features first, such as departments into provinces. Readers who prefer reduced motion (an operating system setting) see the result without the animation.

Value

The modified map object.

Examples

library(mapgl)

maplibre(center = c(137, 37), zoom = 4.2) |>
  add_fill_layer(
    id = "prefectures",
    source = jp_prefectures,
    fill_color = interpolate(
      column = "change",
      values = c(-7, 0, 1),
      stops = c("#b2182b", "#f7f7f7", "#2166ac")
    )
  ) |>
  add_rank_morph(
    layer_id = "prefectures",
    column = "change",
    name_column = "prefecture",
    suffix = " %",
    title = "Population change, 2020-2024"
  )

Add a ranking of the features in view

Description

Adds a panel listing the features in view with the highest (or lowest) values, updated as the map is panned and zoomed. Each entry shows the value, a bar on the scale of the whole layer and the feature's rank among all features; clicking on a name moves the map to it.

Usage

add_view_ranking(
  map,
  layer_id,
  column,
  name_column,
  label = column,
  n = 5,
  order = c("desc", "asc"),
  title = NULL,
  unit = "features",
  locale = "en-US",
  digits = 1,
  prefix = "",
  suffix = "",
  position = "top-right",
  width = 240,
  color = NULL,
  highlight_color = "#1f2328",
  fade = c("none", "moving"),
  collapsible = TRUE,
  collapsed = NULL,
  labels = context_labels()
)

Arguments

map

A map created with mapgl::maplibre() or mapgl::mapboxgl().

layer_id

ID of the layer to summarize: a polygon layer (such as mapgl::add_fill_layer()) or a point layer (such as mapgl::add_circle_layer() or mapgl::add_symbol_layer()). Its source must be an sf object or inline GeoJSON added in R; vector tile sources are not supported.

column

Name of the numeric column to rank by.

name_column

Column with the feature names to list.

label

Name of the variable shown in the panel. Defaults to column.

n

Number of features to list.

order

"desc" (the default) lists the highest values first; "asc", the lowest.

title

Title of the panel. NULL (the default) uses the ranking_highest or ranking_lowest label.

unit

Name for the features, shown as "14 of 528 unit".

locale

Locale for number formatting, such as "en-US" or "es-AR".

digits

Number of decimals for values. Values of a million or more are abbreviated, such as ⁠12.4M⁠.

prefix, suffix

Text added before and after each value, such as "$" or "%".

position

Corner for the panel: one of "top-left", "top-right", "bottom-left" or "bottom-right".

width

Width of the panel in pixels.

color

Color for the bars. NULL (the default) uses the layer's own color scale when it is an interpolate() or step_expr() on column, and blue otherwise.

highlight_color

Color of the outline drawn around a feature when hovering over its name.

fade

"moving" fades the panel while the map is panned or zoomed, so the map shows through, and brings it back when the map stops. "none" (the default) keeps it always solid.

collapsible

If TRUE, adds a button to collapse the panel down to its title and the first name.

collapsed

Whether the panel starts collapsed. NULL (the default) starts collapsed on maps narrower than 480 pixels, such as on phones.

labels

Panel texts, as returned by context_labels().

Details

Which features count as "in view" follows the same rules as add_view_summary().

Value

The modified map object.

Examples

library(mapgl)

maplibre(center = c(-64, -38.5), zoom = 3.4) |>
  add_fill_layer(
    id = "deptos",
    source = ar_departments,
    fill_color = interpolate(
      column = "growth",
      values = c(-10, 0, 10),
      stops = c("#b2182b", "#f7f7f7", "#2166ac"),
      na_color = "#d9d9d9"
    )
  ) |>
  add_view_ranking(
    layer_id = "deptos",
    column = "growth",
    name_column = "department",
    label = "Population growth 2022-2035",
    unit = "departments",
    suffix = "%"
  )

Add an "in this view" summary panel to a map

Description

Adds a panel that summarizes the features of a layer that are currently visible and compares them with the whole layer: how many are in view, their average (or median, or total), the highest and lowest values, and a histogram of the visible values over the distribution of all values. The panel updates as the map is panned and zoomed.

Usage

add_view_summary(
  map,
  layer_id,
  column,
  label = column,
  name_column = NULL,
  stat = c("mean", "median", "sum"),
  weight = NULL,
  weight_label = weight,
  title = NULL,
  unit = "features",
  locale = "en-US",
  digits = 1,
  prefix = "",
  suffix = "",
  diff_suffix = NULL,
  position = "top-left",
  width = 260,
  color = NULL,
  highlight_color = "#1f2328",
  bins = 20,
  chart = c("histogram", "position", "strip", "split", "none"),
  style = c("full", "compact", "mini"),
  show_area = NULL,
  area_unit = c("km2", "mi2"),
  fade = c("none", "moving"),
  collapsible = TRUE,
  collapsed = NULL,
  references = NULL,
  labels = context_labels(),
  compact = NULL
)

Arguments

map

A map created with mapgl::maplibre() or mapgl::mapboxgl().

layer_id

ID of the layer to summarize: a polygon layer (such as mapgl::add_fill_layer()) or a point layer (such as mapgl::add_circle_layer() or mapgl::add_symbol_layer()). Its source must be an sf object or inline GeoJSON added in R; vector tile sources are not supported.

column

Name of the numeric column to summarize.

label

Name of the variable shown in the panel. Defaults to column.

name_column

Optional column with feature names, used to report the highest and lowest features in view.

stat

Statistic shown for the features in view: "mean" (the default), "median" or "sum". For "sum" the panel shows which share of the overall total is in view, which suits absolute values such as population or sales.

weight

Optional numeric column used to weight the mean or median, for example population. When given, the panel also shows the share of the total weight that is in view. It is not used by stat = "sum".

weight_label

Name of the weight shown in the panel. Defaults to weight.

title

Title of the panel. NULL (the default) uses the title label, "In this view".

unit

Name for the features, shown as "14 of 528 unit".

locale

Locale for number formatting, such as "en-US" or "es-AR".

digits

Number of decimals for values. Values of a million or more are abbreviated, such as ⁠12.4M⁠.

prefix, suffix

Text added before and after each value, such as "$" or "%".

diff_suffix

Text added after the difference between the view and the overall average. NULL (the default) uses suffix, except when suffix contains "%": a difference between percentages is in percentage points, so the points label is used instead.

position

Corner for the panel: one of "top-left", "top-right", "bottom-left" or "bottom-right".

width

Width of the panel in pixels.

color

Color for the histogram bars of the features in view. NULL (the default) uses the layer's own color scale when it is an interpolate() or step_expr() on column, and blue otherwise.

highlight_color

Color of the outline drawn around features when hovering over the histogram or the highest and lowest names.

bins

Number of histogram bins.

chart

Chart at the bottom of the "full" style:

  • "histogram" (the default): the values in view over the distribution of all values.

  • "position": a bar spanning all values, in the map's colors, with the value in view and the overall value marked on it. With stat = "sum", the share of the total that is in view.

  • "strip": one dot per feature along the scale of values, those in view in the map's colors over all the others in gray. Above 2,000 features, an even sample of 2,000 is drawn.

  • "split": two histograms on the same scale, one above the other: the features in view, in the map's colors, and all features, in gray.

  • "none": no chart.

style

How much the panel shows:

  • "full" (the default): count, value, comparison, highest and lowest, and histogram.

  • "compact": only the count, the value and the comparison.

  • "mini": three figures side by side: features in view, the value and the area of the view.

show_area

Whether to show the area of the Earth covered by the screen. NULL (the default) shows it in the "mini" style only. It is shown as a dash when part of the screen is not on the Earth: when the globe does not fill the screen, or the map is tilted towards the horizon.

area_unit

Unit for the area: "km2" (the default) or "mi2".

fade

"moving" fades the panel while the map is panned or zoomed, so the map shows through, and brings it back when the map stops. "none" (the default) keeps it always solid.

collapsible

If TRUE, adds a button to collapse the panel down to its title and value.

collapsed

Whether the panel starts collapsed. NULL (the default) starts collapsed on maps narrower than 480 pixels, such as on phones.

references

Up to three other values to compare the view with, besides the whole map, as a named list. Each one is either a single number, such as a target, or a reference_group() of features, whose value is computed with the same stat and weight as the panel. For example, list("CABA" = reference_group("province", "Ciudad Autonoma de Buenos Aires"), "Target" = 2).

labels

Panel texts, as returned by context_labels().

compact

Deprecated: compact = TRUE is the same as style = "compact".

Details

The panel is also a way into the map: hovering over a histogram bar outlines the features in that range, and clicking on the name of the highest or lowest feature moves the map to it. When the layer is colored with mapgl::interpolate() or mapgl::step_expr() on the same column, the histogram uses the same colors, so it doubles as a legend.

All the work happens in the browser, so the panel works in static HTML, R Markdown and Quarto documents without Shiny.

A feature counts as "in view" when any part of it is on screen: for points, when the point is on screen. Features outside the layer's zoom range are not counted, and neither are features hidden by the layer's filter (in that case only features drawn on screen are counted, so very small features may be missed at low zoom).

On a clustered layer (cluster_options in mapgl::add_circle_layer()), every point in view is counted, whether it is drawn on its own or inside a cluster. Use the ID of the layer itself, not of its -clusters layer.

Value

The modified map object.

Examples

library(mapgl)

maplibre(center = c(-64, -38.5), zoom = 3.4) |>
  add_fill_layer(
    id = "deptos",
    source = ar_departments,
    fill_color = interpolate(
      column = "growth",
      values = c(-10, 0, 10),
      stops = c("#b2182b", "#f7f7f7", "#2166ac"),
      na_color = "#d9d9d9"
    ),
    fill_opacity = 0.8
  ) |>
  add_view_summary(
    layer_id = "deptos",
    column = "growth",
    label = "Population growth 2022-2035",
    name_column = "department",
    weight = "pop_2022",
    weight_label = "population",
    unit = "departments",
    suffix = "%"
  )

Argentine departments with projected population growth

Description

The 529 departments (departamentos and partidos) of Argentina with their estimated population for 2022 and 2035 and the projected growth between both years. Department and province names are in Spanish, as in the official sources.

Usage

ar_departments

Format

An sf object with 529 rows and 7 columns:

id

Five-digit INDEC department code.

department

Department name.

province

Province name.

pop_2022

Estimated population, 2022.

pop_2035

Projected population, 2035.

growth

Projected population growth 2022-2035, in percent.

geometry

Department boundaries (WGS 84).

Source

The "Islas del Atlántico Sur" department (Islas Malvinas, Georgias del Sur, Sandwich del Sur and the surrounding islands), part of the province of Tierra del Fuego, has no population estimates: its pop_2022, pop_2035 and growth are NA. maplore's components leave it out of their figures. When coloring the map by one of these columns, give mapgl::interpolate() or mapgl::step_expr() an na_color, such as na_color = "#d9d9d9"; otherwise the map draws it in black.


Texts for the context components

Description

Returns the texts used by add_view_summary(), add_view_ranking(), add_context_tooltip(), add_feature_search(), add_map_notes() and add_rank_morph(). Start from English or Spanish and override any text through ....

Usage

context_labels(lang = c("en", "es"), ...)

view_summary_labels(lang = c("en", "es"), ...)

Arguments

lang

Base language: "en" (English) or "es" (Spanish).

...

Texts to override, by name. Common: of, above, below, same, points, empty, go_to, collapse, expand. View summary: title, whole_map, average, median, sum, overall, highest, lowest, share, view, all. Ranking: ranking_highest, ranking_lowest, in_view, rank_overall. Context tooltip: rank, higher_than, average_of_all. Search: search_placeholder, search_none, search_open. Area: area_zoom_in. References: ref_above, ref_below, ref_same. Map notes: notes_title, notes_source, notes_link. Rank morph: morph_sort, morph_back, morph_highest, morph_lowest ({n} and {total} are replaced by the numbers).

Details

view_summary_labels() is the same function under its earlier name.

Value

A list of class context_labels.

Examples

context_labels("es")
context_labels("en", title = "Visible now")

Earthquakes in southern South America

Description

The 2,569 earthquakes of magnitude 4.5 or more recorded between 2016 and 2025 in southern South America (latitudes 20°S to 56°S, longitudes 76°W to 52°W). Earthquakes get deeper from west to east, following the Nazca plate as it sinks under South America.

Usage

earthquakes

Format

An sf object with 2,569 points and 6 columns:

id

USGS event id.

date

Date (UTC).

magnitude

Magnitude.

depth

Depth in kilometers.

place

Description of the location, as given by the USGS.

geometry

Epicenter (WGS 84).

Source

U.S. Geological Survey (USGS) Earthquake Catalog, public domain, https://earthquake.usgs.gov/earthquakes/search/.


Five prefectures of Japan: a shrinking, aging population

Description

Five prefectures of Japan, from north to south (Hokkaido, Akita, Tokyo, Kyoto and Okinawa), with their population in 2020 (census) and 2024 (official estimate), the change between both years and the share of people aged 65 and over in 2024. Tokyo is the only one that grew; in Akita almost 40% of people are 65 or older.

Usage

jp_prefectures

Format

An sf object with 5 rows and 7 columns:

code

Official prefecture code (JIS X 0401).

prefecture

Prefecture name.

pop_2020

Population, 2020 census.

pop_2024

Population, 2024 estimate, rounded to thousands.

change

Population change 2020-2024, in percent.

aged_65

Share of the population aged 65 and over in 2024, in percent.

geometry

Prefecture boundaries (WGS 84).

Source


A group of features to compare the view with

Description

Defines a reference for the references argument of add_view_summary(): the features of the layer whose column takes one of values, such as all the departments of a province. Its value is computed with the same statistic and weight as the panel.

Usage

reference_group(column, values)

Arguments

column

Name of the column that defines the group.

values

Values of column that belong to the group.

Value

An object of class maplore_reference_group.

Examples

reference_group("province", "Ciudad Autónoma de Buenos Aires")
reference_group("region", c("Kanto", "Kansai"))