| 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 |
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.
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() )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() )
map |
A map created with |
layer_id |
ID of the layer to summarize: a polygon layer (such as
|
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
|
weight |
Optional numeric column used to weight the average, for example population. |
unit |
Name for the features, shown as "14 of 528 |
locale |
Locale for number formatting, such as |
digits |
Number of decimals for values. Values of a million or more
are abbreviated, such as |
prefix, suffix
|
Text added before and after each value, such as
|
diff_suffix |
Text added after the difference between the view and
the overall average. |
color |
Color for the feature's marker on the distribution strip.
|
highlight |
If |
highlight_color |
Color of that outline. |
labels |
Panel texts, as returned by |
Use it instead of the layer's own tooltip argument: with both, two
tooltips are shown.
The modified map object.
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 = "%" )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 = "%" )
Adds a search box that suggests features of a layer as you type, matching
a column such as a name. Choosing a suggestion moves the map to the
feature and outlines it. Matching ignores case and accents, so "cordoba"
finds "Córdoba". Unlike mapgl::add_geocoder_control(), which searches
places around the world, this searches your data.
add_feature_search( map, layer_id, search_column, detail_column = NULL, placeholder = NULL, collapsed = FALSE, max_results = 8, locale = "en-US", position = "top-right", width = 240, highlight_color = "#1f2328", labels = context_labels() )add_feature_search( map, layer_id, search_column, detail_column = NULL, placeholder = NULL, collapsed = FALSE, max_results = 8, locale = "en-US", position = "top-right", width = 240, highlight_color = "#1f2328", labels = context_labels() )
map |
A map created with |
layer_id |
ID of the layer to search. Its source must be an |
search_column |
Column to search, such as the feature names. |
detail_column |
Optional column shown next to each suggestion to tell apart features with the same name, such as the province. |
placeholder |
Text shown in the empty search box. |
collapsed |
If |
max_results |
Maximum number of suggestions. |
locale |
Locale used to sort the suggestions alphabetically. |
position |
Corner for the search box: one of |
width |
Width of the search box in pixels. |
highlight_color |
Color of the outline drawn around the chosen feature. |
labels |
Panel texts, as returned by |
The search box works with the keyboard: arrow keys move through the suggestions, Enter chooses one and Escape closes them.
The modified map object.
library(mapgl) maplibre(center = c(-64, -38.5), zoom = 3.4) |> add_fill_layer(id = "deptos", source = ar_departments, fill_color = "#9ecae1") |> add_feature_search( layer_id = "deptos", search_column = "department", detail_column = "province" )library(mapgl) maplibre(center = c(-64, -38.5), zoom = 3.4) |> add_fill_layer(id = "deptos", source = ar_departments, fill_color = "#9ecae1") |> add_feature_search( layer_id = "deptos", search_column = "department", detail_column = "province" )
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.
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() )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() )
map |
A map created with |
source |
Where the data come from, such as |
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 |
link_label |
Text of the link. |
title |
Title of the panel. |
position |
Corner for the notes: one of |
width |
Width of the open panel in pixels. |
collapsed |
If |
fade |
|
labels |
Panel texts, as returned by |
Texts are shown as plain text, never as HTML.
The modified map object.
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" )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" )
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.
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() )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() )
map |
A map created with |
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 |
|
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 |
position |
Corner for the button: one of |
labels |
Texts, as returned by |
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.
The modified map object.
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" )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" )
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.
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() )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() )
map |
A map created with |
layer_id |
ID of the layer to summarize: a polygon layer (such as
|
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
|
n |
Number of features to list. |
order |
|
title |
Title of the panel. |
unit |
Name for the features, shown as "14 of 528 |
locale |
Locale for number formatting, such as |
digits |
Number of decimals for values. Values of a million or more
are abbreviated, such as |
prefix, suffix
|
Text added before and after each value, such as
|
position |
Corner for the panel: one of |
width |
Width of the panel in pixels. |
color |
Color for the bars. |
highlight_color |
Color of the outline drawn around a feature when hovering over its name. |
fade |
|
collapsible |
If |
collapsed |
Whether the panel starts collapsed. |
labels |
Panel texts, as returned by |
Which features count as "in view" follows the same rules as
add_view_summary().
The modified map object.
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 = "%" )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 = "%" )
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.
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 )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 )
map |
A map created with |
layer_id |
ID of the layer to summarize: a polygon layer (such as
|
column |
Name of the numeric column to summarize. |
label |
Name of the variable shown in the panel. Defaults to
|
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: |
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 |
weight_label |
Name of the weight shown in the panel. Defaults to
|
title |
Title of the panel. |
unit |
Name for the features, shown as "14 of 528 |
locale |
Locale for number formatting, such as |
digits |
Number of decimals for values. Values of a million or more
are abbreviated, such as |
prefix, suffix
|
Text added before and after each value, such as
|
diff_suffix |
Text added after the difference between the view and
the overall average. |
position |
Corner for the panel: one of |
width |
Width of the panel in pixels. |
color |
Color for the histogram bars of the features in view. |
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
|
style |
How much the panel shows:
|
show_area |
Whether to show the area of the Earth covered by the
screen. |
area_unit |
Unit for the area: |
fade |
|
collapsible |
If |
collapsed |
Whether the panel starts collapsed. |
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 |
labels |
Panel texts, as returned by |
compact |
Deprecated: |
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.
The modified map object.
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 = "%" )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 = "%" )
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.
ar_departmentsar_departments
An sf object with 529 rows and 7 columns:
Five-digit INDEC department code.
Department name.
Province name.
Estimated population, 2022.
Projected population, 2035.
Projected population growth 2022-2035, in percent.
Department boundaries (WGS 84).
Geometries: Instituto Geográfico Nacional, via the Georef service of datos.gob.ar (CC BY 4.0), https://infra.datos.gob.ar/georef/departamentos.geojson.
Population: INDEC, Estimaciones de población por sexo, departamento y año calendario 2022-2035, https://www.indec.gob.ar/ftp/cuadros/poblacion/base_estimaciones_pob_deptos_2022_2035.csv.
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.
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 ....
context_labels(lang = c("en", "es"), ...) view_summary_labels(lang = c("en", "es"), ...)context_labels(lang = c("en", "es"), ...) view_summary_labels(lang = c("en", "es"), ...)
lang |
Base language: |
... |
Texts to override, by name. Common: |
view_summary_labels() is the same function under its earlier name.
A list of class context_labels.
context_labels("es") context_labels("en", title = "Visible now")context_labels("es") context_labels("en", title = "Visible now")
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.
earthquakesearthquakes
An sf object with 2,569 points and 6 columns:
USGS event id.
Date (UTC).
Magnitude.
Depth in kilometers.
Description of the location, as given by the USGS.
Epicenter (WGS 84).
U.S. Geological Survey (USGS) Earthquake Catalog, public domain, https://earthquake.usgs.gov/earthquakes/search/.
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.
jp_prefecturesjp_prefectures
An sf object with 5 rows and 7 columns:
Official prefecture code (JIS X 0401).
Prefecture name.
Population, 2020 census.
Population, 2024 estimate, rounded to thousands.
Population change 2020-2024, in percent.
Share of the population aged 65 and over in 2024, in percent.
Prefecture boundaries (WGS 84).
Population: Statistics Bureau of Japan, Japan Statistical Yearbook 2026, tables 2-3 and 2-7, https://www.stat.go.jp/english/data/nenkan/. Created by editing those tables. Terms of use compatible with CC BY 4.0: https://www.e-stat.go.jp/en/terms-of-use.
Boundaries: Natural Earth 1:10m Admin 1 states and provinces, public domain, https://www.naturalearthdata.com/.
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.
reference_group(column, values)reference_group(column, values)
column |
Name of the column that defines the group. |
values |
Values of |
An object of class maplore_reference_group.
reference_group("province", "Ciudad Autónoma de Buenos Aires") reference_group("region", c("Kanto", "Kansai"))reference_group("province", "Ciudad Autónoma de Buenos Aires") reference_group("region", c("Kanto", "Kansai"))