This example shows how to use R and QGIS from within R to perform a series of common point pattern analysis techniques.

library(mapview)
library(sf)
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(censusxy)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
addr<-read.csv(url("https://raw.githubusercontent.com/coreysparks/data/master/wic_west_side.csv"))
addr<-addr[c(6, 12:14)]
names(addr)<-c("street", "city", "st", "zip")
head(addr)
results<-cxy_geocode(addr,
                     street = "street",
                     city = "city",
                     state ="st",
                     zip = "zip",
                     class="sf",
                     output = "simple")
## 36 rows removed to create an sf object. These were addresses that the geocoder could not match.
results.proj<-st_transform(results,
                           crs = 2278)

OR just use the lat / long information in the data!

addr<-read.csv(url("https://raw.githubusercontent.com/coreysparks/data/master/wic_west_side.csv"))
results <- st_as_sf(addr, coords=c("Longitude", "Latitude"), crs=4269,agr="constant")
results.proj<-st_transform(results,
                           crs = 2278)
mapview(results.proj)

mean feature - average of coordinates

mean_feature<-apply(st_coordinates(results.proj), MARGIN = 2, FUN = mean)
mean_feature<-data.frame(place="meanfeature", x=mean_feature[1], y= mean_feature[2])
mean_feature<-st_as_sf(mean_feature, coords = c("x", "y"), crs= 2278)

mapview(mean_feature, col.regions="red")+mapview( results)

Central feature - Median of coordinates

median_feature<-apply(st_coordinates(results.proj), MARGIN = 2, FUN = median)
median_feature<-data.frame(place="medianfeature", x=median_feature[1], y= median_feature[2])
median_feature<-st_as_sf(median_feature, coords = c("x", "y"), crs= 2278)

mapview(median_feature, col.regions="green")+
  mapview(mean_feature, col.regions="red")+
  mapview( results)

Buffer points

wicbuff<- st_buffer(results.proj, dist = 2500)
mapview(wicbuff)+mapview(results.proj, col.regions="green")

Convex hull plot

chull <- st_convex_hull(st_union(results))
## although coordinates are longitude/latitude, st_union assumes that they are planar
mapview(chull)+
  mapview(results, col.regions = "green")

kernel density - You need projected data for this to work right

R can do kernel density maps, but using simple features it’s kind of complicated. I will use Qgis through R instead using the qgisprocess package

library(qgisprocess)
## Using 'qgis_process' at 'C://OSGeo4W64/bin/qgis_process-qgis.bat'.
## Run `qgis_configure()` for details.
qgis_configure()
## getOption('qgisprocess.path') was not found.
## Sys.getenv('R_QGISPROCESS_PATH') was not found.
## Trying 'qgis_process' on PATH
## Error in rethrow_call(c_processx_exec, command, c(command, args), stdin, : Command 'qgis_process' not found @win/processx.c:994 (processx_exec)
## Found 2 QGIS installations containing 'qgis_process':
##  C://OSGeo4W64/bin/qgis_process-qgis.bat
## C://OSGeo4W64/bin/qgis_process-qgis-dev.bat
## Trying command 'C://OSGeo4W64/bin/qgis_process-qgis.bat'
## Success!

To use this, we need to find the name of the Qgis algorithm we want. qgis_algorithms() can return all available algorithms, then we can either filter it with View() or use grep to search for one.

algs<-qgis_algorithms()
algs[grepl(pattern = "density", x = algs$algorithm ),]
qgis_show_help("qgis:heatmapkerneldensityestimation")
## C:\Users\ozd504\Documents\GitHub\DEM7093\code
## Heatmap (Kernel Density Estimation) (qgis:heatmapkerneldensityestimation)
## 
## ----------------
## Description
## ----------------
## 
## ----------------
## Arguments
## ----------------
## 
## INPUT: Point layer
##  Argument type:  source
##  Acceptable values:
##      - Path to a vector layer
## RADIUS: Radius
##  Argument type:  distance
##  Acceptable values:
##      - A numeric value
## RADIUS_FIELD: Radius from field
##  Argument type:  field
##  Acceptable values:
##      - The name of an existing field
##      - ; delimited list of existing field names
## PIXEL_SIZE: Output raster size
##  Argument type:  number
##  Acceptable values:
##      - A numeric value
## WEIGHT_FIELD: Weight from field
##  Argument type:  field
##  Acceptable values:
##      - The name of an existing field
##      - ; delimited list of existing field names
## KERNEL: Kernel shape
##  Argument type:  enum
##  Available values:
##      - 0: Quartic
##      - 1: Triangular
##      - 2: Uniform
##      - 3: Triweight
##      - 4: Epanechnikov
##  Acceptable values:
##      - Number of selected option, e.g. '1'
##      - Comma separated list of options, e.g. '1,3'
## DECAY: Decay ratio (Triangular kernels only)
##  Argument type:  number
##  Acceptable values:
##      - A numeric value
## OUTPUT_VALUE: Output value scaling
##  Argument type:  enum
##  Available values:
##      - 0: Raw
##      - 1: Scaled
##  Acceptable values:
##      - Number of selected option, e.g. '1'
##      - Comma separated list of options, e.g. '1,3'
## OUTPUT: Heatmap
##  Argument type:  rasterDestination
##  Acceptable values:
##      - Path for new raster layer
## 
## ----------------
## Outputs
## ----------------
## 
## OUTPUT: <outputRaster>
##  Heatmap

Run the algorithm

wic_dens<-qgis_run_algorithm(algorithm ="qgis:heatmapkerneldensityestimation",
         INPUT=results.proj,
         RADIUS = 5000,
         PIXEL_SIZE = 100,
         KERNEL = 0,
         OUTPUT=file.path(getwd(), "wicdenst.TIF"),
         load_output = TRUE)
## Running "C://OSGeo4W64/bin/qgis_process-qgis.bat" run \
##   "qgis:heatmapkerneldensityestimation" \
##   "--INPUT=C:\Users\ozd504\AppData\Local\Temp\RtmpmoOrgu\file202815a448f1\file202843437b8.gpkg" \
##   "--RADIUS=5000" "--PIXEL_SIZE=100" "--KERNEL=0" "--OUTPUT_VALUE=0" \
##   "--OUTPUT=C:/Users/ozd504/Documents/GitHub/DEM7093/code/wicdenst.TIF"
## C:\Users\ozd504\Documents\GitHub\DEM7093\code
## 
## ----------------
## Inputs
## ----------------
## 
## INPUT:   C:\Users\ozd504\AppData\Local\Temp\RtmpmoOrgu\file202815a448f1\file202843437b8.gpkg
## KERNEL:  0
## OUTPUT:  C:/Users/ozd504/Documents/GitHub/DEM7093/code/wicdenst.TIF
## OUTPUT_VALUE:    0
## PIXEL_SIZE:  100
## RADIUS:  5000
## 
## 
## 0...10...20...30...40...50...60...70...80...90...
## ----------------
## Results
## ----------------
## 
## OUTPUT:  C:/Users/ozd504/Documents/GitHub/DEM7093/code/wicdenst.TIF
library(raster)
library(RColorBrewer)

result<- qgis_as_raster(wic_dens)

projection(result)<-crs(results.proj)
mapview(result)+mapview(results.proj)

Spatial join

A spatial join can combine attributes of one layer with another layer. Here I combine census variables with the WIC clinic points.

library(tidycensus)
library(dplyr)
#load census tract data
sa_acs<-get_acs(geography = "tract",
                state="TX",
                county = "Bexar", 
                year = 2019,
                variables=c( "DP05_0001E", "DP03_0009P", "DP03_0062E", "DP03_0119PE",
                           "DP05_0001E","DP02_0009PE","DP02_0008PE","DP02_0040E","DP02_0038E",
                            "DP02_0066PE","DP02_0067PE","DP02_0080PE","DP02_0092PE",
                        "DP03_0005PE","DP03_0028PE","DP03_0062E","DP03_0099PE","DP03_0101PE",
                            "DP03_0119PE","DP04_0046PE","DP05_0072PE","DP05_0073PE",
                            "DP05_0066PE", "DP05_0072PE", "DP02_0113PE") ,
                geometry = T, output = "wide")
## Getting data from the 2015-2019 5-year ACS
## Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
## Using the ACS Data Profile
## Using the ACS Data Profile
#rename variables and filter missing cases
sa_acs2<-sa_acs%>%
  mutate(totpop= DP05_0001E, pwhite=DP05_0072PE, 
         pblack=DP05_0073PE , phisp=DP05_0066PE,
         phsormore=DP02_0066PE,punemp=DP03_0009PE, medhhinc=DP03_0062E,
         ppov=DP03_0119PE)%>%
  dplyr::select(GEOID, totpop, pblack, pwhite, phisp, punemp, medhhinc, ppov)

sa_acs2<-st_transform(sa_acs2, crs = 2278)
sa_trol<-st_cast(sa_acs2, "MULTILINESTRING")
spjoin<-st_join(results.proj, sa_acs2)
head(spjoin)
mapview(spjoin["punemp"])+mapview(sa_trol)

Count points in polygons

Point in polygon operations are actually a spatial intersection (more on this next week!) where we see how many points fall within a given polygon.

sa_acs2$nwic<- lengths(st_intersects(sa_acs2, results.proj))
mapview(sa_acs2, zcol="nwic")+
  mapview(results.proj, col.regions = "green")

Thiessen/Voronoi Polygons

Thiessen or Voronoi polygons are a process where we can convert points into polygons.

algs[grepl(pattern = "voronoi", x = algs$algorithm ),]
qgis_show_help("qgis:voronoipolygons")
## C:\Users\ozd504\Documents\GitHub\DEM7093\code
## Voronoi polygons (qgis:voronoipolygons)
## 
## ----------------
## Description
## ----------------
## This algorithm takes a points layer and generates a polygon layer containing the voronoi polygons corresponding to those input points.
## 
## 
## ----------------
## Arguments
## ----------------
## 
## INPUT: Input layer
##  Argument type:  source
##  Acceptable values:
##      - Path to a vector layer
## BUFFER: Buffer region (% of extent)
##  Argument type:  number
##  Acceptable values:
##      - A numeric value
## OUTPUT: Voronoi polygons
##  Argument type:  sink
##  Acceptable values:
##      - Path for new vector layer
## 
## ----------------
## Outputs
## ----------------
## 
## OUTPUT: <outputVector>
##  Voronoi polygons
wic_von<-qgis_run_algorithm(alg="qgis:voronoipolygons",
         INPUT=results.proj,
         OUTPUT=file.path(tempdir(), "wicvon.shp"),
         load_output = TRUE)

wic_von<-sf::read_sf(qgis_output(wic_von, "OUTPUT"))

mapview(wic_von, alpha=.75)+
  mapview(results.proj, col.regions="green")

Nearest Neighbor analysis

library(spatstat)
wic.pp<-as.ppp(as(results.proj, "Spatial"))

plot(nearest.neighbour(wic.pp))

algs[grepl(pattern = "nearest", x = algs$algorithm ),]
qgis_show_help("native:nearestneighbouranalysis")
## C:\Users\ozd504\Documents\GitHub\DEM7093\code
## Nearest neighbour analysis (native:nearestneighbouranalysis)
## 
## ----------------
## Description
## ----------------
## This algorithm performs nearest neighbor analysis for a point layer.
## 
## The output describes how the data are distributed (clustered, randomly or distributed).
## 
## Output is generated as an HTML file with the computed statistical values.
## 
## ----------------
## Arguments
## ----------------
## 
## INPUT: Input layer
##  Argument type:  source
##  Acceptable values:
##      - Path to a vector layer
## OUTPUT_HTML_FILE: Nearest neighbour
##  Argument type:  fileDestination
##  Acceptable values:
##      - Path for new file
## 
## ----------------
## Outputs
## ----------------
## 
## OUTPUT_HTML_FILE: <outputHtml>
##  Nearest neighbour
## OBSERVED_MD: <outputNumber>
##  Observed mean distance
## EXPECTED_MD: <outputNumber>
##  Expected mean distance
## NN_INDEX: <outputNumber>
##  Nearest neighbour index
## POINT_COUNT: <outputNumber>
##  Number of points
## Z_SCORE: <outputNumber>
##  Z-score
wic_nn<-qgis_run_algorithm(alg="native:nearestneighbouranalysis",
         INPUT=results.proj,
        OUTPUT_HTML_FILE=file.path(tempdir(), "wicnn.html"),
         load_output = TRUE)
## Ignoring unknown input 'load_output'
## Running "C://OSGeo4W64/bin/qgis_process-qgis.bat" run \
##   "native:nearestneighbouranalysis" \
##   "--INPUT=C:\Users\ozd504\AppData\Local\Temp\RtmpmoOrgu\file202815a448f1\file202872c11db.gpkg" \
##   "--OUTPUT_HTML_FILE=C:\Users\ozd504\AppData\Local\Temp\RtmpmoOrgu/wicnn.html"
## C:\Users\ozd504\Documents\GitHub\DEM7093\code
## The system cannot find the file C:\rtools40\Version.txt.
## 
## ----------------
## Inputs
## ----------------
## 
## INPUT:   C:\Users\ozd504\AppData\Local\Temp\RtmpmoOrgu\file202815a448f1\file202872c11db.gpkg
## OUTPUT_HTML_FILE:    C:\Users\ozd504\AppData\Local\Temp\RtmpmoOrgu/wicnn.html
## 
## 
## 0...10...20...30...40...50...60...70...80...90...100 - done.
## 
## ----------------
## Results
## ----------------
## 
## EXPECTED_MD: 1356.5835389412343
## NN_INDEX:    0.4987323168352079
## OBSERVED_MD: 676.5720513566673
## OUTPUT_HTML_FILE:    C:\Users\ozd504\AppData\Local\Temp\RtmpmoOrgu/wicnn.html
## POINT_COUNT: 100
## Z_SCORE: -9.589602141964955