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

census <- mapSpain::pobmun19

# Paste CCAA
census <-
  unique(merge(census, esp_codelist[, c("cpro", "codauto")], all.x = TRUE))

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

census_ccaa$porc_women <- census_ccaa$women / census_ccaa$pob19

CCAA.sf <- esp_get_ccaa()

# Merge data
CCAA.sf <- merge(CCAA.sf, census_ccaa)



# Choropleth map

library(cartography)
br <- getBreaks(CCAA.sf$porc_women, method = "pretty")
pal <- hcl.colors(length(br) - 1,
  palette = "Blues 3",
  rev = TRUE,
  alpha = 0.8
)
choroLayer(
  CCAA.sf,
  var = "porc_women",
  breaks = br,
  legend.values.rnd = 3,
  legend.pos = "bottomright",
  col = pal,
  border = NA,
  legend.title.txt = "% women"
)

# Add lines

CCAAbox <- esp_get_can_box()

plot(CCAAbox, add = TRUE, col = pal[length(pal)])

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)

Asturias.sf <- esp_get_munic(region = "Asturias")

Asturias.pop <-
  merge(Asturias.sf, census, by.x = "LAU_CODE", by.y = "municode")

# Get tiles

Asturias.pop <- st_transform(Asturias.pop, 3857)

tile <-
  esp_getTiles(Asturias.pop,
    type = "IGNBase.Todo",
    zoom = 8
  )


# Plot
par(mar = c(0, 0, 0, 0))
library(cartography)

tilesLayer(tile)
br <- getBreaks(Asturias.pop$porc_women, method = "kmeans")


choroLayer(
  Asturias.pop,
  var = "porc_women",
  breaks = br,
  col = hcl.colors(
    n = length(br) - 1,
    palette = "inferno",
    rev = TRUE,
    alpha = 0.4
  ),
  add = TRUE,
  legend.values.rnd = 2,
  border = NA,
  legend.pos = "n"
)

legendChoro(
  pos = "bottomright",
  breaks = br,
  values.rnd = 2,
  horiz = TRUE,
  nodata = FALSE,
  col = hcl.colors(
    n = length(br) - 1,
    palette = "inferno",
    rev = TRUE
  ),
  title.txt = "% women"
)

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.