-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweek6.qmd
1363 lines (857 loc) · 25.4 KB
/
week6.qmd
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Geospatial data analysis in R"
subtitle: "Raster data I"
author: "Josh Merfeld"
institute: "KDI School"
date: "`r Sys.Date()`"
date-format: long
format:
revealjs:
self-contained: true
slide-number: false
progress: false
theme: [serif, custom.scss]
width: 1500
height: 1500*(9/16)
code-copy: true
code-fold: show
code-overflow: wrap
highlight-style: github
execute:
echo: true
warnings: false
---
```{r}
#| label: setup
#| include: false
knitr::knit_hooks$set(crop = knitr::hook_pdfcrop)
library(tidyverse)
library(sf)
library(terra)
library(tidyterra)
library(cowplot)
library(kableExtra)
library(haven)
kdisgreen <- "#006334"
accent <- "#340063"
accent2 <- "#633400"
kdisgray <- "#A7A9AC"
```
## Rasters
- We've discussed shapefiles
- Now let's talk about rasters!<br><br>
- Rasters are a different type of geospatial data
- They are made up of a grid of cells
- Each cell has a value
## A raster with `terra`
```{r}
#| echo: false
#| include: true
#| fig-align: center
#| crop: true
# create example grid
pikachu <- rast("week6assets/pikachu.png")
ggplot() +
geom_spatraster_rgb(data = pikachu) +
theme_void() +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")) +
theme(legend.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
```
## Let's zoom in!
## Let's zoom in!
```{r}
#| echo: false
#| include: true
#| fig-align: center
#| crop: true
# create example grid
pikachu1 <- rast("week6assets/pikachu1.png")
pikachu2 <- rast("week6assets/pikachu2.png")
g1 <- ggplot() +
geom_spatraster_rgb(data = pikachu1) +
theme_void() +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")) +
theme(legend.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
g2 <- ggplot() +
geom_spatraster_rgb(data = pikachu2) +
theme_void() +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")) +
theme(legend.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
plot_grid(g1, g2)
```
## Example raster grid - how much info do we need?
```{r}
#| echo: false
#| include: true
#| fig-align: center
#| crop: true
# create example grid
main <- ggplot() +
geom_hline(yintercept = 1:10, color = "black") +
geom_vline(xintercept = 1:10, color = "black") +
theme_bw() +
labs(x = "X", y = "Y") +
scale_x_continuous(breaks = 1:10, minor_breaks = NULL) +
scale_y_continuous(breaks = 1:10, minor_breaks = NULL) +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")) +
theme(legend.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
gridexample <- ggdraw() +
draw_plot(
{
main +
coord_sf(
xlim = c(0.99, 10.01),
ylim = c(0.99, 10.01),
expand = FALSE) +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")) +
theme(legend.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
}
)
gridexample +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")) +
theme(legend.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
```
- Here's a grid.
- How many points do we need?
## Example raster grid - how much info do we need?
```{r}
#| echo: false
#| include: true
#| crop: true
#| fig-align: center
gridexample +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")) +
theme(legend.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
```
- Need to know location of one grid cell...
- And the size of each grid!
## How much info do we need?
- In other words, we do not need a point for every raster cell<br><br>
- We just need to know:
- The location of one cell
- The size of each cell
- This is called the `resolution` of the raster<br><br>
- Example:
- I know the first grid cell in bottom left is at (0, 0)
- I know each grid cell is 1 meter by 1 meter (the resolution)
- Then I know the exact location of every single grid cell
## Population in Cotonou, Benin
```{r}
#| echo: false
#| include: true
#| crop: true
#| fig-align: center
library(tidyterra)
tif <- rast("week6data/beninpop.tif")
ggplot() +
geom_spatraster(data = tif, ) +
scale_fill_distiller("Population\ncount", palette = "Spectral", na.value = "white") +
theme_bw() +
labs(subtitle = "Population in Cotonou, Benin") +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")) +
theme(legend.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
```
- What are the white values?
## Population in Cotonou, Benin
- Here's the information for this raster
- What's the resolution? What are the units?
```{r}
#| echo: false
#| include: true
#| fig-align: center
#| crop: true
tif
```
## Rasters
- Rasters are defined by the grid layout and the resolution
- Grid cells are sometimes called pixels (just like images, which are often rasters!)<br><br>
- There are many different file types for rasters
- `.tif` or `.tiff` (one of the most common)
- `.nc` (NetCDF, common for very large raster data)
- Image files, e.g. `png`, `jpg`, etc.<br><br>
## Reading rasters in R
- Reading rasters is also quite easy!
- Going to use the `terra` package for it
- Note: can use `terra` for shapefiles, too
- `week6data/beninpop.tif` is a raster of population counts in Benin
```{r}
#| echo: true
#| include: true
#| fig-align: center
#| crop: true
library(terra)
# this is the raster for Cotonou, Benin
cotonou <- rast("week6data/beninpop.tif")
cotonou
```
## Plotting rasters
::: columns
::: {.column width="55%"}
- Plotting rasters only with `terra` is a bit of a pain
- Can't use `ggplot`
- So, I load another package that lets me use `ggplot` with rasters
- `tidyterra`
```{r}
#| echo: true
#| eval: false
#| fig-align: center
#| crop: true
library(tidyterra)
ggplot() +
geom_spatraster(data = cotonou) +
theme_bw()
```
:::
::: {.column width="45%"}
```{r}
#| echo: false
#| include: true
#| fig-align: center
#| crop: true
library(tidyterra)
ggplot() +
geom_spatraster(data = cotonou) +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")) +
theme(legend.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
```
:::
:::
## Making it nicer
::: columns
::: {.column width="55%"}
```{r}
#| echo: true
#| eval: false
#| fig-align: center
#| crop: true
library(tidyterra)
ggplot() +
geom_spatraster(data = cotonou) +
# distiller is for continuous values
# but we can use palettes!
# I like spectral a lot
scale_fill_distiller("Population\ncount",
palette = "Spectral", na.value = "white") +
theme_bw() +
labs(subtitle = "Population in Cotonou, Benin")
```
:::
::: {.column width="45%"}
```{r}
#| echo: false
#| include: true
#| fig-align: center
#| crop: true
library(tidyterra)
ggplot() +
geom_spatraster(data = cotonou) +
scale_fill_distiller("Population\ncount", palette = "Spectral", na.value = "white") +
theme_bw() +
labs(subtitle = "Population in Cotonou, Benin") +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")) +
theme(legend.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
```
:::
:::
## Extracting raster data to shapefiles
- Let's go back to our use case:
- We want to estimate a sub-area model at the EA level in Malawi
- This means we need to extract raster data to the EA level
- We can do this with `terra`, `sf`, and `exactextractr`
- `terra` has its own method, but i find `exactextractr` to be MUCH faster<br><br>
- Let's start by looking at the raster I've uploaded to the `week6data`: `mwpop.tif`
## Give it a try
::: columns
::: {.column width="55%"}
- Try to load it into R using terra, then plot it with tidyterra and ggplot
```{r}
#| echo: true
#| eval: false
#| fig-align: center
#| code-fold: show
#| crop: true
tif <- rast("week6data/mwpop.tif")
ggplot() +
geom_spatraster(data = tif) +
scale_fill_distiller("Population\ncount",
palette = "Spectral", na.value = "white") +
theme_bw() +
labs(subtitle = "Population in Northern Malawi")
```
:::
::: {.column width="45%"}
```{r}
#| echo: false
#| eval: true
#| fig-align: center
#| code-fold: show
#| crop: true
tif <- rast("week6data/mwpop.tif")
ggplot() +
geom_spatraster(data = tif) +
scale_fill_distiller("Population\ncount",
palette = "Spectral", na.value = "white") +
theme_bw() +
labs(subtitle = "Population in Northern Malawi") +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")) +
theme(legend.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
```
:::
:::
## Give it a try
::: columns
::: {.column width="55%"}
- I actually don't like that map! It's too hard to see because of all the low values.
- So let's take logs, instead!
- Note that all the zeros become missing (can't log zero)
```{r}
#| echo: true
#| eval: false
#| fig-align: center
#| code-fold: show
#| crop: true
tif <- rast("week6data/mwpop.tif")
ggplot() +
geom_spatraster(data = log(tif)) +
scale_fill_distiller("Population\ncount (log)",
palette = "Spectral", na.value = "white") +
theme_bw() +
labs(subtitle = "Population in Northern Malawi")
```
:::
::: {.column width="45%"}
```{r}
#| echo: false
#| eval: true
#| fig-align: center
#| code-fold: show
#| crop: true
tif <- rast("week6data/mwpop.tif")
ggplot() +
geom_spatraster(data = log(tif)) +
scale_fill_distiller("Population\ncount (log)",
palette = "Spectral", na.value = "white") +
theme_bw() +
labs(subtitle = "Population in Northern Malawi") +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")) +
theme(legend.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
```
:::
:::
## We want to extract the .tif values to the .shp
```{r}
#| echo: false
#| eval: true
#| fig-align: center
#| crop: true
tif <- rast("week6data/mwpop.tif")
adm4 <- read_sf("week6data/mw4.shp")
g1 <- ggplot() +
geom_spatraster(data = tif) +
scale_fill_distiller("Population\ncount (log)",
palette = "Spectral", na.value = "white") +
theme_bw() +
labs(subtitle = "Population in Northern Malawi") +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")) +
theme(legend.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
g2 <- ggplot() +
geom_sf(data = adm4, color = "black", fill = "transparent", lwd = 0.1) +
theme_bw() +
labs(subtitle = "EAs (admin4) Northern Malawi") +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")) +
theme(legend.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
plot_grid(g1, NA, g2, ncol = 3, rel_widths = c(1, 0.05, 1)) +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")) +
theme(legend.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
```
## Let's do it with `exactextractr`
```{r}
#| echo: true
#| eval: false
#| include: true
#| fig-align: center
#| crop: true
library(exactextractr)
tif <- rast("week6data/mwpop.tif")
adm4 <- read_sf("week6data/mw4.shp")
# make sure they are in the same CRS! (they already are, but just in case)
# st_transform is for the sf object
adm4 <- st_transform(adm4, crs = crs(tif))
# extract the raster values to the shapefile
# we are going to SUM, and add the EA_CODE from the shapefile to the result
extracted <- exact_extract(tif, adm4, fun = "sum", append_cols = "EA_CODE")
```
```{r}
#| echo: false
#| eval: true
#| include: false
#| fig-align: center
#| crop: true
library(exactextractr)
tif <- rast("week6data/mwpop.tif")
adm4 <- read_sf("week6data/mw4.shp")
# make sure they are in the same CRS! (they already are, but just in case)
# st_transform is for the sf object
adm4 <- st_transform(adm4, crs = crs(tif))
# extract the raster values to the shapefile
# we are going to SUM, and add the EA_CODE from the shapefile to the result
extracted <- exact_extract(tif, adm4, fun = "sum", append_cols = "EA_CODE")
# save it!
write_csv(extracted |> rename(pop = sum), "week6data/mwpopEAs.csv")
```
```{r}
#| echo: true
#| eval: true
#| include: true
#| fig-align: center
#| crop: true
head(extracted)
```
## Now we can join the extracted data to the shapefile
::: columns
::: {.column width="55%"}
```{r}
#| echo: true
#| eval: false
#| include: true
#| fig-align: center
#| crop: true
# join
adm4 <- adm4 |>
left_join(extracted, by = "EA_CODE")
# plot it!
ggplot() +
geom_sf(data = adm4, aes(fill = sum),
color = "black", lwd = 0.01) +
scale_fill_distiller("Population\ncount",
palette = "Spectral", na.value = "white") +
theme_bw() +
labs(subtitle = "Population in EAs") +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")) +
theme(legend.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
```
:::
::: {.column width="45%"}
![](week6assets/mwpopEAs.png){fig-align="center"}
:::
:::
## We can also do it with `terra`
```{r}
#| echo: true
#| eval: true
#| include: true
#| fig-align: center
#| crop: true
tif <- rast("week6data/mwpop.tif")
adm4 <- vect("week6data/mw4.shp")
# make sure they are in the same CRS! (they already are, but just in case)
# st_transform is for the sf object
adm4 <- terra::project(adm4, crs(tif))
# we are going to SUM
# just include EA_CODE
extracted <- terra::extract(tif, adm4, fun = "sum")
```
```{r}
#| echo: true
#| eval: true
#| include: true
#| fig-align: center
#| crop: true
head(extracted)
```
## It's in the same order!
```{r}
#| echo: true
#| eval: true
#| include: true
#| fig-align: center
#| crop: true
adm4$pop <- extracted$mwpop
```
```{r}
#| echo: true
#| eval: true
#| include: true
#| fig-align: center
#| crop: true
adm4
```
## Now it's your turn
- Here's your task:
- Search for "worldpop population counts"
- Should be the first result (link: [https://hub.worldpop.org/project/categories?id=3](https://hub.worldpop.org/project/categories?id=3))
- Scroll down the page, click on "unconstrained individual countries 2000-2020 UN adjusted (1km resolution)
![](week6assets/worldpoppage1.png){fig-align="center"}
## Now it's your turn
- Here's your task:
- Search for "worldpop population counts"
- Should be the first result (link: [https://hub.worldpop.org/project/categories?id=3](https://hub.worldpop.org/project/categories?id=3))
- Scroll down the page, click on "unconstrained individual countries 2000-2020 UN adjusted (1km resolution)
- Then, search for a country (maybe yours?)
![](week6assets/worldpoppage2.png){fig-align="center"}
## Now it's your turn
- Here's your task:
- Search for "worldpop population counts"
- Should be the first result (link: [https://hub.worldpop.org/project/categories?id=3](https://hub.worldpop.org/project/categories?id=3))
- Scroll down the page, click on "unconstrained individual countries 2000-2020 UN adjusted (1km resolution)
- Then, search for a country (maybe yours?)
- Click on "Data & Resources" for 2020
- Scroll down to the bottom of the page and download the .tif
## Now it's your turn
- Load the .tif into R using `terra`
- Plot the raster using `tidyterra` and `ggplot`
- Make it look nice!
## Let's keep going!
- Now you need to find a shapefile for the same country
- This will be a bit less straightforward
- Search for "shapefile COUNTRY humdata"
- You should find a link to the Humanitarian Data Exchange
- Click on it and see if it has shapefiles for your country of choice
- If so, download a shapefile (it can be at a higher admin level)
- If not, raise your hand and I'll come help you find a shapefile
- Load it into R and plot it!
## One last thing
- You have the population tif and the shapefile
- Extract the population data (using sum, don't forget!) to the shapefile
- Use `append_cols` and make sure you choose the correct identifier!
- Join the data to the shapefile
- Plot the shapefile with the population data
- Make it look nice!
## What can you do with that data?
- Now you have a shapefile with population data
- You can save it as a `.csv` and use it in your analysis!
- `write_csv(NAME, PATH)`
## Creating a grid
- Yesterday, we used a grid in Korea
- kgrid.shp
- By now, you can probably see that a grid is very similar to a raster!
## Load the shapefile
- Let's load kshape.shp
```{r}
#| echo: true
#| eval: true
#| include: true
#| fig-align: center
#| crop: true
kshape <- vect("week6data/kshape.shp")
kgrid <- rast(kshape, res = 5000)
kgrid <- as.polygons(kgrid)
kgrid$id <- 1:nrow(kgrid)
```
## The grid
```{r}
#| echo: false
#| eval: true
#| include: true
#| fig-align: center
#| crop: true
ggplot() +
geom_spatvector(data = kgrid) +
theme_bw()
```
## Not quite done
- We aren't quite done. What do we want to do now?
. . .
```{r}
#| echo: true
#| eval: true
#| include: true
#| fig-align: center
#| crop: true
intersection <- intersect(kshape, kgrid)
kgrid <- kgrid |>
filter(id %in% intersection$id)
```
## Not quite done
```{r}
#| echo: false
#| eval: true
#| include: true
#| fig-align: center
#| crop: true
ggplot() + geom_spatvector(data = kgrid)
```
## Your turn!
- Find the population raster for Korea on Worldpop
. . .
- Extract the TOTAL population in each grid cell
. . .
- Plot it!
## Let's have a little fun with maps!
```{r}
#| echo: false
#| eval: true
#| include: true
#| fig-align: center
#| crop: true
library(gganimate)
mwrast <- rast("week6data/mwndvimonths.tif")
adm4 <- vect("week6data/mw4.shp")
mwrast <- terra::project(mwrast, crs(adm4))
extract <- terra::extract(mwrast, adm4, fun = "mean")
extract$EA_CODE <- adm4$EA_CODE
extract <- extract[,-1]
# pivot longer
extract <- extract |> pivot_longer(cols = -EA_CODE, names_to = "month", values_to = "ndvi")
extract <- extract |>
filter(month %in% month.abb[seq(from = 1, to = 9, by = 1)])
extract$monthint <- match(extract$month, month.abb)
adm4 <- adm4 |>
left_join(extract, by = "EA_CODE")
ggplot(adm4) +
geom_spatvector(aes(fill = ndvi), color = NA) +
scale_fill_distiller("NDVI", palette = "RdYlGn",
na.value = "white", direction = 1) +
theme_bw() +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")) +
theme(legend.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")) +
# Here comes the gganimate code
transition_manual(
frames = monthint,
cumulative = FALSE
) +
labs(title = "Month: {(adm4 |> filter(monthint==current_frame))$month[1]}")
```
## How do we do this?
```{r}
#| echo: true
#| eval: true
#| include: true
#| fig-align: center
#| crop: true
# load raster
mwrast <- rast("week6data/mwndvimonths.tif")
# vector
adm4 <- vect("week6data/mw4.shp")
# project
mwrast <- terra::project(mwrast, crs(adm4))
# extract
extract <- terra::extract(mwrast, adm4, fun = "mean")
head(extract)
```