Learn R Programming

mapSpain

mapSpain is a package that provides spatial sf objects of the administrative boundaries of Spain, including CCAA, provinces and municipalities.

mapSpain also provides a leaflet plugin to be used with the leaflet package, that loads several basemaps of public institutions of Spain, and the ability of downloading and processing static tiles.

Full site with examples and vignettes on https://ropenspain.github.io/mapSpain/

Installation

Install mapSpain from CRAN:

install.packages("mapSpain", dependencies = TRUE)

You can install the developing version of mapSpain using the r-universe:

# Enable this universe
options(repos = c(
  ropenspain = "https://ropenspain.r-universe.dev",
  CRAN = "https://cloud.r-project.org"
))

install.packages("mapSpain", dependencies = TRUE)

Alternatively, you can install the developing version of mapSpain with:

library(remotes)
install_github("rOpenSpain/mapSpain", dependencies = TRUE)

Usage

This script highlights some features of mapSpain :

library(mapSpain)

census <- mapSpain::pobmun19

# Extract CCAA from base dataset

codelist <- mapSpain::esp_codelist

census <-
  unique(merge(census, codelist[, c("cpro", "codauto")], all.x = TRUE))

# Summarize by CCAA
census_ccaa <-
  aggregate(cbind(pob19, men, women) ~ codauto, data = census, sum)

census_ccaa$porc_women <- census_ccaa$women / census_ccaa$pob19
census_ccaa$porc_women_lab <-
  paste0(round(100 * census_ccaa$porc_women, 2), "%")

# Merge into spatial data

CCAA_sf <- esp_get_ccaa()
CCAA_sf <- merge(CCAA_sf, census_ccaa)
Can <- esp_get_can_box()


# Plot with ggplot
library(ggplot2)


ggplot(CCAA_sf) +
  geom_sf(aes(fill = porc_women),
    color = "grey70",
    lwd = .3
  ) +
  geom_sf(data = Can, color = "grey70") +
  geom_sf_label(aes(label = porc_women_lab),
    fill = "white", alpha = 0.5,
    size = 3,
    label.size = 0
  ) +
  scale_fill_gradientn(
    colors = hcl.colors(10, "Blues", rev = TRUE),
    n.breaks = 10,
    labels = function(x) {
      sprintf("%1.1f%%", 100 * x)
    },
    guide = guide_legend(title = "Porc. women")
  ) +
  theme_void() +
  theme(legend.position = c(0.1, 0.6))

You can combine sf objects with static tiles

# Get census
census <- mapSpain::pobmun19
census$porc_women <- census$women / census$pob19

# Get shapes
shape <- esp_get_munic_siane(region = "Segovia", epsg = 3857)
provs <- esp_get_prov_siane(epsg = 3857)

shape_pop <- merge(shape,
  census,
  by = c("cpro", "cmun"),
  all.x = TRUE
)


tile <-
  esp_getTiles(shape_pop,
    type = "IGNBase.Todo",
    zoom = 10,
    bbox_expand = .1
  )

# Plot

library(ggplot2)

lims <- as.double(terra::ext(tile)@ptr$vector)

ggplot(remove_missing(shape_pop, na.rm = TRUE)) +
  layer_spatraster(tile) +
  geom_sf(aes(fill = porc_women), color = NA) +
  geom_sf(data = provs, fill = NA) +
  scale_fill_gradientn(
    colours = hcl.colors(10, "RdYlBu", alpha = .5),
    n.breaks = 8,
    labels = function(x) {
      sprintf("%1.0f%%", 100 * x)
    },
    guide = guide_legend(title = "", )
  ) +
  coord_sf(
    xlim = lims[c(1, 2)],
    ylim = lims[c(3, 4)],
    expand = FALSE
  ) +
  labs(
    title = "Share of women in Segovia by town (2019)",
    caption = "Source: INE"
  ) +
  theme_void() +
  theme(
    title = element_text(face = "bold")
  )

mapSpain and giscoR

If you need to plot Spain along with another countries, consider using giscoR package, that is installed as a dependency when you installed mapSpain. A basic example:

library(giscoR)

# Set the same resolution for a perfect fit

res <- "03"

# Same crs
target_crs <- 3035

all_countries <- gisco_get_countries(
  resolution = res,
  epsg = target_crs
)
eu_countries <- gisco_get_countries(
  resolution = res, region = "EU",
  epsg = target_crs
)
ccaa <- esp_get_ccaa(
  moveCAN = FALSE, resolution = res,
  epsg = target_crs
)

library(ggplot2)

ggplot(all_countries) +
  geom_sf(fill = "#DFDFDF", color = "#656565") +
  geom_sf(data = eu_countries, fill = "#FDFBEA", color = "#656565") +
  geom_sf(data = ccaa, fill = "#C12838", color = "grey80", lwd = .1) +
  # Center in Europe: EPSG 3035
  coord_sf(
    xlim = c(2377294, 7453440),
    ylim = c(1313597, 5628510)
  ) +
  theme(
    panel.background = element_blank(),
    panel.grid = element_line(
      colour = "#DFDFDF",
      linetype = "dotted"
    )
  )

A note on caching

Some data sets and tiles may have a size larger than 50MB. You can use mapSpain to create your own local repository at a given local directory passing the following option:

esp_set_cache_dir("./path/to/location")

When this option is set, mapSpain would look for the cached file and it will load it, speeding up the process.

Plotting sf objects

Some packages recommended for visualization are:

Citation

To cite the ‘mapSpain’ package in publications use:

Hernangómez D (2022). mapSpain: Administrative Boundaries of Spain. doi: 10.5281/zenodo.5366622 (URL: https://doi.org/10.5281/zenodo.5366622), <URL: https://ropenspain.github.io/mapSpain/>.

A BibTeX entry for LaTeX users is:

@Manual{,
  title = {mapSpain: Administrative Boundaries of Spain},
  year = {2022},
  version = {0.6.1},
  author = {Diego Hernangómez},
  doi = {10.5281/zenodo.5366622},
  url = {https://ropenspain.github.io/mapSpain/},
  abstract = {Administrative Boundaries of Spain at several levels (Autonomous Communities, Provinces, Municipalities) based on the 'GISCO' 'Eurostat' database <https://ec.europa.eu/eurostat/web/gisco> and 'CartoBase SIANE' from 'Instituto Geografico Nacional' <https://www.ign.es/>. It also provides a 'leaflet' plugin and the ability of downloading and processing static tiles.},
}

Contribute

Check the GitHub page for source code.

Copyright notice

This package uses data from CartoBase SIANE, provided by Instituto Geográfico Nacional:

Atlas Nacional de España (ANE) CC BY 4.0 ign.es

See https://github.com/rOpenSpain/mapSpain/tree/sianedata

This package uses data from GISCO. GISCO (FAQ) is a geospatial open data repository including several data sets at several resolution levels.

From GISCO > Geodata > Reference data > Administrative Units / Statistical Units

When data downloaded from this page is used in any printed or electronic publication, in addition to any other provisions applicable to the whole Eurostat website, data source will have to be acknowledged in the legend of the map and in the introductory page of the publication with the following copyright notice:

EN: © EuroGeographics for the administrative boundaries

FR: © EuroGeographics pour les limites administratives

DE: © EuroGeographics bezüglich der Verwaltungsgrenzen

For publications in languages other than English, French or German, the translation of the copyright notice in the language of the publication shall be used.

If you intend to use the data commercially, please contact EuroGeographics for information regarding their license agreements.

Copy Link

Version

Install

install.packages('mapSpain')

Monthly Downloads

1,523

Version

0.6.1

License

GPL-3

Issues

Pull Requests

Stars

Forks

Maintainer

Diego Hernang<c3><b3>mez

Last Published

February 25th, 2022

Functions in mapSpain (0.6.1)

esp_check_access

Check access to SIANE data
addProviderEspTiles

Include base tiles of Spanish public administrations on a leaflet map
esp_getTiles

Get static tiles from public administrations of Spanish.
esp_dict_region_code

Convert and translate Subdivision Names
esp_get_prov

Get Provinces of Spain as sf polygons and points
esp_get_nuts

Get NUTS of Spain as sf polygons and points
esp_get_can_box

Get sf lines and polygons for insetting the Canary Islands
esp_get_munic

Get municipalities of Spain as sf polygons
esp_get_capimun

Get sf points of the municipalities of Spain
esp_get_hypsobath

Get sf polygons and lines with the hypsometry and bathymetry of Spain
mapSpain-package

mapSpain package
pobmun19

Population by municipality (2019)
esp_get_grid_MTN

Get sf polygons of the national geographic grids provided by IGN
esp_get_grid_ESDAC

Get sf polygons of the national geographic grids provided by ESDAC
esp_clear_cache

Clear your mapSpain cache dir
esp_codelist

Spanish Code Translation Data Frame
esp_get_gridmap

Get a sf hexbin or squared polygon of Spain
esp_get_hydrobasin

Get sf polygons of the drainage basin demarcations of Spain
esp_get_ccaa

Get Autonomous Communities of Spain as sf polygons and points
esp_get_country

Get the borders of Spain as a sf polygon
layer_spatraster

Spatial ggplot2 layer for SpatRaster objects
leaflet.providersESP.df

Public WMS and WMTS of Spain
esp_nuts.sf

All NUTS POLYGON object of Spain
esp_set_cache_dir

Set your mapSpain cache dir
esp_get_grid_EEA

Get sf polygons of the national geographic grids provided by EEA
esp_get_grid_BDN

Get sf polygons of the national geographic grids provided by BDN
esp_get_railway

Get sf lines and points with the railways of Spain
esp_get_rivers

Get sf polygon and lines of rivers, channels and other wetlands of Spain
esp_munic.sf

All Municipalities POLYGON object of Spain (2019)
esp_get_roads

Get sf lines of the roads of Spain