---
title: "Decadal Land Use-Land Cover Change in Southern Malawi"
author: "Clinton Nkolokosa"
date: '2022-09-15'
output:
flexdashboard::flex_dashboard:
orientation: rows
source_code: embed
vertical_layout: fill
css: style.css
---
<style type="text/css">
div.main-container {
max-width: 1800px;
margin-left: auto;
margin-right: auto;
}
</style>
```{r setup, include=FALSE}
library(flexdashboard)
library(plotly)
library(shinythemes)
library(shinydashboard)
library(shinyWidgets)
library(leaflet)
library(leaflet.extras)
library(leafem)
library(leafsync)
library(sf)
library(tidyverse)
library(RColorBrewer)
library(shinyjs)
library(htmltools)
library(raster)
library(rgdal)
library(rmapshaper)
library(cols4all)
library(abind)
library(colorspace)
library(kableExtra)
library(networkD3)
# Set colors
lulc_pal <- function(data){
sapply(levels(data)[[1]]$class, function(class){
if(class == "Built-up"){
"#ff0000" # mars red (red)
} else if(class == "Forest"){
"#267300" # fir green (green)
} else if(class == "Herbaceous"){
"#aaff00" # periodt green (greenyellow)
} else if(class == "Bareland"){
"gray"
} else if(class == "Water"){
"#5475a8" # delft blue (blue)
} else if(class == "Cropland"){
"#f09c00" # seville orange (gold)
} else{
"#808000" # olive
}
})
}
builtup_color <- "#ff0000"
forest_color <- "#267300"
herbaceous_color <- "#aaff00"
bareland_color <- "gray"
water_color <- "#5475a8"
cropland_color <- "#f09c00"
shrubland_color <- "#808000"
# Read in data
# classified maps 1km
lulc90 <- raster("C:/R/Flood mapper/LULCC_Analysis/c1990.tif")
lulc00 <- raster("C:/R/Flood mapper/LULCC_Analysis/c2000.tif")
lulc10 <- raster("C:/R/Flood mapper/LULCC_Analysis/c2010.tif")
lulc20 <- raster("C:/R/Flood mapper/LULCC_Analysis/c2020.tif")
extent(lulc90) <- extent(lulc20) # match extent, ignore the small differences in extent
extent(lulc00) <- extent(lulc20)
compareRaster(lulc90, lulc00, lulc10, lulc20) # TRUE
# assign land cover names
lulc <- ratify(lulc90) # access raster attribute
lulc <- levels(lulc)[[1]]
lulc$class <- c("Built-up", "Forest", "Herbaceous", "Bareland",
"Water", "Cropland", "Shrubland")
# assign new attribute
levels(lulc90) <- lulc
levels(lulc00) <- lulc
levels(lulc10) <- lulc
levels(lulc20) <- lulc
# Excel files
lulc_file <- readxl::read_xlsx("C:/R/Flood mapper/LULC data/LULCC.xlsx", sheet = 1)
lulc_file
# round numeric values to 1 decimal place
lulc_file <- lulc_file |>
dplyr::mutate(across(where(is.numeric), round, 1))
```
Dynamics
=====================================================
Row{.tabset .tabset-fade}
-----------------------------------------------------
### Plot
```{r}
plotly::plot_ly(data = lulc_file,
x = ~LULC,
y = ~`1990`,
name = "1990",
type = "bar") |>
plotly::add_trace(y = ~`2000`,
name = "2000") |>
plotly::add_trace(y = ~`2010`,
name = "2010") |>
plotly::add_trace(y = ~`2020`,
name = "2020") |>
plotly::layout(title = " ",
barmode = 'group',
xaxis = list(title = "LULC"),
yaxis = list(title = " Area (sq.km)"))
```
### Sankey
```{r, fig.width=15}
# create Sankey diagram --------------------------------------------------------
# indexing
crosstab90_00 <- as.data.frame(crosstab(lulc90, lulc00, useNA=FALSE))
indexing_90 <- (rep(0:6, 7)) # for zero indexing of Sankey diagram
crosstab90_00$c1990 <- indexing_90
crosstab90_00 <- crosstab90_00[order(crosstab90_00$c1990),]
indexing_00 <- (rep(7:13, 7)) # indexing continued from indexing_90, so from number 7
crosstab90_00$c2000 <- indexing_00 #change numbering to indexing_00
#Now the same is done for 2000 and 2010
crosstab00_10 <- as.data.frame(crosstab(lulc00, lulc10, useNA=FALSE))
crosstab00_10$c2000 <- indexing_00 #change numbering to indexing_00
crosstab00_10 <- crosstab00_10[order(crosstab00_10$c2000),] #order them from small to big for easier indexing merge
indexing_10 <- (rep(14:20, 7)) #indexing for 2010
crosstab00_10$c2010 <- indexing_10 #include indexing for 2010
# 2010 and 2020
crosstab10_20 <- as.data.frame(crosstab(lulc10, lulc20, useNA = FALSE))
indexing_20 <- (rep(21:27, 7)) # indexing continued from indexing_10
crosstab10_20$c2020 <- indexing_20 #include indexing for 2020
crosstab10_20 <- crosstab10_20[order(crosstab10_20$c2010),]
crosstab10_20$c2010 <- indexing_10 #change numbering to indexing_10
#rename columns before merging so that both crosstabs have the same names
names(crosstab90_00)[1] = "Source"
names(crosstab90_00)[2] = "Target"
names(crosstab90_00)[3] = "Value"
names(crosstab00_10)[1] = "Source"
names(crosstab00_10)[2] = "Target"
names(crosstab00_10)[3] = "Value"
names(crosstab10_20)[1] = "Source"
names(crosstab10_20)[2] = "Target"
names(crosstab10_20)[3] = "Value"
crosstab_total <- rbind(crosstab90_00, crosstab00_10, crosstab10_20)
nodes <- c(1:28)
#second column needs to be a combination of landcover and year.
# First copy the landcover types four times
categories <- data.frame(levels(lulc90)) |>
subset(select = -c(ID) )
categories4 <- rbind(categories, categories, categories, categories)
#I then made a list of the years 7 times (for each landcover type)
Year90 <- list(rep(1990, 7))
Year00 <- list(rep(2000, 7))
Year10 <- list(rep(2010, 7))
Year20 <- list(rep(2020, 7))
Year <- rbind.data.frame(Year90, Year00, Year10, Year20)
#the nodes are now combined in three columns. 1: number 1:21, 2: landcover types, 3: years
landcovernodes <- data.frame(nodes, categories4, Year)
#column 2 and 3 are pasted into one, so that landcover and year are in one column.
#the other columns are removed
landcovernodes$name <- paste(landcovernodes$class,
landcovernodes$c.1990..1990..1990..1990..1990..1990..1990..2000..2000..2000..)
landcovernodes <- dplyr::select(landcovernodes, -c(2, 3))
#To ensure the colors correspond with our land cover maps, I made a color file from number 1:7, corresponding with the nodes
my_color <- 'd3.scaleOrdinal() .domain(["1", "2", "3", "4","5","6","7"])
.range(["#ff0000", "#267300", "#aaff00",
"gray", "#5475a8", "#f09c00", "olive"])'
#Include a grouping variable to colour the different flows in the diagram
groupa <- list(rep("1", 7))
groupb <- list(rep("2", 7))
groupc <- list(rep("3", 7))
groupd <- list(rep("4", 7))
groupe <- list(rep("5", 7))
groupf <- list(rep("6", 7))
groupg <- list(rep("7", 7))
groups <- rbind.data.frame(groupa, groupb, groupc, groupd, groupe, groupf, groupg)
groups_double <- rbind.data.frame(groups, groups, groups)
names(groups_double)[1] <- "group"
crosstab_total$group <- (groups_double$group)
# create the sankey diagram
sankeyDiagram <- sankeyNetwork(Links = crosstab_total,
Nodes = landcovernodes,
Source = "Source",
Target = "Target",
Value = "Value",
NodeID = "name",
LinkGroup = "group",
colourScale = my_color,
fontSize = 15,
nodeWidth = 12,
iterations = 0)
sankeyDiagram
```