library(rtemis.draw)
Attaching package: 'rtemis.draw'
The following object is masked from 'package:graphics':
Axis
Attaching package: 'rtemis.draw'
The following object is masked from 'package:graphics':
Axis
Choropleth maps — regions shaded by a value — are rendered with MapLibre GL, a WebGL map renderer, rather than ECharts. As with the Sigma.js network renderer, rtemis.draw picks the most capable JavaScript library per plot type. The high-level draw_choropleth() and the low-level MapModel / MapLibreOption classes are the R counterparts of the choropleth renderer in rtemis.live.
Note on rendering There is no basemap: administrative boundaries are drawn on the themed page background, and the map auto-detects your system’s light/dark setting like every other draw_* function. The location-to-geometry join, the classification scale, and FIPS/ISO key normalization all run in the browser. A small report in the corner shows how many rows matched the geometry — and surfaces any keys that did not, so a silent join failure is always visible.
The simplest input is a data frame with a location column and a value column. For world maps the location key is an ISO-A3 (or ISO-A2) country code; resolution = "country" is the default.
countries <- data.frame(
iso = c(
"USA", "CAN", "MEX", "BRA", "ARG", "GBR", "FRA", "DEU", "ESP", "ITA",
"NOR", "SWE", "POL", "RUS", "CHN", "JPN", "KOR", "IND", "IDN", "AUS",
"NZL", "ZAF", "EGY", "NGA", "KEN", "SAU", "TUR", "IRN", "CHL", "COL"
),
life_exp = c(
78.5, 82.3, 75.0, 75.9, 76.7, 81.3, 82.5, 81.0, 83.5, 82.9,
82.8, 82.8, 78.3, 73.2, 78.2, 84.6, 83.4, 70.2, 71.7, 83.4,
82.1, 64.9, 71.8, 55.7, 66.7, 75.1, 77.7, 76.7, 80.2, 77.3
)
)
draw_choropleth(
countries,
location = "iso",
value = "life_exp",
value_label = "Life expectancy"
)The renderer accepts ISO-A3, ISO-A2, or upper/lower case, so a column of two-letter codes joins just as well.
Set resolution = "state" to join to US states. The location key can be a FIPS code, a postal abbreviation, or a full state name — all normalize to the same 2-digit FIPS id. Here we use the built-in state.x77 data:
classification controls how values are binned into color classes: "quantile" (equal counts, the default), "equal" (equal-width intervals), or "jenks" (natural breaks). num_classes sets the number of classes (2–12), and colormap selects the ramp.
Sequential schemes — "blues", "viridis", "ylorrd", "greens", "magma" — suit data with a natural low-to-high order. Diverging schemes — "rdbu", "rdylgn", "spectral", "brbg" — emphasize departure from a midpoint:
Pass tooltip with extra column names to surface them on hover. The remaining arguments tune appearance: opacity, show_boundaries, outline_width, and the corner anchors legend_position / tooltip_position.
states_full <- data.frame(
abbr = state.abb,
name = state.name,
income = as.numeric(state.x77[, "Income"]),
murder = as.numeric(state.x77[, "Murder"])
)
draw_choropleth(
states_full,
location = "abbr",
value = "income",
resolution = "state",
value_label = "Income",
tooltip = c("name", "murder"),
colormap = "blues",
outline_width = 0.5,
legend_position = "bottom-left",
tooltip_position = "top-right"
)The full set of appearance arguments:
opacity — fill opacity, 0-1.show_boundaries, outline_width — region outlines.show_legend — hide the color legend entirely.legend_position, tooltip_position, report_position — corner anchors, each one of "top-left", "top-right", "bottom-left", "bottom-right".Set resolution = "county" to join on 5-digit FIPS. As an example we use the CDC PLACES county dataset — the same sample data that ships with rtemis.live — read straight from the web:
One row per county, keyed by fips, with five health indicators: smoking, obesity, physical_inactivity, high_bp, and no_insurance. We color by smoking and put the rest in the tooltip. County boundaries are dense, so a thinner outline_width keeps them from swallowing the fill:
draw_choropleth(
places,
location = "fips",
value = "smoking",
resolution = "county",
value_label = "Smoking (%)",
tooltip = c(
"county", "obesity", "physical_inactivity", "high_bp", "no_insurance"
),
colormap = "magma",
opacity = 0.9,
show_boundaries = TRUE,
outline_width = 0.15,
legend_position = "bottom-right",
report_position = "bottom-left"
)The join report in the corner reads 2,945 matched · 12 unmatched: the dataset uses Connecticut’s new planning regions and the two boroughs that replaced Valdez-Cordova, none of which exist in the vendored county geometry, plus one national summary row. Kentucky and Pennsylvania are missing from the data altogether, so they render in the “No data” color rather than silently disappearing.
draw_choropleth() builds a MapModel and a MapLibreOption render spec internally. To assemble rows by hand or set the render spec directly and draw it with draw(), see the MapLibre low-level S7 API chapter.