/
esp_get_gridmap.R
171 lines (138 loc) · 3.64 KB
/
esp_get_gridmap.R
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
#' Get a \CRANpkg{sf} hexbin or squared polygon of Spain
#'
#' @description
#' Loads a hexbin map (\CRANpkg{sf} object) or a map of squares with the
#' boundaries of the provinces or autonomous communities of Spain.
#'
#' @rdname esp_get_gridmap
#' @name esp_get_gridmap
#'
#' @family political
#'
#' @return A \CRANpkg{sf} POLYGON object.
#'
#'
#' @export
#'
#' @inheritParams esp_get_prov
#'
#' @inheritParams esp_get_ccaa
#'
#'
#'
#' @details
#'
#' Hexbin or grid map has an advantage over usual choropleth maps.
#' In choropleths, a large polygon data looks more emphasized just because
#' of its size, what introduces a bias. Here with hexbin, each region is
#' represented equally dismissing the bias.
#'
#' You can use and mix names, ISO codes, "codauto"/"cpro" codes (see
#' [esp_codelist]) and NUTS codes of different levels.
#'
#' When using a code corresponding of a higher level (e.g.
#' `esp_get_prov("Andalucia")`) all the corresponding units of that level are
#' provided (in this case , all the provinces of Andalucia).
#'
#'
#' Results are provided in **EPSG:4258**, use [sf::st_transform()]
#' to change the projection.
#'
#'
#' @example inst/examples/esp_get_gridmap.R
#'
esp_get_hex_prov <- function(prov = NULL) {
region <- prov
data_sf <- esp_hexbin_prov
region <- unique(region)
if (!is.null(region)) {
region <- esp_hlp_all2prov(region)
region <- unique(region)
if (length(region) == 0) {
stop(
"region ",
paste0("'", region, "'", collapse = ", "),
" is not a valid name"
)
}
dfcpro <- mapSpain::esp_codelist
dfcpro <- unique(dfcpro[, c("nuts3.code", "cpro")])
cprocodes <- unique(dfcpro[dfcpro$nuts3.code %in% region, ]$cpro)
data_sf <- data_sf[data_sf$cpro %in% cprocodes, ]
}
# Order
data_sf <- data_sf[order(data_sf$codauto, data_sf$cpro), ]
return(data_sf)
}
#' @rdname esp_get_gridmap
#'
#' @export
esp_get_hex_ccaa <- function(ccaa = NULL) {
region <- ccaa
data_sf <- esp_hexbin_ccaa
region <- unique(region)
if (!is.null(region)) {
nuts_id <- esp_hlp_all2ccaa(region)
nuts_id <- unique(nuts_id)
if (length(nuts_id) == 0) {
stop(
"region ",
paste0("'", region, "'", collapse = ", "),
" is not a valid name"
)
}
data_sf <- data_sf[data_sf$nuts2.code %in% nuts_id, ]
}
# Order
data_sf <- data_sf[order(data_sf$codauto), ]
return(data_sf)
}
#' @rdname esp_get_gridmap
#'
#' @export
esp_get_grid_prov <- function(prov = NULL) {
region <- prov
data_sf <- esp_grid_prov
region <- unique(region)
if (!is.null(region)) {
region <- esp_hlp_all2prov(region)
region <- unique(region)
if (length(region) == 0) {
stop(
"region ",
paste0("'", region, "'", collapse = ", "),
" is not a valid name"
)
}
dfcpro <- mapSpain::esp_codelist
dfcpro <- unique(dfcpro[, c("nuts3.code", "cpro")])
cprocodes <- unique(dfcpro[dfcpro$nuts3.code %in% region, ]$cpro)
data_sf <- data_sf[data_sf$cpro %in% cprocodes, ]
}
# Order
data_sf <- data_sf[order(data_sf$codauto, data_sf$cpro), ]
return(data_sf)
}
#' @rdname esp_get_gridmap
#'
#' @export
esp_get_grid_ccaa <- function(ccaa = NULL) {
region <- ccaa
data_sf <- esp_grid_ccaa
region <- unique(region)
if (!is.null(region)) {
nuts_id <- esp_hlp_all2ccaa(region)
nuts_id <- unique(nuts_id)
if (length(nuts_id) == 0) {
stop(
"region ",
paste0("'", region, "'", collapse = ", "),
" is not a valid name"
)
}
data_sf <- data_sf[data_sf$nuts2.code %in% nuts_id, ]
}
# Order
data_sf <- data_sf[order(data_sf$codauto), ]
return(data_sf)
}