tmap_mode("view")
# hover is on by default when id is set — shows country name on hover
tm_shape(World) +
tm_polygons(fill = "HPI", id = "name")Session 6: Interactive features (tooltips and popups)
In view mode, map objects can respond to user interaction:
These are set via arguments in the layer functions (tm_polygons, tm_symbols, etc.).
hoverBy default, hover is disabled — unless id is set in the layer, in which case the id value is shown automatically:
To disable hover even when id is set:
To show a different column than id on hover, pass a column name explicitly:
idThe id argument in a layer function sets the column used to identify features:
popuppopup = TRUE → default popup, show all variablespopup = FALSE → disable popupsUse a named character vector to display friendly labels:
tm_label_format()tm_label_format() controls how numeric values are formatted — in legend labels and in popups.
It is passed to:
label.format argument of scale functions (tm_scale_intervals(), tm_scale_continuous(), …)format argument of tm_popup()| Argument | Default | Effect |
|---|---|---|
digits |
NA (auto) |
Number of decimal places |
scientific |
FALSE |
Use scientific notation |
big.num.abbr |
c(mln=6, bln=9) |
Abbreviate millions/billions |
prefix |
"" |
Text before number |
suffix |
"" |
Text after number |
text.separator |
"to" |
Between interval bounds |
text.less.than |
"Less than" |
For lowest open interval |
text.or.more |
"or more" |
For highest open interval |
interval.disjoint |
TRUE |
Show disjoint intervals (0–999, 1000–1999) |
html.escape |
TRUE |
Escape HTML in popups; set FALSE to allow HTML |
label.format — prefix and separatorlabel.format — abbreviating large numberspopup.format — per-column formatting in view modetmap_mode("view")
tm_shape(World) +
tm_polygons(
fill = "HPI",
popup.vars = c("name", "HPI", "gdp_cap_est", "pop_est"),
popup.format = list(
HPI = tm_label_format(digits = 1),
gdp_cap_est = tm_label_format(prefix = "$", big.num.abbr = c(k = 3)),
pop_est = tm_label_format(
fun = function(x) paste("About", round(x/1e6), "million"))
)
)Setting html.escape = FALSE allows raw HTML in popup values — for example, rendering numbers in italics:
id = "column" → identifies features; hover is enabled automatically when id is set; use hover = FALSE to disable it, or hover = "column" to show a different columnpopup = tm_popup(...) → popup on clicktm_label_format() controls number formatting in both legend labels and popupsdigits, prefix, suffix, big.num.abbr, text.separator, interval.disjoint, html.escapelabel.format → used inside tm_scale_*() for legend labelsformat → used inside tm_popup() for popup valuesid is covered in Session 11