In this in class exercise, I gain a deeper understanding on the different functions to perform spatial point patterns analysis.
My notes:
packages = c('maptools', 'sf', 'raster','spatstat', 'tmap', 'tidyverse')
for (p in packages){
if(!require(p, character.only = T)){
install.packages(p)
}
library(p,character.only = T)
}
# onemapsgapi
My notes:
maptools: Manipulate geographic data.
sf: used for importing, managing, and processing geospatial data
raster: reads, writes, manipulates, analyses and model of gridded spatial data
spatstat: contains useful functions for point pattern analysis.
tmap: provides functions for plotting cartographic quality static point patterns maps or interactive maps by using leaflet API.
tidyverse (for CHILDCARE dataset): used for importing, wrangling and visualising data and consists of a family of packages:
sg_sf <- st_read(dsn="data/shapefile", layer="CostalOutline")
Reading layer `CostalOutline' from data source
`C:\aisyahajit2018\IS415\IS415_blog\_posts\2021-09-06-in-class-exercise-4\data\shapefile'
using driver `ESRI Shapefile'
Simple feature collection with 60 features and 4 fields
Geometry type: POLYGON
Dimension: XY
Bounding box: xmin: 2663.926 ymin: 16357.98 xmax: 56047.79 ymax: 50244.03
Projected CRS: SVY21
mpsz_sf <- st_read(dsn = "data/shapefile", layer = "MP14_SUBZONE_WEB_PL")
Reading layer `MP14_SUBZONE_WEB_PL' from data source
`C:\aisyahajit2018\IS415\IS415_blog\_posts\2021-09-06-in-class-exercise-4\data\shapefile'
using driver `ESRI Shapefile'
Simple feature collection with 323 features and 15 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: 2667.538 ymin: 15748.72 xmax: 56396.44 ymax: 50256.33
Projected CRS: SVY21
childcare <- read_rds("data/rds/childcare.rds")
CHAS <- read_rds("data/rds/CHAS.rds")
My notes:
** Difference between readRDS and read_rds:
CHAS_sf <- st_as_sf(CHAS,
coords = c("X_COORDINATE",
"Y_COORDINATE"),
crs=3414)
My notes:
childcare$Lat <- as.numeric(childcare$Lat)
childcare$Lng <- as.numeric(childcare$Lng)
childcare_sf <- st_as_sf(childcare,
coords=c("Lng", "Lat"),
crs=4326) %>%
st_transform(crs=3414)
childcare <- as_Spatial(childcare_sf)
CHAS <- as_Spatial(CHAS_sf)
mpsz <- as_Spatial(mpsz_sf)
sg <- as_Spatial(sg_sf)
childcare_sp <- as(childcare, "SpatialPoints")
CHAS_sp <- as(CHAS, "SpatialPoints")
sg_sp <- as(sg, "SpatialPolygons")
My notes:
childcare_ppp <- as(childcare_sp, "ppp")
CHAS_ppp <- as(CHAS_sp, "ppp")
plot(childcare_ppp)
plot(CHAS_ppp)
summary(childcare_ppp)
Planar point pattern: 1545 points
Average intensity 1.91145e-06 points per square unit
*Pattern contains duplicated points*
Coordinates are given to 3 decimal places
i.e. rounded to the nearest multiple of 0.001 units
Window: rectangle = [11203.01, 45404.24] x [25667.6, 49300.88] units
(34200 x 23630 units)
Window area = 808287000 square units
summary(CHAS_ppp)
Planar point pattern: 1192 points
Average intensity 5.39042e-07 points per square unit
*Pattern contains duplicated points*
Coordinates are given to 3 decimal places
i.e. rounded to the nearest multiple of 0.001 units
Window: rectangle = [0, 45475.65] x [0, 48626.7] units
Window area = 2211330000 square units
tmap_mode('view')
tm_shape(childcare_sf) +
tm_dots(alpha=0.4,
col="blue",
size=0.05) +
tm_shape(CHAS_sf) +
tm_dots(alpha=0.4,
col="red",
size=0.05)
Here we are switching back to plot mode:
tmap_mode('plot')