2

I have asked this question in the GIS part of stack exchange https://gis.stackexchange.com/questions/95265/r-how-to-create-a-pre-determined-number-of-identical-square-polygons-to-use-fo - I am asking it here as well as it has also topics of wider interest (e.g. calculation of density) - I hope not to be penalised for this! :)

I am trying to plot crime data density (again!) over a city map, say of NY. As a well known problem there are plenty of examples on this (http://www.obscureanalytics.com/2012/12/07/visualizing-baltimore-with-r-and-ggplot2-crime-data/). These methods plot the crime density through isoclines, while I need to represent it through identical density squares of a pre-determined area (and the area / side length may change from one iteration to the other). This is actually done in commercially available COTS packages like PredPol (see http://www.predpol.com). The reason for representing crime density through squares is that the square are the hotspot areas to be patrolled. The size will influence the overall amount of police people required.

This is what I am trying to achieve:

I would like to be able to create identical square polygons with pre-determined size to overimpose to the map (is it a raster? apologies but I've just started to learn to spell GIS!) I would like to use the above squares as items to colour as in a choropleth map (i.e. different colouring in relation to frequency of crime in the area), probably using ggplot2 or similar. This should allow me to see how the density of crimes per square kilometre varies changing the size (i.e. the area) of the square, proposing different patrolling areas. I do not have a clue if it is possible to use R to create regularly shaped squares polygons of a pre-defined size to use for this (as the code snipped below attests). Any help or links to examples are welcome.

I would be glad to get some indication on alternative ways to calculate the density. I have used the stat_density2 (part of ggplot2) but maybe there are better / faster ways? ( In hindsight, do I need a density function at all? I just need to count the crimes in a cell and colour-plot it accordingly...)

This is where I got to:

library(rgdal)
library(raster)
library(sp)
#NY boroughs shapefile downloaded from NY website 
shp <- readOGR(dsn = "nybb_14a_av", layer = "nybb")
r <- raster(extent(shp))
res(r)=0.05
# using BoroCode as an experiment...
r <- rasterize(shp, field="BoroCode", r)
plot(r)
plot(shp,lwd=10,add=TRUE)
#don't know the result of the above: the laptop basically hangs processing
#plot(r) :)
Community
  • 1
  • 1
Enzo
  • 2,543
  • 1
  • 25
  • 38
  • What programming language are you using and what have you come up with so far? – man-qa May 07 '14 at 09:43
  • I am using R and ggplot2 package (e.g.(http://www.obscureanalytics.com/2012/12/07/visualizing-baltimore-with-r-and-ggplot2-crime-data/). My problem is that usually density is expressed through isoclines, while I need (variable) squares as in a heat map matrix of sort, imposed over a city map. – Enzo May 07 '14 at 09:52
  • I've just seen an interesting post here (http://gis.stackexchange.com/questions/18311/instantiating-spatial-polygon-without-using-a-shape-file-in-r). It explains how to build polygons in R from a set of coordinates. Assuming to use the linked solution, how do I generate the coordinates so that: a) the resulting polygons are all sized say 500m x 500m; b) they are all sort of contained in the city limit (e.g. in Chicago case, no polygon over the lake!)? – Enzo May 07 '14 at 11:10
  • I've found a good post here (http://stackoverflow.com/questions/9989508/creating-random-polygons-within-a-set-shapefiles-boundary-in-r) that suggests to use the raster package. I'll try it, but I'm still puzzled by how to create the polygons in a specific size / area. – Enzo May 07 '14 at 11:46
  • 1
    I'd use something like `PostGIS` for this but you can probably use `R`. Quickest way is probably `QGIS` with Vector Grid. Then you would do a spatial join to remove areas you don't want included once you have those areas in vectors as well. Then in `PostGIS` you do a spatial join to perform counts and colorize with `R`. – ideamotor May 22 '14 at 18:30
  • Looks promising and definitely worth a try. So far the closest that I found within R is to create a Tessellation with the package spatstat. It is not ideal as it does not let you choose the size of the Tessellation (or the area). You can just say how many cells you want on a polygon. I haven't tried to specify different properties in each cell. As a side issue for >100 cells it is quite also quite slow. +1 to ideamotor for the first useful comment to my post! Thanks – Enzo May 22 '14 at 18:46
  • Np. This looks like a promising solution if you are using `PostGIS` http://gis.stackexchange.com/questions/16374/how-to-create-a-regular-polygon-grid-in-postgis. Another route might be accessing GRASS through R but that looks tedious - as does building vector grids in `R` libraries IMO, although some people have discussed related topics in R-Sig-Geo. This looks like a decent reference on spatial joins in `R` (looks far more complicated than PostGIS joins): http://r-sig-geo.2731867.n2.nabble.com/Spatial-join-using-shapefiles-with-R-td7173057.html. – ideamotor May 22 '14 at 19:11
  • FYI, I just started reading 'Applied Spatial Data Analysis in R' and you can definitely do this all in R. As far as making the grid in `R` it looks like you want to make a SpatialGrid using GridTopology() – ideamotor May 29 '14 at 19:22

0 Answers0