MyNixOS website logo
Description

Mongolian 'NSO' 'PXWeb' Data and Boundaries (Tidy Client).

A 'tidyverse'-friendly client for the National Statistics Office of Mongolia 'PXWeb' API <https://data.1212.mn/> with helpers to discover tables, variables, and fetch statistical data. Also includes utilities to retrieve Mongolia administrative boundaries (ADM0-ADM2) as 'sf' objects from open sources for mapping and spatial analysis.

mongolstats

R-CMD-check Lifecycle:experimental

mongolstats is your gateway to the National Statistics Office of Mongolia (NSO). Access official data, analyze economic trends, and map regional statistics—all from within R.

Why mongolstats?

  • Instant Access: Query thousands of official datasets directly from R.
  • Tidy Data: Analysis-ready tibble format compatible with dplyr and ggplot2.
  • Mapping Ready: Built-in administrative boundaries for effortless geospatial analysis.
  • Reliable: Smart caching and robust error handling for smooth workflows.

Installation

You can install mongolstats from CRAN with:

install.packages("mongolstats")

Or install the development version from GitHub with:

# install.packages("devtools")
devtools::install_github("temuulene/mongolstats")

Quick Start

1. The Economic Pulse: GDP Trends

Visualize Mongolia’s economic growth in seconds.

library(mongolstats)
library(dplyr)
library(ggplot2)

# Set language to English
nso_options(mongolstats.lang = "en")

# Fetch GDP data - using labels for clarity
gdp <- nso_data(
  tbl_id = "DT_NSO_0500_001V1",
  selections = list(
    "Indicator" = "GDP, at current prices",
    "Economic activity" = "Total",
    "Year" = c(
      "2010", "2011", "2012", "2013", "2014",
      "2015", "2016", "2017", "2018", "2019",
      "2020", "2021", "2022", "2023", "2024"
    )
  ),
  labels = "en" # Get English labels
)

# Visualize the GDP trend as a static plot
p <- gdp |>
  ggplot(aes(x = as.integer(Year_en), y = value / 1e6, group = 1)) +
  geom_area(fill = "#42b883", alpha = 0.6) +  # shaded area emphasizes cumulative growth
  geom_line(color = "#2c3e50", linewidth = 1.2) +
  geom_point(color = "#2c3e50", size = 3, shape = 21, fill = "white", stroke = 1.5) +
  scale_y_continuous(labels = scales::label_number(suffix = "T")) +  # "T" suffix for trillions
  scale_x_continuous(breaks = function(x) seq(ceiling(min(x)), floor(max(x)), by = 1)) +
  labs(
    title = "Mongolia's GDP Growth (2010-2024)",
    subtitle = "Gross Domestic Product (in Trillions MNT)",
    x = NULL,
    y = "GDP (Trillions MNT)",
    caption = "Source: NSO Mongolia via mongolstats"
  ) +
  theme_minimal(base_size = 12) +
  theme(
    plot.title = element_text(face = "bold", size = 16),
    plot.subtitle = element_text(color = "grey40"),
    panel.grid.minor = element_blank(),
    panel.grid.major.x = element_blank()  # vertical gridlines removed for cleaner look
  )

p  # print static ggplot

2. Mapping Regional Population

Discover how population is distributed across the country.

library(sf)

# 1. Fetch Population by Aimag
# Get all region codes first
regions <- nso_dim_values("DT_NSO_0300_002V1", "Region")$code

pop <- nso_data(
  tbl_id = "DT_NSO_0300_002V1",
  selections = list(
    "Region" = regions,
    "Year" = "2024" # Use the year label
  ),
  labels = "en" # Get English labels for joining
) |>
  filter(!Region %in% c("0", "1", "2", "3", "4", "511")) |> # Exclude Total, Regions, and duplicate UB
  mutate(
    Region_en = trimws(Region_en),
    Region_en = dplyr::case_match(
      Region_en,
      "Bayan-Ulgii" ~ "Bayan-Ölgii",
      "Uvurkhangai" ~ "Övörkhangai",
      "Khuvsgul" ~ "Hovsgel",
      "Umnugovi" ~ "Ömnögovi",
      "Tuv" ~ "Töv",
      "Sukhbaatar" ~ "Sükhbaatar",
      .default = Region_en
    )
  )

# 2. Get Administrative Boundaries
map <- mn_boundaries(level = "ADM1")

# 3. Join and Map
pop_map <- map |>
  left_join(pop, by = c("shapeName" = "Region_en"))

p <- ggplot(pop_map) +
  geom_sf(aes(fill = value), color = "white", size = 0.2) +
  # Log scale because population spans 3 orders of magnitude (20k to 1.5M)
  scale_fill_viridis_c(
    option = "magma",
    direction = -1,
    trans = "log10",
    breaks = c(20000, 50000, 100000, 500000, 1500000),
    labels = scales::label_number(scale_cut = scales::cut_short_scale()),
    name = "Population\n(Log Scale)"
  ) +
  labs(
    title = "Population Distribution (2024)",
    subtitle = "Mid-year resident population by Aimag (Log Scale)",
    caption = "Source: NSO Mongolia"
  ) +
  theme_void() +
  theme(
    plot.title = element_text(face = "bold", size = 16),
    plot.subtitle = element_text(color = "grey40"),
    legend.position = "bottom",
    legend.key.width = unit(1.5, "cm")
  )

p  # print static ggplot

Documentation

Full documentation is available at temuulene.github.io/mongolstats.

Contributing

We welcome contributions! Please see the Contributing Guidelines for details.

License

MIT.

Metadata

Version

0.1.1

License

Unknown

Platforms (78)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    uefi
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-uefi
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-linux
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-uefi
  • x86_64-windows