rnaturalearth R Package

repository·main·Indexed 42 days ago

https://github.com/ropensci/rnaturalearth

An R package providing access to Natural Earth map data. It enables downloading, subsetting, and working with vector and raster map data in sf and sv formats via functions such as ne_countries(), ne_states(), and ne_download().

Tokens
876
Snippets
4
Records
6
Agent score
4%

What's inside rnaturalearth

  1. Set the return class for spatial data

    main

    All retrieval functions in rnaturalearth accept a returnclass argument. This allows you to specify the spatial object format returned:

    • returnclass = "sf": Returns sf (Simple Features) objects.
    • returnclass = "sv": Returns sv (SpatVector) objects.
  2. Access introductory vignettes

    main

    For a detailed guide on using the package, run the following command in your R session:

    vignette("rnaturalearth", package = "rnaturalearth")

    For information regarding country definitions and scales, use:

    vignette("what-is-a-country", package = "rnaturalearth")
  3. Install rnaturalearth

    main

    You can install the stable version of rnaturalearth from CRAN. To install the development version, use pak.

    Note that much of the package's functionality depends on two additional data packages: rnaturalearthdata and rnaturalearthhires. It is recommended to install these manually to ensure all features work correctly.

    # Install from CRAN
    install.packages("rnaturalearth")
    
    # Install development version from GitHub
    pak::pkg_install("ropensci/rnaturalearth")
    
    # Install required data packages
    pak::pkg_install("ropensci/rnaturalearthdata")
    pak::pkg_install("ropensci/rnaturalearthhires")
  4. Retrieve administrative boundaries with ne_states()

    main

    Use ne_states() to retrieve administrative level 1 boundaries (e.g., states or provinces) for a specific country.

    library(rnaturalearth)
    
    # Plot states for Spain
    plot(ne_states(country = "spain"))
  5. Retrieve and plot world countries with ne_countries()

    main

    Use ne_countries() to retrieve vector data for world countries. You can subset the data by providing a specific country name.

    By default, the returned object can be plotted using base R plot(), but it is also compatible with ggplot2 and tmap if you specify the correct return class.

    library(rnaturalearth)
    
    # Plot all world countries
    plot(ne_countries())
    
    # Plot a specific country (e.g., United Kingdom)
    plot(ne_countries(country = "united kingdom"))
  6. Download custom Natural Earth data with ne_download()

    main

    If the data you need is not included in the package, use ne_download() to fetch it directly from Natural Earth. You must specify the scale, type, and category (either "physical" or "cultural").

    Example of downloading lakes and rivers:

    library(rnaturalearth)
    library(ggplot2)
    library(sf)
    
    # Download lakes at 1:110 scale
    lakes110 <- ne_download(scale = 110, type = "lakes", category = "physical")
    plot(lakes110)
    
    # Download rivers at 1:50 scale as an sf object
    rivers50 <- ne_download(
      scale = 50,
      type = "rivers_lake_centerlines",
      category = "physical",
      returnclass = "sf"
    )
    
    # Plot using ggplot2
    ggplot(rivers50) +
      geom_sf() +
      theme_minimal()