-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweek7.qmd
1409 lines (826 loc) · 25.7 KB
/
week7.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 II"
author: "Josh Merfeld"
institute: "KDI School"
date: "11-05-2024"
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
message: false
---
```{r}
#| label: setup
#| include: false
knitr::knit_hooks$set(crop = knitr::hook_pdfcrop)
Sys.setenv("RETICULATE_PYTHON" = paste0(getwd(), "/classenv/bin/python"))
library(reticulate)
use_virtualenv(paste0(getwd(), "/classenv"), required=TRUE)
library(tidyverse)
library(terra)
library(tidyterra)
library(cowplot)
library(kableExtra)
library(haven)
kdisgreen <- "#006334"
accent <- "#340063"
accent2 <- "#633400"
kdisgray <- "#A7A9AC"
```
## What are we doing today?
- A short review of in-class lab 1
- Installing Python and Positron for the GEE API
## In-class lab 1
- Let's go over a few things from in-class lab 1
- Three things I want to cover:
- Making maps look nice
- Creating a spatial object from points
- Distances
## Making maps look nice
- let's look at the grid cells:
```{r}
#| echo: true
#| eval: false
grids <- vect("assignments/in-class1/data/seoulgrid.shp")
ggplot(grids) +
geom_spatvector(aes(fill = log(pop))) +
scale_fill_distiller("Pop (log)", palette = "YlOrRd", direction = 1) +
theme_bw()
```
## Making maps look nice
```{r}
#| echo: false
#| eval: true
#| crop: true
#| fig.align: center
grids <- vect("assignments/in-class1/data/seoulgrid.shp")
ggplot(grids) +
geom_spatvector(aes(fill = log(pop))) +
scale_fill_distiller("Pop (log)", palette = "YlOrRd", direction = 1) +
theme_bw() +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")) +
theme(legend.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
```
## Compare the previous map to this one:
```{r}
#| echo: true
#| eval: false
ggplot(grids) +
geom_spatvector(aes(fill = log(pop)), color = NA) +
scale_fill_distiller("Pop (log)", palette = "YlOrRd", direction = 1) +
theme_bw()
```
## Compare the previous map to this one:
```{r}
#| echo: false
#| eval: true
#| crop: true
#| fig.align: center
ggplot(grids) +
geom_spatvector(aes(fill = log(pop)), color = NA) +
scale_fill_distiller("Pop (log)", palette = "YlOrRd", direction = 1) +
theme_bw() +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")) +
theme(legend.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
```
## Together
```{r}
#| echo: false
#| eval: true
#| crop: true
#| fig.align: center
g1 <- ggplot(grids) +
geom_spatvector(aes(fill = log(pop))) +
scale_fill_distiller("Pop (log)", palette = "YlOrRd", direction = 1) +
theme_bw() +
labs(subtitle = "With borders") +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")) +
theme(legend.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
g2 <- ggplot(grids) +
geom_spatvector(aes(fill = log(pop)), color = NA) +
scale_fill_distiller("Pop (log)", palette = "YlOrRd", direction = 1) +
theme_bw() +
labs(subtitle = "Without borders") +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")) +
theme(legend.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
plot_grid(g1, g2) +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")) +
theme(legend.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
```
## Spatial objects from points
- Now let's create a spatial object from points
- Let's use the schools:
```{r}
#| echo: true
#| eval: true
df <- read_csv("assignments/in-class1/data/seoulpoints.csv")
head(df)
```
- What is the crs for the points themselves?
. . .
- They are in longitude/latitude (WGS 84): "EPSG:4326"
## Creating the object
- We have to specify the CRS based on what the POINTS represent, not what we want it to eventually be
- Then we can project it
```{r}
#| echo: true
#| eval: true
# create the object
df <- vect(df, geom = c("lon", "lat"), crs = "EPSG:4326")
# now project it
df <- project(df, crs(grids))
```
## Compare the output
**original object**
class : SpatVector
geometry : points
dimensions : 1459, 3 (geometries, attributes)
extent : 126.9018, 127.1308, 37.46144, 37.65323 (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 (EPSG:4326)
**after projection**
class : SpatVector
geometry : points
dimensions : 1459, 3 (geometries, attributes)
extent : 947164.1, 967361.8, 1940349, 1961613 (xmin, xmax, ymin, ymax)
coord. ref. : Korea 2000 / Unified CS (EPSG:5179)
## Distances
- We now have our grids (we'll get rid of zero pop in a moment0)
- And we have points in the same CRS
```{r}
#| echo: true
#| eval: true
grids <- grids |>
filter(!is.na(pop))
# create distance matrix for PRIMARY schools
distances <- distance(grids, df |> filter(fclass=="elementaryschool"))
dim(distances)
head(distances)
```
## Distances
- We want to find the MINIMUM distance to a primary school for each grid cell
```{r}
#| echo: true
#| eval: true
closest <- apply(distances, 1, "min")
grids$closest_primary <- closest
```
## The map for primary school
```{r}
#| echo: false
#| eval: true
#| crop: true
#| fig.align: center
ggplot(grids) +
geom_spatvector(aes(fill = closest_primary), color = NA) +
scale_fill_distiller("Distance\n(m)", palette = "YlOrRd", direction = 1) +
theme_bw() +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")) +
theme(legend.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
```
## The map for primary school
```{r}
#| echo: false
#| eval: true
#| crop: true
#| fig.align: center
ggplot(grids) +
geom_density(aes(x = closest_primary)) +
theme_bw() +
labs(x = "Distance to closest primary school (m)", y = "Density") +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")) +
theme(legend.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
```
## What if we want to calculate proportion of population?
- Let's create some new values
```{r}
#| echo: true
#| eval: true
totalpop <- sum(grids$pop)
# within 1km
popwithin1km <- sum(grids$pop[grids$closest_primary <= 1000])/totalpop
# within 2km
popwithin2km <- sum(grids$pop[grids$closest_primary <= 2000])/totalpop
# within 5km
popwithin5km <- sum(grids$pop[grids$closest_primary <= 5000])/totalpop
popmat <- matrix(c(popwithin1km, popwithin2km, popwithin5km), nrow = 1)
rownames(popmat) <- "Primary school"
colnames(popmat) <- c("1km", "2km", "5km")
```
## What if we want to calculate proportion of population?
- Let's create some new values
```{r}
#| echo: true
#| eval: true
kable(popmat, digits = 3, row.names = TRUE) %>%
kable_styling("striped", full_width = F) %>%
column_spec(2:4, width = "2cm")
```
## Your turn
- Try it for middle and high schools!
## Example code
::: {.panel-tabset}
## Primary school
``` {.r}
distances <- distance(grids, df |> filter(fclass=="elementaryschool"))
distances <- apply(distances, 1, "min")
# within 1km
popwithin1km <- sum(grids$pop[distances <= 1000])/totalpop
# within 2km
popwithin2km <- sum(grids$pop[distances <= 2000])/totalpop
# within 5km
popwithin5km <- sum(grids$pop[distances <= 5000])/totalpop
popmat <- matrix(c(popwithin1km, popwithin2km, popwithin5km), nrow = 1)
rownames(popmat) <- "Primary school"
```
## Middle school
``` {.r}
distancesmid <- distance(grids, df |> filter(fclass=="middleschool"))
distancesmid <- apply(distancesmid, 1, "min")
# within 1km
popwithin1kmmid <- sum(grids$pop[distancesmid <= 1000])/totalpop
# within 2km
popwithin2kmmid <- sum(grids$pop[distancesmid <= 2000])/totalpop
# within 5km
popwithin5kmmid <- sum(grids$pop[distancesmid <= 5000])/totalpop
popmatmid <- matrix(c(popwithin1kmmid, popwithin2kmmid, popwithin5kmmid), nrow = 1)
rownames(popmatmid) <- "Middle school"
```
## High school
``` {.r}
distanceshigh <- distance(grids, df |> filter(fclass=="highschool"))
distanceshigh <- apply(distanceshigh, 1, "min")
# within 1km
popwithin1kmhigh <- sum(grids$pop[distanceshigh <= 1000])/totalpop
# within 2km
popwithin2kmhigh <- sum(grids$pop[distanceshigh <= 2000])/totalpop
# within 5km
popwithin5kmhigh <- sum(grids$pop[distanceshigh <= 5000])/totalpop
popmathigh <- matrix(c(popwithin1kmhigh, popwithin2kmhigh, popwithin5kmhigh), nrow = 1)
rownames(popmathigh) <- "High school"
```
:::
## The table
```{r}
#| echo: false
#| eval: true
distancesmid <- distance(grids, df |> filter(fclass=="middleschool"))
distancesmid <- apply(distancesmid, 1, "min")
# within 1km
popwithin1kmmid <- sum(grids$pop[distancesmid <= 1000])/totalpop
# within 2km
popwithin2kmmid <- sum(grids$pop[distancesmid <= 2000])/totalpop
# within 5km
popwithin5kmmid <- sum(grids$pop[distancesmid <= 5000])/totalpop
popmatmid <- matrix(c(popwithin1kmmid, popwithin2kmmid, popwithin5kmmid), nrow = 1)
rownames(popmatmid) <- "Middle school"
colnames(popmatmid) <- c("1km", "2km", "5km")
distanceshigh <- distance(grids, df |> filter(fclass=="highschool"))
distanceshigh <- apply(distanceshigh, 1, "min")
# within 1km
popwithin1kmhigh <- sum(grids$pop[distanceshigh <= 1000])/totalpop
# within 2km
popwithin2kmhigh <- sum(grids$pop[distanceshigh <= 2000])/totalpop
# within 5km
popwithin5kmhigh <- sum(grids$pop[distanceshigh <= 5000])/totalpop
popmathigh <- matrix(c(popwithin1kmhigh, popwithin2kmhigh, popwithin5kmhigh), nrow = 1)
rownames(popmathigh) <- "High school"
colnames(popmathigh) <- c("1km", "2km", "5km")
popmat <- rbind(popmat, popmatmid, popmathigh)
kable(popmat, digits = 3, row.names = TRUE, format = "html") %>%
column_spec(1, width = "6cm") %>%
column_spec(2:4, width = "3cm")
```
# Using Python and the GEE API
## Just for today, Python
- We are going to:
- Install Python
- Install Positron
- You should already have a GEE account from last week
## Installing Python
- If you already have Python installed, don't install it!
- You can check in the terminal/console:
```{r}
#| results: asis
#| eval: false
python --version
```
- If it returns a version number, you have Python installed
- We want it to be `Python 3.XX.X` (some version of python3)
## Installing Python
- If it is not downloaded, you have two options:
- Download from the [Python website](https://www.python.org/downloads/)
- Download from the [Anaconda website](https://www.anaconda.com/products/distribution)
- I recommend Anaconda (or miniconda) for beginners
## Installing Positron
- We are going to use the same IDE
- Let's install Positron
- Same company that makes RStudio
- It's my favorite Python IDE
- Go to the [Positron website](https://github.com/posit-dev/positron/releases)
- First post > Assets > correct version for your OS
## Next steps
- Here's what we are going to do:
- Open a folder (maybe make a new one?)
- Create a "virtual environment"
- Install the packages we need
## Step 1: Open a folder
![](week7assets/positron1.png){fig-align="center"}
## Step 2: Create an environment
- Now let's create a "virtual environment"
![](week7assets/positron2.png){fig-align="center"}
## Step 2: Create an environment
```{bash}
#| results: asis
#| eval: false
python -m venv classenv
```
- Positron will ask if you want to use the environment
- Click "Yes"
- It will also ask to install the `ipykernel` package
- Click "Install"
. . .
- This will create a new folder called `classenv` in your current directory (that you opened/made earlier)
- This is where all the packages will be installed
## Step 2: Create an environment
- Finally, close Positron and reopen it
- In the "Terminal" you should see the name of the environment you just created, something like this:
```{bash}
#| results: asis
#| eval: false
(classenv) Joshs-MacBook-Pro:geospatialdataR Josh$
```
## Step 3: Install packages
- We need two separate packages:
- `earthengine-api`
- `geopandas`
- We do this in the "Terminal"
```{bash}
#| results: asis
#| eval: false
pip3 install earthengine-api geopandas
```
## Step 3: Install packages
![](week7assets/positron3.png){fig-align="center"}
## One last step: create a script!
- In the upper left, you should see "New"
- Click it, then click "New File"
- Save it as `inclassgee.py` - MAKE SURE to add the `.py` extension!
## Just like with R, load libraries
```{python}
#| echo: true
#| eval: false
# load libraries
import ee # <1>
import geopandas as gpd # <2>
```
1. This is the Earth Engine API library
2. This is the `geopandas` library (you'll see what the `as gpd` does later)
- Note we can create comments just like in `RStudio`
- You can run them just like in `RStudio` (highlight and press `ctrl/cmd + enter`)
## We need to initialize the API
- Last week, you created a "project"
- You need to figure out its name to use here! (upper-right corner of the code editor on GEE)
- This should open a pop-up in your browser
```{python}
#| echo: true
#| eval: false
ee.Authenticate()
ee.Initialize(project="ee-geefolder")
```
## Searching for data
- NDVI ([GEE link](https://developers.google.com/earth-engine/datasets/catalog/MODIS_061_MOD13A3))
```{python}
#| echo: true
#| eval: false
# Let's look at NDVI
ndvi = ee.ImageCollection("MODIS/061/MOD13A3")
# we can use "print" and "getInfo()" to look at more information
# If the above call worked correctly, you should see a bunch of information printed in the console
print(ndvi.getInfo())
```
## Filtering by date
```{python}
#| echo: true
#| eval: false
# We can also filter the collection by date. Let's look september 2023
ndvi = ndvi.filterDate("2023-08-01", "2023-08-31")
# for assets that have many bands (raster layers), we can select the specific ones we want:
ndvi = ndvi.select("NDVI")
ndvi
# finally, just make sure we have an IMAGE, not an image collection
ndvi = ndvi.mean()
ndvi
```
## Filtering by area
- Let's use Korea to pull data
- `week7files`: `kgrid.shp`
- Going to read this into Python using `geopandas`
```{python}
#| echo: true
#| eval: false
# let's load the shapefile (note the use of "gpd" instead of "geopandas"!)
shape = gpd.read_file("week7files/kgrid.shp")
# make sure it is in lat/lon (project it)
shape = shape.to_crs("EPSG:4326")
# let's get the total bounds for the shapefile
bounds = shape.total_bounds
bounds
```
## Downloading
```{python}
#| echo: true
#| eval: false
# let's create a bounding box in earth engine. Note the syntax (xmin, ymin, xmax, ymax)
# this does not accept an array (which is what bounds was), so we will extract the individual components
# Also note that indexing in python starts at 0, not 1! bounds[0] gives the first value in the array
bbox = ee.Geometry.BBox(bounds[0], bounds[1], bounds[2], bounds[3])
task = ee.batch.Export.image.toDrive(image=ndvi,
description="krndvi",
scale=1000, # set scale the same as the raster's resolution (you can find this on GEE)
region=bbox,
crs="EPSG:4326",
fileFormat="GeoTIFF")
# start the task. You must do this for GEE to actually run the command.
task.start()
# can check the status of the task
task.status()
```
## Some notes on downloading
- You do not have to check the status
- It will finish automatically
- You can start multiple tasks
- Some tasks will take longer than others
- One way to speed it up: use a larger resolution (e.g. 100 instead of 10)
## Land classification
- Now let's download land classification for the most recent year
- Find the appropriate GEE id
- Let's use the Copernicus Global Land Cover Layers
. . .
- What is the id?
- What are the available dates?
## Land classification
![](week7assets/gee1.png){fig-align="center"}
## Land classification
```{python}
#| echo: true
#| eval: false
# land classification
lc = ee.ImageCollection("COPERNICUS/Landcover/100m/Proba-V-C3/Global")
# Only 2021
lc = lc.filterDate("2019-01-01", "2019-12-31")
# just make sure we have an IMAGE, not an image collection
lc = lc.first()
# let's try something different: download the discrete_classification only
lc = lc.select("discrete_classification")
task = ee.batch.Export.image.toDrive(image=lc,
description="krlc",
scale=100, # Let's do 100 (for time and memory)
region=bbox,
crs="EPSG:4326",
fileFormat="GeoTIFF")
task.start()
task.status()
```
## Land classification
- While we wait for that to download, I've already downloaded it for you!
- `week7files/krlc.tf`
- Let's look at it in `R`, using `terra`
## The raster
- What are we looking at here?
```{r}
#| echo: true
#| eval: true
lc <- rast("week7files/krlc.tif")
head(lc)
```
. . .
- We need to turn this into a factor!
## This isn't straightforward...
- This discrete classification variable is not numeric
- We need to find out what the values represent
- How?
. . .
- Let's check the GEE page for this data
## Land classification
![](week7assets/gee2.png){fig-align="center"}
## How to get the values?
- We could of course write them all by hand!
- For example, let's create a new variable that just has 8 values:
- 0: No data
- 1: Shrubs/plants
- 2: Agricultural land
- 3: Urban
- 4: Bare land
- 5: Snow/ice
- 6: Water
- 7: Forest
## Creating the data
```{r}
#| echo: true
#| eval: true
factorvalues <- data.frame(value = 0:7,
class = c("No data", "Shrubs/plants", "Agricultural land", "Urban", "Bare land", "Snow/ice", "Water", "Forest"))
factorvalues
```
## Now: change values of raster
```{r}
#| echo: true
#| eval: true
lcnew <- lc |>
mutate(discrete_classification = case_when(
discrete_classification == 0 ~ 0,
discrete_classification %in% c(20, 30, 90, 100) ~ 1,
discrete_classification == 40 ~ 2,
discrete_classification == 50 ~ 3,
discrete_classification == 60 ~ 4,
discrete_classification == 70 ~ 5,
discrete_classification %in% c(80, 200) ~ 6,
TRUE ~ 7
))
```
```{r}
#| echo: true
#| eval: true
# Double check
summary(lcnew)
```
## Now: create labels and assign
```{r}
#| echo: true
#| eval: true
cls<- data.frame(id = 0:7,
cover = c("No data", "Shrubs/plants", "Agricultural land", "Urban", "Bare land", "Snow/ice", "Water", "Forest"))
levels(lcnew) <- cls
lcnew
levels(lcnew)
```
## Plot it
```{r}
#| echo: true
#| eval: false
ggplot() +
geom_spatraster(data = lcnew, aes(fill = cover)) +
# Note we need fewer than 8 colors! Only 7 show up in our raster
scale_fill_manual("Classification", values = c("#ffbb22", "#f096ff", "#fa0000", "#b4b4b4", "#f0f0f0", "#0032c8", "#58481f")) +
theme_bw()
```
## Plot it
```{r}
#| echo: false
#| eval: true
#| crop: true
#| fig.align: center
ggplot() +
geom_spatraster(data = lcnew, aes(fill = cover)) +
scale_fill_manual("Classification", values = c("#ffbb22", "#f096ff", "#fa0000", "#b4b4b4", "#0032c8", "#009900")) +
theme_bw() +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")) +
theme(legend.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))