setwd("C:/Users/PC/Desktop/R")
library(sp)
library(GISTools)
## Loading required package: maptools
## Checking rgeos availability: TRUE
## Please note that 'maptools' will be retired by the end of 2023,
## plan transition at your earliest convenience;
## some functionality will be moved to 'sp'.
## Loading required package: RColorBrewer
## Loading required package: MASS
## Loading required package: rgeos
## rgeos version: 0.5-8, (SVN revision 679)
##  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-5 
##  Polygon checking: TRUE
library(sf)
## Linking to GEOS 3.9.1, GDAL 3.2.1, PROJ 7.2.1
library(tmap)
library(tmaptools)


#importing the data
Nairobi_County<-st_read("Nairobi_county.geojson")
## Reading layer `Nairobi_county' from data source 
##   `C:\Users\PC\Desktop\R\Nairobi_county.geojson' using driver `GeoJSON'
## Simple feature collection with 17 features and 14 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 36.66348 ymin: -1.442148 xmax: 37.10367 ymax: -1.157977
## Geodetic CRS:  WGS 84
Police_stations<-read.csv("Nairobi_policestations&policeposts.csv")

#converting the police stations data into sf
   #defining the coordinates
coordinates<-cbind(Police_stations$x, Police_stations$y)

#create the SpatialPointsDataFrame
Police_stations_sp<-SpatialPointsDataFrame(
  coordinates,data = data.frame(Police_stations),
  proj4string = CRS("+proj=longlat")
)

#converting to sf
Police_stations_sf<-st_as_sf(Police_stations_sp)

#making the map
tm_shape(Nairobi_County)+
  tm_grid()+
  tm_polygons(border.col = "black",fill="grey")+
  tm_text("ADM2_EN", 
          size =0.6,)+
  tm_shape(Police_stations_sf)+
  tm_dots("TYPE",
          shape = 19, 
          size = 0.2,
          palette="YlOrRd"
          )+
  tm_scale_bar(width = 0.15,position =c("left","bottom"))+
  tm_compass(position = c("right","top"),size = 0.5)+
  tm_layout(main.title = "DESIGNATED POLICE STATIONS IN NAIROBI",
            main.title.size =0.7,
            main.title.position ="center")