mn_join: Join your data to a map of Mongolia

View source: R/join.R

mn_joinR Documentation

Join your data to a map of Mongolia

Description

Attaches a data frame to boundaries in one step. The column in by can hold names in any common spelling, Cyrillic names, NSO codes, ISO codes or P-codes; they are matched with mn_match(). Every unit of the level is kept, so places without data still appear on the map (with NA).

Usage

mn_join(
  data,
  by,
  level = NULL,
  by_parent = NULL,
  within = NULL,
  drop_other_levels = TRUE,
  resolution = c("low", "high"),
  lang = NULL,
  crs = NULL
)

Arguments

data

A data frame.

by

The column of data with place names or codes, as a string or a bare column name.

level

The level of the places in by. NULL detects it.

by_parent

Optional column of data naming each row's parent (for example the aimag of each soum). Needed when soum or bag names repeat across the country.

within

Keep only units inside these places, or these places themselves: names or codes of any level, such as "Khovd", "MN84" or "Western region".

drop_other_levels

If TRUE (default), rows for units at other levels, such as national or aimag totals in a soum table, are dropped with a single message. If FALSE they are reported as unmatched.

resolution

"low" (default) uses simplified boundaries that ship with the package and suit most maps. "high" uses full-resolution boundaries, downloaded once (about 10 MB) and cached; see mn_cache_dir().

lang

Language of the name column: "en" (English, as in NSO tables), "mn" (Cyrillic) or "mns" (Latin with diacritics, MNS 5217). Defaults to getOption("mongolmaps.lang", "en").

crs

Coordinate reference system of the result. NULL (default) keeps longitude/latitude (EPSG:4326). Use "albers", "lcc" or "utm" for a projection suited to Mongolia (see mn_crs()), or any value accepted by sf::st_crs().

Details

Level. When level is NULL, the level with the most matches is used. NSO tables often list several levels in one column (the national total, regions, aimags, soums ...); rows at other levels are dropped with a message (see drop_other_levels). Join NSO tables by their code column (such as Region) rather than the label column: labels such as "Ulaanbaatar" name both a region and an aimag.

Several rows per unit. Data with several rows per place (for example one per year) give several copies of that place's polygon, ready for ggplot2::facet_wrap().

Places without boundaries. Villages (tosgon) and rural bags have NSO codes but no boundary; their rows are reported and left out.

Value

An sf tibble: the boundaries of every unit at the level, with the columns of data added. Columns of data whose names clash with boundary columns get the suffix .data.

See Also

Other joining data: mn_example_population

Examples

# Mid-year population from NSO, by aimag and year
pop <- mn_example_population[mn_example_population$Year == 2025, ]
mn_join(pop, by = "Region", level = "aimag")

# Khoroos of Ulaanbaatar, from the same table
mn_join(pop, by = "Region", level = "bag", within = "Ulaanbaatar")

# Names in any spelling work
df <- data.frame(aimag = c("Khovsgol", "Hovd", "\\u0423\\u0432\\u0441"), value = 1:3)
mn_join(df, aimag)

mongolmaps documentation built on Oct. 10, 2026, 5:08 p.m.