Introduction of geo-boundaries | R bloggers

Introduction of geo-boundaries | R bloggers

3 minutes
If you’ve ever worked with spatial data in R, this might ring a bell…

  • Look for boundary data
  • Find out which version is “official”.
  • Download a shape file
  • Unpack it
  • Load it
  • Correct projections
  • Repeat

While searching for new data sources I found the excellent one
geoborders database. However, accessing the data can be tedious because it is provided as compressed shapefiles, and as any GIS professional knows: shapefiles should die!

In the past the geoborders package was on CRAN, but was archived. So I decided to make my own version, and geoborders was born.

It connects directly to the excellent
geoborders database and returns clean, ready to use sf objects with a single function call. No manual downloads. No shapefile clutter.

This is how it works.

Installation

geoborders was recently adopted
CRANEso just install it with:

install.packages("geobounds")

Load the package and other additional packages:

library(geobounds)
library(sf)
library(ggplot2)
library(dplyr)

Obtain Management Levels (ADM)

Administrative level 0 (ADM0) corresponds to countries:

# Panama

gb_get_adm0(country = "Panama") |>
  ggplot() +
  geom_sf(fill = "#072357") +
  labs(caption = "Source: www.geoboundaries.org")

You can also request multiple management levels at the same time. For example:

# Simplified files
gb_get(country = "Panama", adm_lvl = "all", simplified = TRUE) |>
  ggplot() +
  geom_sf(aes(fill = shapeType), color = "grey50", linewidth = 0.1) +
  facet_wrap(vars(shapeType)) +
  scale_fill_viridis_d() +
  labs(
    title = "Administrative levels of Panama",
    fill = "level",
    caption = "Source: www.geoboundaries.org"
  )

Global Composite Borders (CGAZ)

When you download individual country files, each country reflects its own view of borders. This results in: – Overlapping borders – Geographic gaps – Disputed territories

For clear global visualizations, geoBoundaries offers a Composite Global Administrative Zones (CGAZ) dataset that can be accessed with gb_get_world().

Here’s an example with country-level files:

# Using individual (gb_get_adm) shapefiles
gb_get_adm0(country = c("India", "Pakistan")) |>
  # Disputed area: Kashmir
  ggplot() +
  geom_sf(aes(fill = shapeName), alpha = 0.5) +
  scale_fill_manual(values = c("#FF671F", "#00401A")) +
  labs(
    fill = "Country",
    title = "Map of India & Pakistan",
    subtitle = "Note overlapping in Kashmir region",
    caption = "Source: www.geoboundaries.org"
  )

And here is the same comparison with CGAZ gb_get_world():

gb_get_world(c("India", "Pakistan")) |>
  ggplot() +
  geom_sf(aes(fill = shapeName), alpha = 0.5) +
  scale_fill_manual(values = c("#FF671F", "#00401A")) +
  labs(
    fill = "Country",
    title = "Map of India & Pakistan",
    subtitle = "CGAZ does not overlap",
    caption = "Source: www.geoboundaries.org"
  )

Understanding the data

The geoBoundaries database undergoes strict quality assurance, including manual review and manual digitization of physical maps. This guarantees the highest level of spatial accuracy for scientific and academic research.

This accuracy comes at a cost: some files may be large and may take longer to download. For visualization and general mapping, we recommend using simplified datasets per setting simplified = TRUE.

# Different resolutions
norway <- gb_get_adm0("NOR") |>
  mutate(res = "Full resolution")
print(object.size(norway), units = "Mb")
#> 26.5 Mb

norway_simp <- gb_get_adm0(country = "NOR", simplified = TRUE) |>
  mutate(res = "Simplified")
print(object.size(norway_simp), units = "Mb")
#> 1.5 Mb

norway_all <- bind_rows(norway, norway_simp)

# Plot ggplot2
ggplot(norway_all) +
  geom_sf(fill = "#BA0C2F", color = "#00205B") +
  facet_wrap(vars(res)) +
  theme_minimal() +
  labs(caption = "Source: www.geoboundaries.org")

Caching

Downloaded files are cached locally. That means:

  • You download once
  • Rerunning your script is quick
  • Your workflow remains reproducible

You can set the cache folder with:

gb_set_cache_dir("a/path/to/a/folder")

When should you use geobounds?

Usage geoborders when:

  • You need reliable global administrative boundaries
  • You want reproducible workflows
  • You prefer code over manual downloads
  • You build maps, dashboards or spatial analyses

geoborders is not alone in this room. Depending on your needs, you may also want to look at:

natural earth

A very popular package for accessing Natural Earth datasets directly from R. It is lightweight and ideal for fast world maps, especially on small scales.

If you need physical layers (rivers, coastlines, elevation changes) in addition to political boundaries, this is often a good choice.

giscoR

If your focus is on Europe, giscoR provides direct access to Eurostat GISCO data. It is especially useful for NUTS regions and European statistical boundaries.

osmdata

When administrative boundaries are not enough and you need OpenStreetMap features (roads, POIs, land use, etc.), osmdata gives you powerful search capabilities.

I built geoborders to provide direct access to geoBoundaries products. It is a welcome addition to the R-GIS ecosystem.

Enjoy!


#Introduction #geoboundaries #bloggers

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *