Skip to main content

21  Choropleth maps

library(rtemis.draw)

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 renders each plot type with a JavaScript library chosen for it. 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 follows the system’s light/dark setting like every other draw_* function. The location-to-geometry join, the classification scale, and FIPS/ISO key normalization run in the browser through the functions rtemis.live also uses, and SVG export runs the same functions. aggregate ("sum", "mean", or "median") combines the rows naming one region, such as the counties of a state; with the default "none", two rows naming one region stop the map with an error naming both. Unmatched location keys are reported in the browser console. Regions without data remain visible in a neutral color.

21.1 Countries

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 the same way.

21.2 US states

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. The example uses the built-in state.x77 data:

states <- data.frame(
  abbr = state.abb,
  income = as.numeric(state.x77[, "Income"])
)

draw_choropleth(
  states,
  location = "abbr",
  value = "income",
  resolution = "state",
  value_label = "Income (1974, $)",
  colormap = "greens"
)

21.3 Classification and color schemes

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. The classes cover the values of the regions the rows name, and tied values can leave fewer classes. The legend writes each class’s bounds to three significant digits.

draw_choropleth(
  states,
  location = "abbr",
  value = "income",
  resolution = "state",
  value_label = "Income",
  classification = "jenks",
  num_classes = 6,
  colormap = "viridis"
)

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:

state_pop <- data.frame(
  abbr = state.abb,
  illiteracy = as.numeric(state.x77[, "Illiteracy"])
)

draw_choropleth(
  state_pop,
  location = "abbr",
  value = "illiteracy",
  resolution = "state",
  value_label = "Illiteracy (%)",
  colormap = "rdylgn",
  classification = "equal"
)

21.4 Tooltips and styling

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 — corner anchors, each one of "top-left", "top-right", "bottom-left", "bottom-right".

21.5 US counties

Set resolution = "county" to join on 5-digit FIPS. The example uses the CDC PLACES county dataset — the same sample data that ships with rtemis.live — read straight from the web:

places <- read.csv(
  "https://live.rtemis.org/sample/places_county.csv",
  colClasses = c(fips = "character")
)

One row per county, keyed by fips, with five health indicators: smoking, obesity, physical_inactivity, high_bp, and no_insurance. The map colors counties by smoking and shows 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"
)

The county geometry and dataset may use different boundary vintages. Keys without a matching region are reported in the browser console; regions without a value use the “No data” color.

21.6 Export a map

Save a choropleth as SVG with editable boundaries, colors, legend, and text:

state_map <- draw_choropleth(
  states, "abbr", "income", resolution = "state",
  value_label = "Income (1974, $)", colormap = "greens"
)
save_drawing(state_map, file.path(tempdir(), "state-income.svg"),
             width = 1100, height = 700)

This requires Node.js and exports the initial Mercator view. Browser panning and zooming do not change the saved R object.

21.7 Lower-level API

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 MapLibre option classes.

© 2026 E.D. Gennatas