-
Notifications
You must be signed in to change notification settings - Fork 0
/
2019_creating_maps_with_R.Rmd
414 lines (251 loc) · 9.51 KB
/
2019_creating_maps_with_R.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
407
408
409
410
411
412
413
414
---
title: "Creating (Web) Maps with R"
author: "Stephen Roecker"
date: "`r Sys.Date()`"
output:
html_document:
number_sections: yes
toc: yes
toc_float:
collapsed: yes
smooth_scroll: no
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(warning=FALSE, message=FALSE, cache=TRUE)
```
# Abstract
Most data has a spatial dimension to it. Knowing 'where' the data is coming from is often as crucial as knowing the 'what', 'when' and 'who' dimensions of a given dataset. Therefore it should be no surprise that R has a rich suite of packages for constructing maps and analyzing spatial data. R's capability has grown so much over the years that it's functionality rivals many dedicated geographic information systems (GIS). During this Meetup the basics for managing and mapping spatial data with be introduced, using the following packages: sf, ggplot2, tmap, mapview and leaflet.
# Example datasets
How you plot your data in R depends on what format it's in. R has several different formats for managing spatial data (e.g. sp vs sf), and different formats are only compatible with certain plotting systems. Thankfully converting between different spatial formats is not difficult.
## maps R package
```{r example-maps}
library(maps)
map(database = "state")
map(database = "state", region = "indiana")
```
## County Boundaries
```{r example-county}
library(USAboundaries)
library(sf)
cnty <- us_counties()
cnty <- subset(cnty, !state_name %in% c("Alaska", "Hawaii"))
vars <- c("statefp", "countyfp")
plot(cnty[vars])
```
## USDA-NASS Corn Yield Data
https://github.com/potterzot/rnassqs
```{r example-nass, eval=FALSE}
devtools::install_github('potterzot/rnassqs')
library(rnassqs)
source("C:/Users/steph/Nextcloud/code/api_keys.R")
data(state)
st <- state.abb
corn_us <- lapply(st, function(x) {
cat("getting", x, as.character(Sys.time()), "\n")
tryCatch({
corn = nassqs_yield(
list("commodity_desc"="CORN",
"agg_level_desc"="COUNTY",
"state_alpha"=x
),
key = nass_key
)},
error = function(err) {
print(paste("Error occured: ",err))
return(NULL)
}
)
})
corn_us <- do.call("rbind", corn_us)
save(corn_us, file = "C:/workspace2/corn_us.RData")
write.csv(corn_us, file = "nass_corn_us.csv", row.names = FALSE)
```
```{r yield2}
load(file = "C:/Users/Stephen.Roecker/Nextcloud/data/corn_us.RData")
corn_yield <- subset(corn_us, short_desc == "CORN, GRAIN - YIELD, MEASURED IN BU / ACRE")
corn_yield <- within(corn_yield, {
Value = as.numeric(Value)
year = as.numeric(year)
state_name = NULL
state = state_alpha
})
cnty_corn <- merge(cnty, corn_yield,
by.x = c("state_abbr", "countyfp"),
by.y = c("state_alpha", "county_code"),
all.x = TRUE
)
corn_states <- c("IL", "IA", "IN", "MI", "MN", "MO", "NE", "OH", "SD", "ND", "WI")
library(dplyr)
library(ggplot2)
group_by(corn_yield, state_alpha, year) %>%
summarize(
yield_low = min(Value, na.rm = TRUE),
yield_median = median(Value, na.rm = TRUE),
yield_max = max(Value, na.rm = TRUE)
) %>%
filter(state_alpha %in% corn_states) %>%
mutate(state = state_alpha,
source = "NASS"
) %>%
ggplot() +
geom_line(aes(x = year, y = yield_median, col = source)) +
geom_ribbon(aes(x = year, ymin = yield_low, ymax = yield_max), alpha = 0.25) +
facet_wrap(~ state) +
ylab("yield per county (bu/acre)") +
ggtitle("USDA-NASS Corn Yields")
# geom_point(data = yld_sum[yld_sum$state %in% corn_states, ], aes(x = 2018, y = yield_med, col = "NASIS"), size = 1) +
# geom_ribbon(data = yld_sum2[yld_sum2$state %in% corn_states, ], aes(x = year, ymin = yield_low2, ymax = yield_max2, col = "NASIS"), alpha = 0.25) +
# geom_pointrange(data = yld_sum[yld_sum$state %in% corn_states, ], aes(x = 2018, y = yield_med, ymin = yield_low2, ymax = yield_max2, col = source))
```
## Indiana General Soil Map
```{r example-statsgo}
IN <- sf::read_sf(dsn = "D:/geodata/soils/soils_GSMCLIP_mbr_2599033_03/wss_gsmsoil_IN_[2006-07-06]/spatial/gsmsoilmu_a_in.shp", layer = "gsmsoilmu_a_in")
# simplify polygons
IN <- rmapshaper::ms_simplify(IN)
```
## Kellogg Soil Survey Laboratory Data
```{r example-kssl}
library(aqp)
library(soilDB)
# download lab locations for the Miami soil series
miami <- fetchKSSL("Miami")
miami <- site(miami)
miami <- subset(miami, complete.cases(x, y))
miami <- within(miami, {
lon = x
lat = y
})
head(miami)
```
# Construct and convert spatial objects
## point objects
```{r sp convert}
# construct
# sp object
library(sp)
miami_sp <- SpatialPointsDataFrame(
data = miami,
coords = cbind(miami$lon, miami$lat),
proj4string = CRS("+init=epsg:4326")
)
# sf object
library(sf)
miami_sf <- st_as_sf(
miami,
coords = c("lon", "lat"),
crs = 4326
)
# data structures
str(miami_sp, 2)
str(miami_sf)
# convert
miami_sf <- st_as_sf(miami_sp)
miami_sp <- as(miami_sf, "Spatial")
```
## Convert map objects
```{r map convert}
library(maptools)
st <- map("state", fill = TRUE, plot = FALSE)
# convert to sp object
st_sp <- map2SpatialPolygons(st, IDs = st$names)
proj4string(st_sp) <- CRS("+init=epsg:4326")
st_sp$state <- st$names
# loads data with ggplot2 package
st <- ggplot2::map_data("state")
```
## Convert with broom package
```{r broom}
library(broom)
st_tidy <- tidy(st_sp, region = "state")
IN_tidy <- tidy(as(IN, "Spatial"), region = "MUSYM")
```
# Example maps with several R packages
## [ggplot2](https://ggplot2.tidyverse.org/)
ggplot2 plots data frames, but can also use sf objects (which are a special case of data frames). It can plot rasters, but only if they are converted to a data frame.
```{r ggplot2}
library(ggplot2)
# Lines
ggplot() +
geom_point(data = miami, aes(x = lon, y = lat)) +
geom_path(data = st, aes(x = long, y = lat, group = group)) +
xlim(range(miami$lon)) +
ylim(range(miami$lat)) +
ggtitle("Location of Miami Lab Pedons")
# Polygons
ggplot() +
geom_polygon(data = st_tidy, aes(x = long, y = lat, group = group, fill = id)) +
coord_map(projection = "albers", lat0 = 39, lat1 = 45) +
# remove legend
guides(fill = FALSE)
# sf objects
# Polygons
ggplot() +
geom_sf(data = cnty, aes(fill = statefp, lty = NA)) +
geom_sf(data = miami_sf) +
coord_sf(crs = "+init=epsg:5070") +
guides(fill = FALSE)
# Facets
test <- subset(cnty_corn, year %in% 2012:2017)
ggplot() +
geom_sf(data = test, aes(fill = Value, lty = NA)) +
scale_fill_viridis_c(na.value = "transparent") +
facet_wrap(~ year) +
geom_path(data = st, aes(x = long, y = lat, group = group)) +
ggtitle(corn_yield$short_desc[1])
```
## [ggmap](https://github.com/dkahle/ggmap)
ggmap expands ggplot2 to download and plot base maps.
```{r ggmap}
library(ggmap)
# build bound box and get base map via ggmap
bb <- sf::st_bbox(IN)
bb <- make_bbox(lon = bb[c(3, 1)], lat = bb[c(2, 4)])
gmap <- get_map(bb, maptype = "terrain", source = "osm")
# Lines
ggmap(gmap) +
geom_path(data = IN_tidy, aes(x = long, y = lat, group = group))
# geom_sf(data = IN, fill = NA, inherit.aes = FALSE) +
# guides(fill = FALSE)
# geom_sf() doesn't work with ggmp, their is a systematic shift https://github.com/r-spatial/sf/issues/336
```
## [tmap](https://github.com/mtennekes/tmap)
The "t" in tmap stands for thematic, but tmap can also plot rasters natively. tmap's syntax is very similar to ggplot2, but with a few twists.
```{r tmap}
library(tmap)
tm_shape(IN) + tm_polygons("MUSYM", border.col = NULL) +
tm_shape(cnty) + tm_borders() +
tm_shape(miami_sf) + tm_dots() +
tm_legend(legend.outside = TRUE)
# interactive web map
tmap_mode("view")
tm_basemap("OpenStreetMap") +
tm_shape(IN) + tm_borders()
```
## [mapview](https://r-spatial.github.io/mapview/)
```{r mapview}
library(mapview)
cols <- RColorBrewer::brewer.pal(50, "Paired")
test <- mapview(IN, zcol = "MUSYM", lwd = 0, col.regions = cols) +
mapview(cnty, type = "l")
test
# export to html
mapshot(test, url = "C:/workspace2/test.html", selfcontained = FALSE)
```
## [leaflet](https://rstudio.github.io/leaflet/)
```{r leaflet}
library(leaflet)
test <- leaflet() %>%
addProviderTiles("Esri.WorldImagery", group = "Imagery") %>%
addPolygons(data = IN, fill = FALSE, color = "black", weight = 2)
test
# export to html
htmlwidgets::saveWidget(test, file = "C:/workspace2/test.html", selfcontained = FALSE)
```
# Additional Reading
Healy, K., 2018. Data Visualization: a practical introduction. Princeton University Press. [http://socviz.co/](http://socviz.co/)
Gimond, M., 2019. Intro to GIS and Spatial Analysis. [https://mgimond.github.io/Spatial/](https://mgimond.github.io/Spatial/)
Hijmans, R.J., 2019. Spatial Data Science with R. [https://rspatial.org/](https://rspatial.org/)
Lovelace, R., J. Nowosad, and J. Muenchow, 2019. Geocomputation with R. CRC Press. [https://bookdown.org/robinlovelace/geocompr/](https://bookdown.org/robinlovelace/geocompr/)
Pebesma, E., and R. Bivand, 2019. Spatial Data Science. [https://keen-swartz-3146c4.netlify.com/](https://keen-swartz-3146c4.netlify.com/)