-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScript.R
53 lines (48 loc) · 2.4 KB
/
Script.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
library(rgeoboundaries)
library(sf)
library(raster)
library(here)
library(ggplot2)
library(viridis)
library(dplyr)
# Downloading the boundary of Kenya
map_boundary <- geoboundaries("Kenya")
# Reading in the downloaded landcover raster data
IGBP_raster <- raster(here::here("C:/Users/user/KY/Kenya_adm0/LandCover_Type_Yearly_500m_v6/LC1/MCD12Q1_LC1_2019_001.tif"))
# Transforming data
IGBP_raster <- projectRaster(IGBP_raster, crs = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
# Cropping data
IGBP_raster <- raster::mask(IGBP_raster, as_Spatial(map_boundary))
# Converting the raster object into a dataframe and converting the IGBP classification into a factor
IGBP_df <- as.data.frame(IGBP_raster, xy = TRUE, na.rm = TRUE) %>%
mutate(MCD12Q1_LC1_2019_001 = as.factor(round(MCD12Q1_LC1_2019_001)))
rownames(IGBP_df) <- c()
# Renaming IGBP classification levels
levels(IGBP_df$MCD12Q1_LC1_2019_001) <- c( "Evergreen needleleaf forests",
"Evergreen broadleaf forests",
"Deciduous needleleaf forests",
"Deciduous broadleaf forests",
"Mixed forests",
"Closed shrublands",
"Open shrublands",
"Woody savannas",
"Savannas",
"Grasslands",
"Permanent wetlands",
"Croplands",
"Urban and built-up lands",
"Cropland/natural vegetation mosaics",
"Snow and ice",
"Barren",
"Water bodies")
# Visualising using ggplot2
ggplot() +
geom_raster(data = IGBP_df,
aes(x = x, y = y, fill = MCD12Q1_LC1_2019_001)) +
geom_sf(data = map_boundary, inherit.aes = FALSE, fill = NA) +
scale_fill_viridis(name = "Land Cover Type", discrete = TRUE) +
labs(title = "Land Cover classification in Kenya",
subtitle = "01-01-2019 - 31-12-2019",
x = "Longitude",
y = "Latitude") +
theme_minimal()