library(leaflet)
library(sf)
## Linking to GEOS 3.12.2, GDAL 3.8.5, PROJ 9.4.0; sf_use_s2() is TRUE
library(rgdal)
## Loading required package: sp
## Please note that rgdal will be retired during October 2023,
## plan transition to sf/stars/terra functions using GDAL and PROJ
## at your earliest convenience.
## See https://r-spatial.org/r/2023/05/15/evolution4.html and https://github.com/r-spatial/evolution
## rgdal: version: 1.6-7, (SVN revision 1203)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.8.5, released 2024/04/02
## Path to GDAL shared files: /usr/share/gdal
##  GDAL does not use iconv for recoding strings.
## GDAL binary built with GEOS: TRUE 
## Loaded PROJ runtime: Rel. 9.4.0, March 1st, 2024, [PJ_VERSION: 940]
## Path to PROJ shared files: /home/dragon/.local/share/proj:/usr/share/proj
## PROJ CDN enabled: FALSE
## Linking to sp version:2.1-4
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading sp or rgdal.
# Load the downloaded GADM data for Kenya
kenya_data <- readRDS("gadm36_KEN_2_sf.rds")
kenya_data
## Simple feature collection with 301 features and 13 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 33.90959 ymin: -4.720417 xmax: 41.92622 ymax: 5.061166
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## Geodetic CRS:  WGS 84
## First 10 features:
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
##    GID_0 NAME_0   GID_1  NAME_1 NL_NAME_1     GID_2          NAME_2 VARNAME_2
## 1    KEN  Kenya KEN.1_1 Baringo      <NA> KEN.1.1_1             805      <NA>
## 2    KEN  Kenya KEN.1_1 Baringo      <NA> KEN.1.2_1 Baringo Central      <NA>
## 3    KEN  Kenya KEN.1_1 Baringo      <NA> KEN.1.3_1   Baringo North      <NA>
## 4    KEN  Kenya KEN.1_1 Baringo      <NA> KEN.1.4_1   Baringo South      <NA>
## 5    KEN  Kenya KEN.1_1 Baringo      <NA> KEN.1.5_1   Eldama Ravine      <NA>
## 6    KEN  Kenya KEN.1_1 Baringo      <NA> KEN.1.6_1         Mogotio      <NA>
## 7    KEN  Kenya KEN.1_1 Baringo      <NA> KEN.1.7_1           Tiaty      <NA>
## 81   KEN  Kenya KEN.2_1   Bomet      <NA> KEN.2.1_1   Bomet Central      <NA>
## 82   KEN  Kenya KEN.2_1   Bomet      <NA> KEN.2.2_1      Bomet East      <NA>
## 83   KEN  Kenya KEN.2_1   Bomet      <NA> KEN.2.3_1      Chepalungu      <NA>
##    NL_NAME_2       TYPE_2    ENGTYPE_2 CC_2 HASC_2
## 1       <NA> Constituency Constituency  162   <NA>
## 2       <NA> Constituency Constituency  159   <NA>
## 3       <NA> Constituency Constituency  158   <NA>
## 4       <NA> Constituency Constituency  160   <NA>
## 5       <NA> Constituency Constituency  162   <NA>
## 6       <NA> Constituency Constituency  161   <NA>
## 7       <NA> Constituency Constituency  157   <NA>
## 81      <NA> Constituency Constituency  197   <NA>
## 82      <NA> Constituency Constituency  196   <NA>
## 83      <NA> Constituency Constituency  195   <NA>
##                          geometry
## 1  MULTIPOLYGON (((35.87727 -0...
## 2  MULTIPOLYGON (((35.80651 0....
## 3  MULTIPOLYGON (((35.81394 0....
## 4  MULTIPOLYGON (((36.25757 0....
## 5  MULTIPOLYGON (((35.84734 -0...
## 6  MULTIPOLYGON (((36.10672 0....
## 7  MULTIPOLYGON (((35.67241 1....
## 81 MULTIPOLYGON (((35.31016 -0...
## 82 MULTIPOLYGON (((35.35149 -0...
## 83 MULTIPOLYGON (((35.26193 -1...
# Create the leaflet map
leaflet() %>%
  addTiles() %>%
  addPolygons(data = kenya_data,
              color = "blue",
              weight = 1,
              smoothFactor = 0.5,
              opacity = 1.0,
              fillOpacity = 0.5,
              popup = ~paste("Province:", NAME_1, "<br>", "County:", NAME_2)) %>%
  addLegend(position = "bottomright", colors = c("blue"), labels = c("Counties"), title = "Kenya Map")
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
# Load necessary libraries
library(sf)

# Load the downloaded GADM data for Kenya
kenya_data <- readRDS("gadm36_KEN_2_sf.rds")

# Number of counties
n_counties <- nrow(kenya_data)

# Generate random values between 1 and 99
set.seed(123) # For reproducibility
random_corruption <- runif(n_counties, min = 1, max = 99)

# Normalize the values so they sum to 100
normalized_corruption <- random_corruption / sum(random_corruption) * 100

# Add the corruption column to the data
kenya_data$corruption <- normalized_corruption

# Check if the column has been added correctly and sums to 100
print(sum(kenya_data$corruption)) # Should print 100
## [1] 100
# Verify the first few rows
head(kenya_data)
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## Simple feature collection with 6 features and 14 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 35.52292 ymin: -0.198901 xmax: 36.29659 ymax: 0.9904135
## Geodetic CRS:  WGS 84
##   GID_0 NAME_0   GID_1  NAME_1 NL_NAME_1     GID_2          NAME_2 VARNAME_2
## 1   KEN  Kenya KEN.1_1 Baringo      <NA> KEN.1.1_1             805      <NA>
## 2   KEN  Kenya KEN.1_1 Baringo      <NA> KEN.1.2_1 Baringo Central      <NA>
## 3   KEN  Kenya KEN.1_1 Baringo      <NA> KEN.1.3_1   Baringo North      <NA>
## 4   KEN  Kenya KEN.1_1 Baringo      <NA> KEN.1.4_1   Baringo South      <NA>
## 5   KEN  Kenya KEN.1_1 Baringo      <NA> KEN.1.5_1   Eldama Ravine      <NA>
## 6   KEN  Kenya KEN.1_1 Baringo      <NA> KEN.1.6_1         Mogotio      <NA>
##   NL_NAME_2       TYPE_2    ENGTYPE_2 CC_2 HASC_2
## 1      <NA> Constituency Constituency  162   <NA>
## 2      <NA> Constituency Constituency  159   <NA>
## 3      <NA> Constituency Constituency  158   <NA>
## 4      <NA> Constituency Constituency  160   <NA>
## 5      <NA> Constituency Constituency  162   <NA>
## 6      <NA> Constituency Constituency  161   <NA>
##                         geometry corruption
## 1 MULTIPOLYGON (((35.87727 -0... 0.19369224
## 2 MULTIPOLYGON (((35.80651 0.... 0.51939086
## 3 MULTIPOLYGON (((35.81394 0.... 0.27265656
## 4 MULTIPOLYGON (((36.25757 0.... 0.58099651
## 5 MULTIPOLYGON (((35.84734 -0... 0.61836483
## 6 MULTIPOLYGON (((36.10672 0.... 0.03626951
# Load necessary libraries
library(leaflet)
library(sf)

# Load the downloaded GADM data for Kenya
kenya_data <- readRDS("gadm36_KEN_2_sf.rds")

# Generate random corruption percentages (as an example)
set.seed(123)
kenya_data$corruption <- runif(nrow(kenya_data), min = 1, max = 99)

# Normalize the corruption values to range from 0 to 1 for heatmap intensity
kenya_data$corruption_normalized <- (kenya_data$corruption - min(kenya_data$corruption)) / (max(kenya_data$corruption) - min(kenya_data$corruption))

# Create the leaflet map
leaflet(kenya_data) %>%
  addTiles() %>%
  addPolygons(
    fillColor = ~colorNumeric("Reds", corruption_normalized)(corruption_normalized),
    color = "white",
    weight = 1,
    smoothFactor = 0.5,
    opacity = 1.0,
    fillOpacity = 0.7,
    popup = ~paste("Province:", NAME_1, "<br>", "County:", NAME_2, "<br>", "Corruption:", round(corruption, 2)),
    label = ~paste(NAME_1, "-", NAME_2),
    highlight = highlightOptions(
      weight = 2,
      color = "black",
      fillOpacity = 0.7,
      bringToFront = TRUE
    )
  ) %>%
  addLegend(
    position = "bottomright",
    pal = colorNumeric("Reds", domain = kenya_data$corruption_normalized),
    values = ~corruption_normalized,
    title = "Corruption Scale",
    labFormat = labelFormat(suffix = "%")
  ) %>%
  setView(lng = 37.8, lat = 0.6, zoom = 6)
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()