-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathREADME.Rmd
408 lines (316 loc) · 12.5 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
---
output: github_document
always_allow_html: true
editor_options:
markdown:
wrap: 80
---
```{r, include = FALSE}
knitr::opts_knit$set(
progress = TRUE,
base.url = "https://raw.githubusercontent.com/ropenspain/mapSpain/main/"
)
knitr::opts_chunk$set(
collapse = TRUE,
tidy = "styler",
comment = "#>",
fig.path = "img/README-",
warning = FALSE,
message = FALSE,
dev = "ragg_png",
dpi = 300,
out.width = "100%"
)
```
# mapSpain <img src="man/figures/logo.png" align="right" width="120"/>
<!-- badges: start -->
[](https://ropenspain.es/)
[](https://CRAN.R-project.org/package=mapSpain)
[](https://cran.r-project.org/web/checks/check_results_mapSpain.html)
[](https://CRAN.R-project.org/package=mapSpain)
[](https://ropenspain.r-universe.dev/mapSpain)
[](https://github.com/rOpenSpain/mapSpain/actions/workflows/check-full.yaml)
[](https://github.com/rOpenSpain/mapSpain/actions/workflows/rhub.yaml)
[](https://app.codecov.io/gh/rOpenSpain/mapSpain)
[](https://doi.org/10.5281/zenodo.5366622)
[](https://www.repostatus.org/#active)
[](https://CRAN.R-project.org/package=mapSpain)
<!-- badges: end -->
[**mapSpain**](https://ropenspain.github.io/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](https://rstudio.github.io/leaflet/), that loads several base maps 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**](https://CRAN.R-project.org/package=mapSpain):
```{r, eval = FALSE}
install.packages("mapSpain", dependencies = TRUE)
```
You can install the developing version of **mapSpain** using the
[r-universe](https://ropenspain.r-universe.dev/mapSpain):
```{r, eval = FALSE}
# Install mapSpain in R:
install.packages("mapSpain",
repos = c(
"https://ropenspain.r-universe.dev",
"https://cloud.r-project.org"
),
dependencies = TRUE
)
```
Alternatively, you can install the developing version of **mapSpain** with:
```{r, eval = FALSE}
remotes::install_github("rOpenSpain/mapSpain", dependencies = TRUE)
```
## Usage
This script highlights some features of **mapSpain** :
```{r static}
library(mapSpain)
library(sf)
library(dplyr)
census <- mapSpain::pobmun19
# Extract CCAA from base dataset
codelist <- mapSpain::esp_codelist %>%
select(cpro, codauto) %>%
distinct()
census_ccaa <- census %>%
left_join(codelist) %>%
# Summarize by CCAA
group_by(codauto) %>%
summarise(pob19 = sum(pob19), men = sum(men), women = sum(women)) %>%
mutate(
porc_women = women / pob19,
porc_women_lab = paste0(round(100 * porc_women, 2), "%")
)
# Merge into spatial data
ccaa_sf <- esp_get_ccaa() %>%
left_join(census_ccaa)
can <- esp_get_can_box()
# Plot with ggplot
library(ggplot2)
ggplot(ccaa_sf) +
geom_sf(aes(fill = porc_women), color = "grey70", linewidth = .3) +
geom_sf(data = can, color = "grey70") +
geom_sf_label(aes(label = porc_women_lab),
fill = "white", alpha = 0.5,
size = 3, label.size = 0
) +
scale_fill_gradientn(
colors = hcl.colors(10, "Blues", rev = TRUE),
n.breaks = 10, labels = scales::label_percent(),
guide = guide_legend(title = "Porc. women", position = "inside")
) +
theme_void() +
theme(legend.position.inside = c(0.1, 0.6))
```
You can combine `sf` objects with static tiles
```{r tile}
# Get census
census <- mapSpain::pobmun19 %>%
mutate(porc_women = women / pob19) %>%
select(cpro, cmun, porc_women)
# Get shapes
shape <- esp_get_munic_siane(region = "Segovia", epsg = 3857)
provs <- esp_get_prov_siane(epsg = 3857)
shape_pop <- shape %>% left_join(census)
tile <- esp_getTiles(shape_pop, type = "IDErioja.Relieve", zoommin = 1)
# Plot
library(ggplot2)
library(tidyterra)
lims <- as.vector(terra::ext(tile))
ggplot(remove_missing(shape_pop, na.rm = TRUE)) +
geom_spatraster_rgb(data = tile, maxcell = 10e6) +
geom_sf(aes(fill = porc_women), color = NA) +
geom_sf(data = provs, fill = NA) +
scale_fill_gradientn(
colours = hcl.colors(10, "RdYlBu", alpha = .5),
n.breaks = 8,
labels = function(x) {
sprintf("%1.0f%%", 100 * x)
},
guide = guide_legend(title = "", )
) +
coord_sf(
xlim = lims[c(1, 2)],
ylim = lims[c(3, 4)],
expand = FALSE
) +
labs(
title = "Share of women in Segovia by town (2019)",
caption = "Source: INE, CC BY 4.0 www.iderioja.org"
) +
theme_void() +
theme(
title = element_text(face = "bold")
)
```
## mapSpain and giscoR
If you need to plot Spain along with another countries, consider using
[**giscoR**](https://ropengov.github.io/giscoR/) package, that is installed as a
dependency when you installed **mapSpain**. A basic example:
```{r giscoR, fig.asp=1}
library(giscoR)
# Set the same resolution for a perfect fit
res <- "20"
all_countries <- gisco_get_countries(resolution = res) %>%
st_transform(3035)
eu_countries <- gisco_get_countries(resolution = res, region = "EU") %>%
st_transform(3035)
ccaa <- esp_get_ccaa(moveCAN = FALSE, resolution = res) %>%
st_transform(3035)
library(ggplot2)
ggplot(all_countries) +
geom_sf(fill = "#DFDFDF", color = "#656565") +
geom_sf(data = eu_countries, fill = "#FDFBEA", color = "#656565") +
geom_sf(data = ccaa, fill = "#C12838", color = "grey80", linewidth = .1) +
# Center in Europe: EPSG 3035
coord_sf(xlim = c(2377294, 7453440), ylim = c(1313597, 5628510)) +
theme(
panel.background = element_blank(),
panel.grid = element_line(colour = "#DFDFDF", linetype = "dotted")
)
```
## 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:
```{r, eval = FALSE}
esp_set_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:
- [**tmap**](https://github.com/r-tmap/tmap)
- [**mapsf**](https://riatelab.github.io/mapsf/)
- [**ggplot2**](https://github.com/tidyverse/ggplot2) +
[**tidyterra**](https://github.com/dieghernan/tidyterra).
- [**leaflet**](https://rstudio.github.io/leaflet/)
## Citation
```{r echo=FALSE, results='asis'}
print(citation("mapSpain"), style = "html")
```
A BibTeX entry for LaTeX users is:
```{r echo=FALSE, comment=''}
toBibtex(citation("mapSpain"))
```
## Contribute
Check the GitHub page for [source
code](https://github.com/ropenspain/mapSpain/).
## Copyright notice
This package uses data from CartoBase SIANE, provided by Instituto Geográfico
Nacional:
> Atlas Nacional de España (ANE) [CC BY
> 4.0](https://creativecommons.org/licenses/by/4.0/deed.en)
> [ign.es](https://www.ign.es/)
See <https://github.com/rOpenSpain/mapSpain/tree/sianedata>
This package uses data from **GISCO**. GISCO
[(FAQ)](https://ec.europa.eu/eurostat/web/gisco) is a geospatial open data
repository including several data sets at several resolution levels.
*From GISCO \> Geodata \> Reference data \> Administrative Units / Statistical
Units*
> When data downloaded from this page is used in any printed or electronic
> publication, in addition to any other provisions applicable to the whole
> Eurostat website, data source will have to be acknowledged in the legend of
> the map and in the introductory page of the publication with the following
> copyright notice:
>
> EN: © EuroGeographics for the administrative boundaries
>
> FR: © EuroGeographics pour les limites administratives
>
> DE: © EuroGeographics bezüglich der Verwaltungsgrenzen
>
> For publications in languages other than English, French or German, the
> translation of the copyright notice in the language of the publication shall
> be used.
If you intend to use the data commercially, please contact EuroGeographics for
information regarding their license agreements.
## Contributors
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->
All contributions to this project are gratefully acknowledged using the [`allcontributors` package](https://github.com/ropensci/allcontributors) following the [allcontributors](https://allcontributors.org) specification. Contributions of any kind are welcome!
### Code
<table class="table allctb-table" >
<tr>
<td align="center">
<a href="https://github.com/dieghernan">
<img src="https://avatars.githubusercontent.com/u/25656809?v=4" width="100px;" class="allctb-avatar" alt=""/>
</a><br>
<a href="https://github.com/rOpenSpain/mapSpain/commits?author=dieghernan">dieghernan</a>
</td>
</tr>
</table>
### Issue Authors
<table class="table allctb-table" >
<tr>
<td align="center">
<a href="https://github.com/jesbrz">
<img src="https://avatars.githubusercontent.com/u/19475313?v=4" width="100px;" class="allctb-avatar" alt=""/>
</a><br>
<a href="https://github.com/rOpenSpain/mapSpain/issues?q=is%3Aissue+author%3Ajesbrz">jesbrz</a>
</td>
<td align="center">
<a href="https://github.com/pedrotercero3">
<img src="https://avatars.githubusercontent.com/u/90156958?v=4" width="100px;" class="allctb-avatar" alt=""/>
</a><br>
<a href="https://github.com/rOpenSpain/mapSpain/issues?q=is%3Aissue+author%3Apedrotercero3">pedrotercero3</a>
</td>
<td align="center">
<a href="https://github.com/ajcanepa">
<img src="https://avatars.githubusercontent.com/u/10628672?u=f474ccb4da200b642706382fa2a7e946454af9ab&v=4" width="100px;" class="allctb-avatar" alt=""/>
</a><br>
<a href="https://github.com/rOpenSpain/mapSpain/issues?q=is%3Aissue+author%3Aajcanepa">ajcanepa</a>
</td>
<td align="center">
<a href="https://github.com/fgoerlich">
<img src="https://avatars.githubusercontent.com/u/6486324?v=4" width="100px;" class="allctb-avatar" alt=""/>
</a><br>
<a href="https://github.com/rOpenSpain/mapSpain/issues?q=is%3Aissue+author%3Afgoerlich">fgoerlich</a>
</td>
<td align="center">
<a href="https://github.com/perezcalderon">
<img src="https://avatars.githubusercontent.com/u/8152544?v=4" width="100px;" class="allctb-avatar" alt=""/>
</a><br>
<a href="https://github.com/rOpenSpain/mapSpain/issues?q=is%3Aissue+author%3Aperezcalderon">perezcalderon</a>
</td>
<td align="center">
<a href="https://github.com/Cidree">
<img src="https://avatars.githubusercontent.com/u/96820235?u=473cfd139b57545c3f9fc8ddb070ac1909db7b22&v=4" width="100px;" class="allctb-avatar" alt=""/>
</a><br>
<a href="https://github.com/rOpenSpain/mapSpain/issues?q=is%3Aissue+author%3ACidree">Cidree</a>
</td>
<td align="center">
<a href="https://github.com/catbru">
<img src="https://avatars.githubusercontent.com/u/2419189?u=d8fd560c3e349236450bdd9669f9d2ef1176d8d9&v=4" width="100px;" class="allctb-avatar" alt=""/>
</a><br>
<a href="https://github.com/rOpenSpain/mapSpain/issues?q=is%3Aissue+author%3Acatbru">catbru</a>
</td>
<td align="center">
<a href="https://github.com/ana-m-m">
<img src="https://avatars.githubusercontent.com/u/78867570?u=e82579f7b35ca989a167342ad18e5c003980943a&v=4" width="100px;" class="allctb-avatar" alt=""/>
</a><br>
<a href="https://github.com/rOpenSpain/mapSpain/issues?q=is%3Aissue+author%3Aana-m-m">ana-m-m</a>
</td>
</tr>
</table>
### Issue Contributors
<table class="table allctb-table" >
<tr>
<td align="center">
<a href="https://github.com/mpizarrotig">
<img src="https://avatars.githubusercontent.com/u/18368413?u=a85f35a53cf336f532b6e939b68ebf430669d2f5&v=4" width="100px;" class="allctb-avatar" alt=""/>
</a><br>
<a href="https://github.com/rOpenSpain/mapSpain/issues?q=is%3Aissue+commenter%3Ampizarrotig">mpizarrotig</a>
</td>
</tr>
</table>
<!-- markdownlint-enable -->
<!-- prettier-ignore-end -->
<!-- ALL-CONTRIBUTORS-LIST:END -->