# call requisite libraries
library(rgdal)
## Loading required package: sp
## Please note that rgdal will be retired by the end of 2023,
## plan transition to sf/stars/terra functions using GDAL and PROJ
## at your earliest convenience.
## 
## rgdal: version: 1.5-28, (SVN revision 1158)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.2.1, released 2020/12/29
## Path to GDAL shared files: C:/Users/User/Documents/R/win-library/4.1/rgdal/gdal
## GDAL binary built with GEOS: TRUE 
## Loaded PROJ runtime: Rel. 7.2.1, January 1st, 2021, [PJ_VERSION: 721]
## Path to PROJ shared files: C:/Users/User/Documents/R/win-library/4.1/rgdal/proj
## PROJ CDN enabled: FALSE
## Linking to sp version:1.4-6
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading sp or rgdal.
## Overwritten PROJ_LIB was C:/Users/User/Documents/R/win-library/4.1/rgdal/proj
library(rgeos)
## rgeos version: 0.5-9, (SVN revision 684)
##  GEOS runtime version: 3.9.1-CAPI-1.14.2 
##  Please note that rgeos will be retired by the end of 2023,
## plan transition to sf functions using GEOS at your earliest convenience.
##  GEOS using OverlayNG
##  Linking to sp version: 1.4-6 
##  Polygon checking: TRUE
library(sf)
## Linking to GEOS 3.9.1, GDAL 3.2.1, PROJ 7.2.1; sf_use_s2() is TRUE
library(sp)
library(tmap)
library(tmaptools)
# create a new shapefile
knbs_select <- readxl::read_xlsx("D:/Documents/climate and energy advisory/2022/August/Alternativie fuel resource assessment/inception report/data_and_excel sheets/county_households_new.xlsx" 
                       )
print(length(knbs_select$name))
## [1] 15
# join the selected counties with shapefile
counties_shape <- readOGR(
  dsn = "D:/DUKTO/ken_adm_iebc_20191031_shp/ken_admbnda_adm1_iebc_20191031.shp"
  )
## OGR data source with driver: ESRI Shapefile 
## Source: "D:\DUKTO\ken_adm_iebc_20191031_shp\ken_admbnda_adm1_iebc_20191031.shp", layer: "ken_admbnda_adm1_iebc_20191031"
## with 47 features
## It has 12 fields
households_select <- merge(counties_shape, knbs_select, by.x = "ADM1_EN", by.y = "name", all.x = F)
households_select@data
##            ADM1_EN Shape_Leng Shape_Area ADM1_PCODE ADM1_REF ADM1ALT1EN
## 1  Elgeyo-Marakwet   3.888933  0.2444318      KE028     <NA>       <NA>
## 2         Homa Bay   3.373781  0.3844443      KE043     <NA>       <NA>
## 3           Isiolo  10.219754  2.0638503      KE011     <NA>       <NA>
## 4          Kajiado   7.412835  1.7791830      KE034     <NA>       <NA>
## 5         Kakamega   4.125304  0.2443814      KE037     <NA>       <NA>
## 6          Kericho   3.541334  0.2041787      KE035     <NA>       <NA>
## 7           Kilifi   5.744543  1.0251272      KE003     <NA>       <NA>
## 8        Kirinyaga   1.947362  0.1198584      KE020     <NA>       <NA>
## 9            Kitui   9.074169  2.4753375      KE015     <NA>       <NA>
## 10           Kwale   5.426097  0.6732554      KE002     <NA>       <NA>
## 11            Meru   4.400599  0.5680862      KE012     <NA>       <NA>
## 12         Nairobi   1.658061  0.0574956      KE047     <NA>       <NA>
## 13       Nyandarua   3.571838  0.2653980      KE018     <NA>       <NA>
## 14    Taita Taveta   5.677845  1.3941980      KE006     <NA>       <NA>
## 15     Uasin Gishu   3.926051  0.2759761      KE027     <NA>       <NA>
##    ADM1ALT2EN ADM0_EN ADM0_PCODE       date    validOn validTo Population
## 1        <NA>   Kenya         KE 2017/11/03 2019/10/31    <NA>     453403
## 2        <NA>   Kenya         KE 2017/11/03 2019/10/31    <NA>    1125823
## 3        <NA>   Kenya         KE 2017/11/03 2019/10/31    <NA>     267997
## 4        <NA>   Kenya         KE 2017/11/03 2019/10/31    <NA>    1107296
## 5        <NA>   Kenya         KE 2017/11/03 2019/10/31    <NA>    1861332
## 6        <NA>   Kenya         KE 2017/11/03 2019/10/31    <NA>     896863
## 7        <NA>   Kenya         KE 2017/11/03 2019/10/31    <NA>    1440958
## 8        <NA>   Kenya         KE 2017/11/03 2019/10/31    <NA>     605630
## 9        <NA>   Kenya         KE 2017/11/03 2019/10/31    <NA>    1130134
## 10       <NA>   Kenya         KE 2017/11/03 2019/10/31    <NA>     858748
## 11       <NA>   Kenya         KE 2017/11/03 2019/10/31    <NA>    1535635
## 12       <NA>   Kenya         KE 2017/11/03 2019/10/31    <NA>    4337080
## 13       <NA>   Kenya         KE 2017/11/03 2019/10/31    <NA>     636002
## 14       <NA>   Kenya         KE 2017/11/03 2019/10/31    <NA>     335747
## 15       <NA>   Kenya         KE 2017/11/03 2019/10/31    <NA>    1152671
##    No.of Households Avg Household size
## 1             99861                 45
## 2            262036                 43
## 3             58072                 46
## 4            316179                 35
## 5            433207                 43
## 6            206036                 44
## 7            298472                 48
## 8            204188                 30
## 9            262942                 43
## 10           173176                 50
## 11           426360                 36
## 12          1506888                 29
## 13           179686                 35
## 14            96429                 35
## 15           304943                 38
# view the knbs households 2019 dataset
knbs_households <- read.csv("D:/Documents/climate and energy advisory/2022/August/Alternativie fuel resource assessment/inception report/data_and_excel sheets/county_households.csv", 
                            header = T)
# join the counties shapefile with the knbs dataset through the county names
households_shape <- merge(
  counties_shape, knbs_households, by.x = "ADM1_EN", by.y = "name", all.x = T)
# draw a the selected counties overlying the previous tmap
tm_shape(
  households_shape, 
  bbox = households_select
  ) + tm_polygons(
    col = "No.of.Households", 
    palette = "-inferno", 
    style = "quantile", 
    n =7, 
    legend.show = F, 
    alpha = 0.1
    ) + 
  tm_shape(households_select) + 
  tm_polygons(
    col = "No.of Households", 
    palette = "-inferno", 
    style = "cat", 
    n = 15, 
    title = "No. of households for selected counties"
    ) +
  tm_text(
    text = "ADM1_EN", 
    size = "AREA", 
    col = "firebrick4"
    ) + 
  tm_grid(
    projection = "+init=epsg:4326", 
    lines = F
    ) + tm_legend(
      legend.outside = T, 
      legend.outside.position = "right", 
      bg.color = "grey85"
      ) + 
  tm_layout(
    main.title = "Distribution of households for target counties", 
    bg.color = "gray90", 
    frame = T
    ) + 
  tm_scale_bar() + 
  tm_compass(
    position = c("right", "top")
    )
## Warning in CPL_crs_from_input(x): GDAL Message 1: +init=epsg:XXXX syntax is
## deprecated. It might return a CRS with a non-EPSG compliant axis order.

# install the grid package
# install.packages("grid")
library(grid)
# the inset map
zone <- bb_poly(households_select, projection = 4326)
inset <- tm_shape(households_shape) + 
  tm_polygons(
    col = "No.of.Households", 
    palette = "-inferno", 
    style = "quantile", 
    n =7, 
    legend.show = F
    ) +
  tm_shape(zone) + 
  tm_borders(
    lwd = 2, 
    col = "red"
    )
# the overview map
overview <- tm_shape(
  households_shape, 
  bbox = households_select
  ) + tm_polygons(
    col = "No.of.Households", 
    palette = "-inferno", 
    style = "quantile", 
    n =7, 
    legend.show = F, 
    alpha = 0.1
    ) + 
  tm_shape(households_select) + 
  tm_polygons(
    col = "No.of Households", 
    palette = "-inferno", 
    style = "cat", 
    n = 15, 
    title = "No. of households for selected counties"
    ) +
  tm_text(
    text = "ADM1_EN", 
    size = "AREA", 
    col = "firebrick4"
    ) + 
  tm_grid(
    projection = "+init=epsg:4326", 
    lines = F
    ) + 
  tm_legend(
    legend.outside = T, 
    legend.outside.position = "right", 
    bg.color = "grey85"
    ) + 
  tm_layout(
    main.title = "Distribution of households for target counties", 
    bg.color = "gray90", 
    frame = T
    ) + 
  tm_scale_bar() + 
  tm_compass(
    position = c("right", "top")
    ) 
# draw tmap with inset map
library(tmaptools)

tmap_save(
  tm = overview, 
  insets_tm = inset, 
  insets_vp = viewport(x = 0.2, 
                       y = 0.3, 
                       width = unit(1, "inches"), 
                       height = unit(1, "inches")
                               ), 
filename = "D:/Documents/climate and energy advisory/2022/August/Alternativie fuel resource assessment/population_maps/final_three.jpg", 
dpi = 400
)
## Map saved to D:\Documents\climate and energy advisory\2022\August\Alternativie fuel resource assessment\population_maps\final_three.jpg
## Resolution: 2704.366 by 2899.016 pixels
## Size: 6.760914 by 7.24754 inches (400 dpi)