load libraries

library(leaflet) # mapping 
library(sf) # for spatial data analysis
library(tidycensus)
library(tidyverse)
library(tmap)
library(mapview)
library(tigris)

Import the csv with the coordinates and records

##links2 <- read.csv("D:/R_Studio/Village_GPS4.csv")
links2 <- read.csv("D:/Jeremiah/template species abundance csv.csv")

Read the Kenya shapefile with county adminstrative boundaries

The st_read() function comes from sf package

ke.counties2 <- st_read("D:/R_Studio/kenyan-counties/County.shp")
## Reading layer `County' from data source `D:\R_Studio\kenyan-counties\County.shp' using driver `ESRI Shapefile'
## Simple feature collection with 47 features and 8 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 33.91182 ymin: -4.702271 xmax: 41.90626 ymax: 5.430648
## Geodetic CRS:  WGS 84
#use "view" for interactive plot; "plot" for static map
tmap_mode("plot")

Visualize point in polygon

Adding circle markers to the Kenya map.

 my_map <- leaflet() %>%
      addTiles() %>%
      setView(lng = 37, lat = 0.3, zoom = 6) %>%
      addPolygons(
        data = ke.counties2,
        fillColor = "none",
        weight = 2,
        opacity = 1
      ) %>%
  addCircleMarkers(data = links2, 
                   lng = links2$longitude, 
                   lat = links2$latitude,
                   radius = 5,
                   weight = 3,
                   color = " ") %>%
  addMiniMap (position = "bottomleft") 

my_map
#knitr::include_graphics(my_map)

Session Info:

sessionInfo()
## R version 4.1.2 (2021-11-01)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 22000)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=English_Kenya.1252  LC_CTYPE=English_Kenya.1252   
## [3] LC_MONETARY=English_Kenya.1252 LC_NUMERIC=C                  
## [5] LC_TIME=English_Kenya.1252    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] tigris_1.6.1     mapview_2.11.0   tmap_3.3-3       forcats_0.5.2   
##  [5] stringr_1.4.1    dplyr_1.0.10     purrr_0.3.4      readr_2.1.3     
##  [9] tidyr_1.2.1      tibble_3.1.8     ggplot2_3.3.6    tidyverse_1.3.2 
## [13] tidycensus_1.2.3 sf_1.0-8         leaflet_2.1.1   
## 
## loaded via a namespace (and not attached):
##  [1] fs_1.5.2                satellite_1.0.4         lubridate_1.8.0        
##  [4] webshot_0.5.4           RColorBrewer_1.1-3      httr_1.4.4             
##  [7] tools_4.1.2             backports_1.4.1         bslib_0.4.0            
## [10] utf8_1.2.2              rgdal_1.5-32            R6_2.5.1               
## [13] KernSmooth_2.23-20      DBI_1.1.3               colorspace_2.0-3       
## [16] raster_3.6-3            withr_2.5.0             sp_1.5-0               
## [19] tidyselect_1.1.2        compiler_4.1.2          leafem_0.2.0           
## [22] cli_3.4.1               rvest_1.0.3             xml2_1.3.3             
## [25] sass_0.4.2              scales_1.2.1            classInt_0.4-8         
## [28] proxy_0.4-27            rappdirs_0.3.3          digest_0.6.29          
## [31] foreign_0.8-83          rmarkdown_2.16          base64enc_0.1-3        
## [34] dichromat_2.0-0.1       pkgconfig_2.0.3         htmltools_0.5.3        
## [37] dbplyr_2.2.1            fastmap_1.1.0           htmlwidgets_1.5.4      
## [40] rlang_1.0.6             readxl_1.4.1            rstudioapi_0.14        
## [43] jquerylib_0.1.4         generics_0.1.3          jsonlite_1.8.2         
## [46] crosstalk_1.2.0         googlesheets4_1.0.1     magrittr_2.0.3         
## [49] Rcpp_1.0.9              munsell_0.5.0           fansi_1.0.3            
## [52] abind_1.4-5             terra_1.6-17            lifecycle_1.0.2        
## [55] stringi_1.7.8           leafsync_0.1.0          yaml_2.3.5             
## [58] tmaptools_3.1-1         grid_4.1.2              maptools_1.1-4         
## [61] parallel_4.1.2          crayon_1.5.2            lattice_0.20-45        
## [64] haven_2.5.1             stars_0.5-6             hms_1.1.2              
## [67] knitr_1.40              pillar_1.8.1            uuid_1.1-0             
## [70] stats4_4.1.2            codetools_0.2-18        XML_3.99-0.10          
## [73] reprex_2.0.2            glue_1.6.2              evaluate_0.16          
## [76] leaflet.providers_1.9.0 modelr_0.1.9            png_0.1-7              
## [79] vctrs_0.4.2             tzdb_0.3.0              cellranger_1.1.0       
## [82] gtable_0.3.1            assertthat_0.2.1        cachem_1.0.6           
## [85] xfun_0.33               lwgeom_0.2-9            broom_1.0.1            
## [88] e1071_1.7-11            viridisLite_0.4.1       class_7.3-20           
## [91] googledrive_2.0.0       gargle_1.2.1            units_0.8-0            
## [94] ellipsis_0.3.2