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")

You can install the developing version of mapSpain with:

library(remotes)
install_github("rOpenSpain/mapSpain")

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 tmap
library(tmap)

tm_shape(CCAA_sf) +
  tm_polygons(
    "porc_women",
    border.col = "grey70",
    title = "Porc. women",
    palette = "Blues",
    alpha = 0.7,
    legend.format = list(
      fun = function(x) {
        sprintf("%1.1f%%", 100 * x)
      }
    )
  ) +
  tm_shape(CCAA_sf, point.per = "feature") +
  tm_text("porc_women_lab", auto.placement = TRUE) +
  tm_shape(Can) +
  tm_lines(col = "grey70") +
  tm_layout(legend.position = c("LEFT", "center"))

You can combine POLYGONS with static tiles

library(mapSpain)
library(sf)

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

census$municode <- paste0(census$cpro, census$cmun)

shape <- esp_get_munic(region = "Teruel")
provs <- esp_get_prov()

shape_pop <-
  merge(shape, census, by.x = "LAU_CODE", by.y = "municode")

# Get tiles

shape_pop <- st_transform(shape_pop, 3857)
provs <- st_transform(provs, 3857)

tile <-
  esp_getTiles(shape_pop,
    type = "IGNBase.Todo",
    zoom = 9 # Increase to get more quality
  )

# Plot

library(tmap)

tm_shape(tile, raster.downsample = FALSE) +
  tm_rgb() +
  tm_shape(shape_pop) +
  tm_fill("porc_women",
    palette = "-inferno",
    title = "",
    alpha = 0.6,
    legend.format = list(
      fun = function(x) {
        sprintf("%1.0f%%", 100 * x)
      },
      text.to.columns = TRUE
    )
  ) +
  tm_shape(provs) +
  tm_borders() +
  tm_layout(
    main.title = "Share of women in Teruel by town (2019)",
    main.title.fontface = "bold",
    main.title.size = 1.2,
    legend.text.size = 1,
    legend.bg.color = "white",
    legend.frame = TRUE,
    legend.position = c("right", "bottom")
  )

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"

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

# Project to same CRS

all_countries <- st_transform(all_countries, 3035)
eu_countries <- st_transform(eu_countries, 3035)
ccaa <- st_transform(ccaa, 3035)

# Plot
library(tmap)

tm_shape(all_countries, bbox = c(23, 14, 67, 54) * 10e4) +
  tm_graticules(col = "#DFDFDF", alpha = 0.7) +
  tm_fill("#DFDFDF") +
  tm_shape(eu_countries) +
  tm_polygons("#FDFBEA", border.col = "#656565") +
  tm_shape(ccaa) +
  tm_polygons("#C12838", border.col = "grey80", border.alpha = 0.5)

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:

options(mapSpain_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:

Contribute

Check the Github page for source code.