Skip to contents

This dataset shows boundaries of municipalities in Spain.

Usage

esp_get_munic(
  year = 2024,
  epsg = 4258,
  cache = deprecated(),
  update_cache = FALSE,
  cache_dir = NULL,
  verbose = FALSE,
  region = NULL,
  munic = NULL,
  moveCAN = TRUE,
  ext = "gpkg"
)

Arguments

year

year character string or number. Release year of the file. See giscoR::gisco_get_lau() and giscoR::gisco_get_communes() for valid values.

epsg

character string or number. Projection of the map: 4-digit EPSG code. One of:

cache

[Deprecated]. This argument is deprecated, the dataset will always be downloaded to the cache_dir.

update_cache

logical. Should the cached file be refreshed? Default is FALSE. When set to TRUE, it will force a new download.

cache_dir

character string. A path to a cache directory. See Caching strategies section in esp_set_cache_dir().

verbose

logical. If TRUE displays informational messages.

region

Optional. A vector of region names, NUTS or ISO codes (see esp_dict_region_code()).

munic

character string. A name or regex expression with the names of the required municipalities. NULL will return all municipalities.

moveCAN

A logical TRUE/FALSE or a vector of coordinates c(lat, lon). It places the Canary Islands close to Spain's mainland. Initial position can be adjusted using the vector of coordinates. See Displacing the Canary Islands in esp_move_can().

ext

character. Extension of the file (default "gpkg"). See giscoR::gisco_get_nuts().

Value

A sf object.

Details

When using region you can use and mix names and NUTS codes (levels 1, 2 or 3), ISO codes (corresponding to level 2 or 3) or "cpro" (see esp_codelist).

When calling a higher level (province, CCAA or NUTS1), all the municipalities of that level will be added.

Note

Please check the download and usage provisions on gisco_attributions().

Examples

# \donttest{
# The Spanish Lapland:
# https://en.wikipedia.org/wiki/Celtiberian_Range

# Get munics
spanish_laplad <- esp_get_munic(
  year = 2023,
  region = c(
    "Cuenca", "Teruel",
    "Zaragoza", "Guadalajara",
    "Soria", "Burgos",
    "La Rioja"
  )
)
#> ! The file to be downloaded has size 61 Mb.

breaks <- sort(c(0, 5, 10, 50, 100, 200, 500, 1000, Inf))
spanish_laplad$dens_breaks <- cut(spanish_laplad$POP_DENS_2023, breaks,
  dig.lab = 20
)

cut_labs <- prettyNum(breaks, big.mark = " ")[-1]
cut_labs[length(breaks)] <- "> 1000"

library(ggplot2)
ggplot(spanish_laplad) +
  geom_sf(aes(fill = dens_breaks), color = "grey30", linewidth = 0.1) +
  scale_fill_manual(
    values = hcl.colors(length(breaks) - 1, "Spectral"), na.value = "black",
    name = "people per sq. kilometer",
    labels = cut_labs,
    guide = guide_legend(
      direction = "horizontal",
      nrow = 1
    )
  ) +
  theme_void() +
  labs(
    title = "The Spanish Lapland",
    caption = giscoR::gisco_attributions()
  ) +
  theme(
    text = element_text(colour = "white"),
    plot.background = element_rect(fill = "grey2"),
    plot.title = element_text(hjust = 0.5),
    plot.subtitle = element_text(hjust = 0.5, face = "bold"),
    plot.caption = element_text(
      color = "grey60", hjust = 0.5, vjust = 0,
      margin = margin(t = 5, b = 10)
    ),
    legend.position = "bottom",
    legend.title.position = "top",
    legend.text.position = "bottom",
    legend.key.height = unit(0.5, "lines"),
    legend.key.width = unit(1, "lines")
  )

# }