-
Notifications
You must be signed in to change notification settings - Fork 1
/
worksheet.R
242 lines (134 loc) · 4.08 KB
/
worksheet.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
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
# Introduction to geospatial data analysis in R - Worksheet
#### Explore a vector dataset ####
library(sf)
# Load MRC data
mrc <- ...(..., stringsAsFactors = FALSE)
# Display vector layer
plot(...)
plot(..., axes = TRUE)
#### Exercise 1 ####
# Select the MRCs in the Bas-St-Laurent (*reg_id*: 01) and Gaspesie (*reg_id*: 11)
# regions, then display their 2016 population on a map.
# Hint: The operator %in% can check if a variable has one value within a set,
# for example x %in% c(1, 3) returns TRUE if x is equal to 1 or 3.
# Integration with the dplyr package
library(dplyr)
# Name and population for MRCs with population over 200,000
filter(...) %>%
select(...)
# Sum population by region
regions <- group_by(...) %>%
summarize(...)
plot(...)
# Create a spatial object from a data frame
plots <- read.csv(..., stringsAsFactors = FALSE)
plots <- st_as_sf(plots, ...)
plot(...)
#### Coordinate reference systems and transformations ####
mrc_proj <- ...(mrc, ...)
plot(mrc_proj["geometry"], axes = TRUE)
# Add graticule
plot(mrc_proj["geometry"], axes = TRUE, ...)
#### Customize maps with ggplot2 ####
library(ggplot2)
# Bar chart of forest inventory plots by height class and cover type
ggplot(data = ..., aes(... = height_cls, ... = cover_type)) +
...() +
labs(title = "Forest inventory plots", x = "Height class",
y = "Count", fill = "Cover type")
# Simple map
ggplot(...) +
...()
# Multiple layers
ggplot() +
geom_sf(data = ...) +
geom_sf(data = ..., aes(...), size = 1) +
theme_bw()
# Change coordinate systems
ggplot(data = plots) +
geom_sf() +
...
# Add labels, select section of map
ggplot(data = regions) +
geom_sf(aes(fill = pop2016)) +
geom_sf_label(aes(...)) +
coord_sf(...)
#### Exercise 2 ####
# Create a map of the MRCs with different fill colors for each region.
#### Geometric operations on vector data ####
areas <- ...
# Change units
units(areas) <- ...
# Which plots and MRCs intersect?
inters <- ...
# Spatial join
plots_mrc <- ...
mrc_01_11 <- mrc[...]
plots_01_11 <- ...
# Show on map
ggplot() +
geom_sf(data = mrc_01_11) +
geom_sf(data = plots_01_11) +
theme_bw()
#### Exercise 3 ####
# The shapefile "data/tbe2016_gaspe.shp" contains a map of areas defoliated
# by the spruce budworm in the Bas-St-Laurent and Gaspesie regions in 2016.
# The defoliation level is represented by an integer:
# 1 = Light, 2 = Moderate and 3 = Severe.
# a) How many forest inventory plots in these regions are affected
# at each defoliation level?
# Hint: The `table` function could be useful to get counts of each value in a column.
# b) Plot the defoliated areas located in the MRC of Kamouraska, along with the MRC border.
# Create a buffer around points
plots_proj <- st_transform(plots_01_11, crs = 6622)
plots_buffer <- ...
ggplot() +
geom_sf(data = plots_buffer, linetype = "dotted", fill = NA) +
geom_sf(data = plots_proj) +
theme_bw()
# Union of polygons
buffer_union <- ...
# Intersection and difference
mrc_01_11_proj <- st_transform(mrc_01_11, crs = 6622)
mrc_inters <- ...
mrc_diff <- ...
ggplot(mrc_inters) +
geom_sf() +
theme_bw()
ggplot(mrc_diff) +
geom_sf() +
theme_bw()
#### Raster datasets ####
library(stars)
# Load a raster
cdem <- ...("data/cdem_022BC_3s.tif")
# Plot a raster
plot(...)
ggplot() +
geom_stars(...) +
geom_sf(...) +
scale_fill_viridis_c() +
coord_sf(xlim = c(-70, -66), ylim = c(48, 49)) +
theme_bw()
# Raster operations
# Crop by coordinate values
cdem_part <- filter(...)
# Crop by polygon (MRC of La Matapedia)
matap <- ...
matap <- ...
cdem_matap <- ...
# Raster math
cdem_km <- ...
cdem_500 <- ...
#### Exercise 4 ####
# a) Show a map of the points in the MRC of La Mitis
# with an elevation between 5 and 100 m.
# b) What is the highest elevation in that MRC?
#### Extract values from raster at points ####
library(raster)
# Convert from stars to raster format
cdem_r <- as(cdem, "Raster")
plots_elev <- ...
#### Interactive maps with mapview ####
library(mapview)
mapview(...)