In-class Exercise 2

pacman::p_load(sf,tidyverse,tmap,spdep,funModeling)

Importing Geospatial Data

Importing water point geospatial data

wp <- st_read(dsn="geodata",
              layer="geo_export",
              crs=4326) %>%
  filter(clean_coun == "Nigeria")

write_rds is used to save the extracted sf data table into an output file in rds data format.

wp_nga <- write_rds(wp,
                    "geodata/wp_nga.rds")

Importing Nigeria LGA boundary data

nga <- st_read(dsn="geodata",layer="nga_admbnda_adm2_osgof_20190417",
               crs=4326)

Data Wrangling

Recode NA values into string

The code chunk is used to recode all NA values into Unknown.

wp_nga <- read_rds("geodata/wp_nga.rds") %>%
  mutate(status_cle = replace_na(status_cle, "Unknown"))

Explanatory Data Analysis

freq (data=wp_nga,input = "status_cle")

Extracting Water Point Data

Extracting functional water point

wpt_functional <- wp_nga %>%
  filter(status_cle %in%
           c("Functional", 
             "Functional but not in use",
             "Functional but needs repair"))
freq(data=wpt_functional, input="status_cle")

Extracting non-functional water point

wpt_nonfunctional <- wp_nga %>%
  filter(status_cle %in%
           c("Abandoned/Decommissioned", 
             "Abandoned",
             "Non-Functional",
             "Non functional due to dry season",
             "Non-Functional due to dry season"))
freq(data=wpt_nonfunctional, 
     input = 'status_cle')

Extracting water point with Unknown class

wpt_unknown <- wp_nga %>%
  filter(status_cle == "Unknown")

Peforming Point-in-Polygon Count

nga_wp <- nga %>% 
  mutate(`total wpt` = lengths(
    st_intersects(nga, wp_nga))) %>%
  mutate(`wpt functional` = lengths(
    st_intersects(nga, wpt_functional))) %>%
  mutate(`wpt non-functional` = lengths(
    st_intersects(nga, wpt_nonfunctional))) %>%
  mutate(`wpt unknown` = lengths(
    st_intersects(nga, wpt_unknown)))

Saving the analytical data table

nga_wp <- nga_wp %>%
  mutate(pct_functional = `wpt functional`/`total wpt`) %>%
  mutate(`pct_non-functional` = `wpt non-functional`/`total wpt`) %>%
  select(3:4, 9:10, 18:23)

Now have the tidy sf data table and save it into rds format.

write_rds(nga_wp, "geodata/nga_wp.rds")

Visualising the spatial distribution of water points

nga_wp <- read_rds("geodata/nga_wp.rds")
total <- qtm(nga_wp, "total wpt")
wp_functional <- qtm(nga_wp, "wpt functional")
wp_nonfunctional <- qtm(nga_wp, "wpt non-functional")
unknown <- qtm(nga_wp, "wpt unknown")

tmap_arrange(total, wp_functional, wp_nonfunctional, unknown, asp=0, ncol=2)