).
-Los objetos resultantes se proporcionan en formato `sf` o `SpatRaster`(`terra`).
+Los objetos resultantes se proporcionan en formato `sf` (librería **sf)** o
+`SpatRaster`(librería **terra**).
Página web:
@@ -69,12 +68,13 @@ Usando el [r-universe](https://ropenspain.r-universe.dev/ui#builds):
```{r instruniverse, eval=FALSE}
# Enable this universe
-options(repos = c(
- ropenspain = "https://ropenspain.r-universe.dev",
- CRAN = "https://cloud.r-project.org"
-))
-
-install.packages("mapSpain", dependencies = TRUE)
+install.packages("mapSpain",
+ repos = c(
+ "https://ropenspain.r-universe.dev",
+ "https://cloud.r-project.org"
+ ),
+ dependencies = TRUE
+)
```
#### Remotes
@@ -91,20 +91,13 @@ library(tidyverse)
galicia <- esp_get_munic_siane(region = "Galicia") %>%
# Homogeinizo labels
- mutate(
- Provincia = esp_dict_translate(ine.prov.name, "es")
- )
+ mutate(Provincia = esp_dict_translate(ine.prov.name, "es"))
ggplot(galicia) +
- geom_sf(aes(fill = Provincia),
- color = "grey70"
- ) +
+ geom_sf(aes(fill = Provincia), color = "grey70") +
labs(title = "Provincias de Galicia") +
- scale_fill_discrete(
- type =
- hcl.colors(4, "Blues")
- ) +
+ scale_fill_discrete(type = hcl.colors(4, "Blues")) +
theme_bw()
```
@@ -114,8 +107,7 @@ Si exploramos el dataset:
library(reactable)
reactable(galicia,
- searchable = TRUE, striped = TRUE,
- filterable = TRUE, height = 350,
+ searchable = TRUE, striped = TRUE, filterable = TRUE, height = 350,
elementId = "munis-galicia"
)
```
@@ -136,10 +128,8 @@ esp_rnat <- ne_countries("large", country = "Spain", returnclass = "sf")
esp_mapspain <- esp_get_country(epsg = 4326)
# Imagen Ria Ferrol
-tile <- esp_getTiles(esp_get_munic(munic = "Ferrol", epsg = 3857),
- "IGNBase.Gris",
- bbox_expand = 0.5, zoommin = 1
-)
+tile <- esp_get_munic(munic = "Ferrol", epsg = 3857) %>%
+ esp_getTiles("IGNBase.Gris", bbox_expand = 0.5, zoommin = 1)
# Prepara el plot
library(tidyterra)
@@ -151,14 +141,9 @@ esp_all$source <- c("rnaturalearth", "mapSpain")
ggplot(esp_all) +
geom_spatraster_rgb(data = tile, maxcell = Inf) +
- geom_sf(aes(color = source),
- fill = NA, show.legend = "line",
- linewidth = 1.2
- ) +
+ geom_sf(aes(color = source), fill = NA, show.legend = "line", linewidth = 1.2) +
coord_sf(
- crs = 4326,
- xlim = c(-8.384421, -8.154413),
- ylim = c(43.43201, 43.59545),
+ crs = 4326, xlim = c(-8.384421, -8.154413), ylim = c(43.43201, 43.59545),
expand = FALSE
) +
scale_color_manual(values = c("red", "black")) +
@@ -167,7 +152,6 @@ ggplot(esp_all) +
```
- **rnaturalearth**: No capta bien el contorno.
-
- **mapSpain**: Resultados satisfactorios.
### Almacenamiento
@@ -209,13 +193,10 @@ códigos:
- `esp_dict_region_code()` convierte textos en códigos de CCAA y provincias.
Esquemas de codificación soportados:
-
- ISO2
- NUTS
- INE (codauto y cpro)
-
- `esp_dict_translate()` traduce textos a diferentes idiomas:
-
- Castellano
- Inglés
- Catalán
@@ -241,27 +222,19 @@ esp_dict_region_code(iso2vals, origin = "iso2")
iso2vals <- c("ES-GA", "ES-CT", "ES-PV")
-esp_dict_region_code(iso2vals,
- origin = "iso2",
- destination = "nuts"
-)
+esp_dict_region_code(iso2vals, origin = "iso2", destination = "nuts")
# Soporta diferentes niveles
valsmix <- c("Centro", "Andalucia", "Seville", "Menorca")
esp_dict_region_code(valsmix, destination = "nuts")
-esp_dict_region_code(c("Murcia", "Las Palmas", "Aragón"),
- destination = "iso2"
-)
+esp_dict_region_code(c("Murcia", "Las Palmas", "Aragón"), destination = "iso2")
```
#### `esp_dict_translate()`
```{r}
-vals <- c(
- "La Rioja", "Sevilla", "Madrid",
- "Jaen", "Orense", "Baleares"
-)
+vals <- c("La Rioja", "Sevilla", "Madrid", "Jaen", "Orense", "Baleares")
esp_dict_translate(vals, lang = "en")
esp_dict_translate(vals, lang = "es")
@@ -350,15 +323,12 @@ ggplot(nuts3_sf) +
```{r ccaa}
ccaa <- esp_get_ccaa(ccaa = c(
- "Catalunya",
- "Comunidad Valenciana",
- "Aragón",
+ "Catalunya", "Comunidad Valenciana", "Aragón",
"Baleares"
))
-ccaa <- ccaa %>% mutate(
- ccaa_cat = esp_dict_translate(ccaa$ine.ccaa.name, "ca")
-)
+ccaa <- ccaa %>%
+ mutate(ccaa_cat = esp_dict_translate(ine.ccaa.name, "ca"))
ggplot(ccaa) +
geom_sf(aes(fill = ccaa_cat)) +
@@ -374,14 +344,12 @@ provincias de esa entidad.
```{r prov}
provs <- esp_get_prov_siane(c(
- "Andalucía", "Ciudad Real",
- "Murcia", "Ceuta", "Melilla"
+ "Andalucía", "Ciudad Real", "Murcia", "Ceuta",
+ "Melilla"
))
ggplot(provs) +
- geom_sf(aes(fill = prov.shortname.es),
- alpha = 0.9
- ) +
+ geom_sf(aes(fill = prov.shortname.es), alpha = 0.9) +
scale_fill_discrete(type = hcl.colors(12, "Cividis")) +
theme_minimal() +
labs(fill = "Provincias")
@@ -398,13 +366,11 @@ ggplot(munic) +
geom_sf(aes(fill = pob19), alpha = 0.9, color = NA) +
scale_fill_gradientn(
colors = hcl.colors(100, "Inferno"),
- n.breaks = 10,
- labels = scales::label_comma(),
+ n.breaks = 10, labels = scales::label_comma(),
guide = guide_legend()
) +
labs(
- fill = "Habitantes",
- title = "Población en Segovia",
+ fill = "Habitantes", title = "Población en Segovia",
subtitle = "Datos INE (2019)"
) +
theme_void() +
@@ -476,7 +442,8 @@ ggplot() +
madrid <- esp_get_munic_siane(munic = "^Madrid$", epsg = 3857)
madrid_mask <- esp_getTiles(madrid, "IGNBase.Gris",
- mask = TRUE, crop = TRUE, zoommin = 2
+ mask = TRUE, crop = TRUE,
+ zoommin = 2
)
diff --git a/vignettes/basic-1.png b/vignettes/basic-1.png
index 1b6e4c61..8f38a224 100644
Binary files a/vignettes/basic-1.png and b/vignettes/basic-1.png differ
diff --git a/vignettes/basic2-1.png b/vignettes/basic2-1.png
index 302c600f..5fd8d381 100644
Binary files a/vignettes/basic2-1.png and b/vignettes/basic2-1.png differ
diff --git a/vignettes/basic3-1.png b/vignettes/basic3-1.png
index 2a04176e..a1f2526b 100644
Binary files a/vignettes/basic3-1.png and b/vignettes/basic3-1.png differ
diff --git a/vignettes/choro-1.png b/vignettes/choro-1.png
index 42178a6c..dae89b86 100644
Binary files a/vignettes/choro-1.png and b/vignettes/choro-1.png differ
diff --git a/vignettes/giscoR-1.png b/vignettes/giscoR-1.png
index ffdf9709..78c53abd 100644
Binary files a/vignettes/giscoR-1.png and b/vignettes/giscoR-1.png differ
diff --git a/vignettes/mapSpain.Rmd b/vignettes/mapSpain.Rmd
index e86b5542..872c2e54 100644
--- a/vignettes/mapSpain.Rmd
+++ b/vignettes/mapSpain.Rmd
@@ -144,16 +144,16 @@ census <- mapSpain::pobmun19
codelist <- mapSpain::esp_codelist
-census <-
- unique(merge(census, codelist[, c("cpro", "codauto")], all.x = TRUE))
+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 <- 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), "%")
+census_ccaa$porc_women_lab <- paste0(
+ round(100 * census_ccaa$porc_women, 2),
+ "%"
+)
# Merge into spatial data
@@ -168,9 +168,7 @@ ggplot(CCAA_sf) +
) +
geom_sf(data = Can, color = "grey70") +
geom_sf_label(aes(label = porc_women_lab),
- fill = "white", alpha = 0.5,
- size = 3,
- label.size = 0
+ fill = "white", alpha = 0.5, size = 3, label.size = 0
) +
scale_fill_gradientn(
colors = hcl.colors(10, "Blues", rev = TRUE),
@@ -178,10 +176,10 @@ ggplot(CCAA_sf) +
labels = function(x) {
sprintf("%1.1f%%", 100 * x)
},
- guide = guide_legend(title = "Porc. women")
+ guide = guide_legend(title = "Porc. women", position = "inside")
) +
theme_void() +
- theme(legend.position = c(0.1, 0.6))
+ theme(legend.position.inside = c(0.1, 0.6))
```
@@ -223,14 +221,7 @@ ggplot(munic.pop) +
scale_fill_manual(
values = c("grey5", hcl.colors(length(br) - 2, "Spectral")),
labels = prettyNum(c(0, br[-1]), big.mark = ","),
- guide = guide_legend(
- title = "Pop. per km2",
- direction = "horizontal",
- nrow = 1,
- keywidth = 2,
- title.position = "top",
- label.position = "bottom"
- )
+ guide = guide_legend(title = "Pop. per km2", direction = "horizontal", nrow = 1)
) +
labs(title = "Population density in Spain (2019)") +
theme_void() +
@@ -238,7 +229,10 @@ ggplot(munic.pop) +
plot.title = element_text(hjust = .5),
plot.background = element_rect(fill = "black"),
text = element_text(colour = "white"),
- legend.position = "bottom"
+ legend.position = "bottom",
+ legend.title.position = "top",
+ legend.text.position = "bottom",
+ legend.key.width = unit(30, "pt")
)
```
diff --git a/vignettes/mapSpain.Rmd.orig b/vignettes/mapSpain.Rmd.orig
index 12e67982..76fc0970 100644
--- a/vignettes/mapSpain.Rmd.orig
+++ b/vignettes/mapSpain.Rmd.orig
@@ -55,7 +55,6 @@ of Spain.
as:
```{r, eval=FALSE}
-
esp_set_cache_dir("./path/to/location")
```
@@ -68,7 +67,6 @@ same caching directory on both would speed up the data load on your session.
Some examples of what **mapSpain** can do:
```{r basic, fig.cap="Example: Map of Spain", fig.asp=0.7}
-
library(mapSpain)
library(ggplot2)
@@ -89,7 +87,6 @@ ggplot(country) +
face = "bold"
)
)
-
```
```{r basic2, fig.cap="Example: Provinces of Andalucia", fig.asp=0.55}
@@ -134,23 +131,22 @@ Let's analyze the distribution of women in each autonomous community with
`ggplot`:
```{r choro, fig.cap="Percentage of women by Autonomous Community (2019)", fig.asp=0.7}
-
census <- mapSpain::pobmun19
# Extract CCAA from base dataset
codelist <- mapSpain::esp_codelist
-census <-
- unique(merge(census, codelist[, c("cpro", "codauto")], all.x = TRUE))
+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 <- 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), "%")
+census_ccaa$porc_women_lab <- paste0(
+ round(100 * census_ccaa$porc_women, 2),
+ "%"
+)
# Merge into spatial data
@@ -165,9 +161,7 @@ ggplot(CCAA_sf) +
) +
geom_sf(data = Can, color = "grey70") +
geom_sf_label(aes(label = porc_women_lab),
- fill = "white", alpha = 0.5,
- size = 3,
- label.size = 0
+ fill = "white", alpha = 0.5, size = 3, label.size = 0
) +
scale_fill_gradientn(
colors = hcl.colors(10, "Blues", rev = TRUE),
@@ -175,10 +169,10 @@ ggplot(CCAA_sf) +
labels = function(x) {
sprintf("%1.1f%%", 100 * x)
},
- guide = guide_legend(title = "Porc. women")
+ guide = guide_legend(title = "Porc. women", position = "inside")
) +
theme_void() +
- theme(legend.position = c(0.1, 0.6))
+ theme(legend.position.inside = c(0.1, 0.6))
```
## Thematic maps
@@ -190,7 +184,6 @@ handles `sf` objects (e.g. **tmap**, **mapsf**, **leaflet**, etc. could be
used).
```{r thematic, fig.cap="Population density in Spain (2019)", crop=TRUE, dpi=120}
-
# Population density of Spain
library(sf)
@@ -212,16 +205,10 @@ munic.pop$cuts <- cut(munic.pop$dens, br)
ggplot(munic.pop) +
geom_sf(aes(fill = cuts), color = NA, linewidth = 0) +
- scale_fill_manual(values = c("grey5", hcl.colors(length(br) - 2,"Spectral")),
+ scale_fill_manual(
+ values = c("grey5", hcl.colors(length(br) - 2, "Spectral")),
labels = prettyNum(c(0, br[-1]), big.mark = ","),
- guide = guide_legend(
- title = "Pop. per km2",
- direction = "horizontal",
- nrow = 1,
- keywidth = 2,
- title.position = "top",
- label.position = "bottom"
- )
+ guide = guide_legend(title = "Pop. per km2", direction = "horizontal", nrow = 1)
) +
labs(title = "Population density in Spain (2019)") +
theme_void() +
@@ -229,7 +216,10 @@ ggplot(munic.pop) +
plot.title = element_text(hjust = .5),
plot.background = element_rect(fill = "black"),
text = element_text(colour = "white"),
- legend.position = "bottom"
+ legend.position = "bottom",
+ legend.title.position = "top",
+ legend.text.position = "bottom",
+ legend.key.width = unit(30, "pt")
)
```
@@ -240,7 +230,6 @@ If you need to plot Spain along with another countries, consider using
dependency when you installed **mapSpain**. A basic example:
```{r giscoR, fig.cap="mapSpain and giscoR example", fig.asp=0.9}
-
library(giscoR)
# Set the same resolution for a perfect fit
@@ -251,11 +240,13 @@ all_countries <- gisco_get_countries(resolution = res) |>
st_transform(3035)
eu_countries <- gisco_get_countries(
- resolution = res, region = "EU") |>
+ resolution = res, region = "EU"
+) |>
st_transform(3035)
ccaa <- esp_get_ccaa(
- moveCAN = FALSE, resolution = res) |>
+ moveCAN = FALSE, resolution = res
+) |>
st_transform(3035)
# Plot
@@ -275,7 +266,6 @@ ggplot(all_countries) +
linetype = "dotted"
)
)
-
```
## Working with tiles
diff --git a/vignettes/thematic-1.png b/vignettes/thematic-1.png
index 42fdfc68..ba43d3a2 100644
Binary files a/vignettes/thematic-1.png and b/vignettes/thematic-1.png differ