-
Notifications
You must be signed in to change notification settings - Fork 2
/
README.Rmd
205 lines (143 loc) · 5.38 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
---
output: github_document
always_allow_html: true
---
```{r, include = FALSE}
knitr::opts_chunk$set(collapse = TRUE,
tidy = "styler",
comment = "#>",
fig.path = "man/figures/README-")
knitr::knit_hooks$set(
margin = function(before, options, envir) {
if (before) {
par(mar = c(0, 0, 0, 0))
}
}
)
```
# mapSpain <img src="man/figures/logo.png" align="right" width="120" />
<!-- badges: start -->
![R-CMD-check](https://github.com/dieghernan/mapSpain/workflows/R-CMD-check/badge.svg)
[![codecov](https://codecov.io/gh/dieghernan/mapSpain/branch/master/graph/badge.svg)](https://codecov.io/gh/dieghernan/mapSpain)
[![lifecycle](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://www.tidyverse.org/lifecycle/#experimental)
<!-- badges: end -->
**[mapSpain](https://dieghernan.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 basemap of public institutions of Spain, and the ability of downloading and processing static tiles.
Full site with examples and vignettes on <https://dieghernan.github.io/mapSpain/>
## Installation
You can install the developing version of `mapSpain` with:
``` r
library(remotes)
install_github("dieghernan/mapSpain")
```
## Usage
This script highlights some features of `mapSpain`:
```{r static}
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")
choroLayer(
CCAA.sf,
var = "porc_women",
breaks = br,
legend.values.rnd = 3,
legend.pos = "bottomright",
col = hcl.colors(
length(br) - 1,
palette = "Blues 3",
rev = TRUE,
alpha = 0.8
),
border = NA,
legend.title.txt = "% women"
)
# Add lines
CCAAbox <- esp_get_can_box()
plot(CCAAbox, add = TRUE)
```
You can combine `POLYGONS` with static tiles
```{r tile, warning=FALSE, fig.asp=0.7}
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 with hillshade
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:
```r
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:
* [`tmap`](https://mtennekes.github.io/tmap/)
* [`cartography`](http://riatelab.github.io/cartography/docs/)
* [`ggplot2`](https://github.com/tidyverse/ggplot2) + [`ggspatial`](https://github.com/paleolimbot/ggspatial)
* [`leaflet`](https://rstudio.github.io/leaflet/)
## Contribute
Check the Github page for [source code](https://github.com/dieghernan/mapSpain/).
## Copyright notice
This package uses data from **GISCO**. GISCO [(FAQ)](https://ec.europa.eu/eurostat/web/gisco/faq) 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 licence agreements.