Full site with more examples and vignettes on https://ropenspain.github.io/mapSpain/
mapSpain is a package designed to provide geographical information of Spain at different levels.
mapSpain provides shapefiles of municipalities, provinces, autonomous communities and NUTS levels of Spain. It also provides hexbin shapefiles and other complementary shapes, as the usual lines around the Canary Islands.
mapSpain provides access to map tiles of public organisms of Spain, that can be represented on static maps via mapSpain::esp_getTiles() or on a R leaflet map using mapSpain::addProviderEspTiles().
On top of that, mapSpain also has a powerful dictionary that translate provinces and other regions to English, Spanish, Catalan, Basque language or Galician, and also converts those names to different coding standards, as NUTS, ISO2 or the coding system used by the INE, that is the official statistic agency of Spain.
mapSpain provides a dataset and tile caching capability, that could be set as:
options(mapSpain_cache_dir = "~/path/to/dir")
OR
options(gisco_cache_dir = "~/path/to/dir")mapSpain relies on giscoR for downloading some files, and both packages are well synchronized, so if you already use giscoR and you have set your caching options for that package it would be recognized too by mapSpain.
Install mapSpain from CRAN:
install.packages("mapSpain")For installing the development version on (Github):
library(remotes)
install_github("rOpenSpain/mapSpain")Some examples of what mapSpaincan do:
library(mapSpain)
library(tmap)
country <- esp_get_country()
lines <- esp_get_can_box()
tm_shape(country) +
tm_polygons() +
tm_shape(lines) +
tm_lines() +
tm_graticules(lines = FALSE) +
tm_style("classic") +
tm_layout(main.title = "Map of Spain")
# Plot provinces
Andalucia <- esp_get_prov("Andalucia")
tm_shape(Andalucia) +
tm_polygons(col = "darkgreen", border.col = "white") +
tm_graticules(lines = FALSE)
# Plot municipalities
Euskadi_CCAA <- esp_get_ccaa("Euskadi")
Euskadi <- esp_get_munic(region = "Euskadi")
# Use dictionary
Euskadi$name_eu <- esp_dict_translate(Euskadi$ine.prov.name, lang = "eu")
tm_shape(Euskadi_CCAA) +
tm_fill("grey50") +
tm_shape(Euskadi) +
tm_polygons("name_eu",
palette = c("red2", "darkgreen", "ivory2"),
title = ""
) +
tm_layout(
main.title = paste0(
"Euskal Autonomia Erkidegoko",
"\n",
"Probintziak"
),
main.title.size = 0.8,
main.title.fontface = "bold"
)Let’s analyze the distribution of women in each autonomous community with tmap:
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
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"))This is an example on how mapSpain can be used to beautiful thematic maps. For plotting purposes we would use the tmap package, however any package that handles sf objects (e.g. ggplot2, mapsf, leaflet, etc. could be used).
# Population density of Spain
library(sf)
pop <- mapSpain::pobmun19
munic <- esp_get_munic()
# Get area (km2) - Use LAEA projection
municarea <- as.double(st_area(st_transform(munic, 3035)) / 1000000)
munic$area <- municarea
munic.pop <- merge(munic, pop, all.x = TRUE)
munic.pop$dens <- munic.pop$pob19 / munic.pop$area
br <-
c(
0,
10,
25,
100,
200,
500,
1000,
5000,
10000,
Inf
)
tm_shape(munic.pop) +
tm_fill("dens",
breaks = br,
alpha = 0.8,
title = "Pop. per km2",
palette = "inferno",
showNA = FALSE,
colorNA = "#000004"
) +
tm_layout(
main.title = "Population Density in Spain (2019)",
main.title.size = 0.8,
frame = FALSE,
bg.color = "#000004",
legend.outside = TRUE,
legend.text.color = "white",
legend.title.color = "white",
main.title.color = "white",
main.title.fontface = "bold",
legend.text.fontface = "bold",
legend.title.fontface = "bold"
)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 <- "20"
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
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 = "white")Details
#> R version 4.0.3 (2020-10-10)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19041)
#>
#> Matrix products: default
#>
#> locale:
#> [1] LC_COLLATE=C LC_CTYPE=Spanish_Spain.1252
#> [3] LC_MONETARY=Spanish_Spain.1252 LC_NUMERIC=C
#> [5] LC_TIME=Spanish_Spain.1252
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] giscoR_0.2.4 sf_0.9-8 tmap_3.3-1 mapSpain_0.2.2
#>
#> loaded via a namespace (and not attached):
#> [1] Rcpp_1.0.6 countrycode_1.2.0 lattice_0.20-41 png_0.1-7
#> [5] class_7.3-18 assertthat_0.2.1 digest_0.6.27 utf8_1.2.1
#> [9] R6_2.5.0 evaluate_0.14 e1071_1.7-6 highr_0.9
#> [13] pillar_1.6.0 rlang_0.4.10 raster_3.4-5 jquerylib_0.1.3
#> [17] rmarkdown_2.7 stringr_1.4.0 htmlwidgets_1.5.3 proxy_0.4-25
#> [21] compiler_4.0.3 xfun_0.22 pkgconfig_2.0.3 tmaptools_3.1-1
#> [25] base64enc_0.1-3 htmltools_0.5.1.1 tidyselect_1.1.0 tibble_3.1.0
#> [29] codetools_0.2-18 XML_3.99-0.6 fansi_0.4.2 viridisLite_0.4.0
#> [33] crayon_1.4.1.9000 dplyr_1.0.5 grid_4.0.3 jsonlite_1.7.2
#> [37] lwgeom_0.2-6 lifecycle_1.0.0 DBI_1.1.1 magrittr_2.0.1
#> [41] units_0.7-1 KernSmooth_2.23-18 stringi_1.5.3 leafsync_0.1.0
#> [45] leaflet_2.0.4.1 sp_1.4-5 bslib_0.2.4 ellipsis_0.3.1
#> [49] generics_0.1.0 vctrs_0.3.7 RColorBrewer_1.1-2 tools_4.0.3
#> [53] dichromat_2.0-0 leafem_0.1.3 glue_1.4.2 purrr_0.3.4
#> [57] crosstalk_1.1.1 abind_1.4-5 parallel_4.0.3 yaml_2.2.1
#> [61] stars_0.5-2 classInt_0.4-3 knitr_1.31 sass_0.3.1