Skip to main content

Section 10.2 Using raster micro-grid cells to spot hot spots

Analysis which produces "hot spots" generally focuses on understanding small places or micro-geographical units where crime (or other topic of interest) may concentrate. There are different ways in which one could define this "micro-unit". A number of studies, recognising the nature of police data, look at this from the perspective of street segments. Another way of approaching the problem would be to define micro-grid cells. [Chainey (2021)] argues that if street segments are available, this should be the preferred level of analysis "because they act as a behavioral setting along which social activities are organized" (p. 42). Yet, as we will see throughout this chapter, most of the more traditional techniques that have been developed for detecting local clusters, and that are available in standard software for hot spot detection, were designed for points and polygons rather than lines. The focus of this chapter is on these more traditional techniques, and therefore we will use micro-grid cells as the vector object to work with throughout this chapter.
In Chapter 4 we discussed alternatives to choropleth maps, and one of these was to use a grid map. This is a process whereby a tessellating grid of a shape such as squares or hexagons is overlaid on top of our area of interest, and our points are aggregated to such a grid. We explored this using ggplot2 package. We also used this approach when discussing quadrat counting with ppp objects and using the functionality provided by the spatstat package in Chapter 7.
Here we can revisit this with another approach, using the raster package. First we need to create the grid by using the raster() function. The first argument defines the extent for the grid. By using our manchester sf object, we ensure it includes all of our study area. Then the resolution argument defines how large we want the cells in this grid to be. You can play around with this argument and see how the results will vary.
# Create empty raster grid
manchester_r <- raster(manchester, resolution = 0.004)
We can then use the rasterize() function to count the number of points that fall within each cell. This is just a form of quadrat counting as the one we introduced in Chapter 7. The rasterize() function takes as the first argument the coordinates of the points. The second argument provides the grid we just created (manchester_r). The field = argument in this case is assigned a value of 1 so that each point is counted a unit. Then we use the fun = argument to specify what we want to do with the points. In this case, we want to "count" the points within each cell. The rasterize() function by default will set at NA all the values where there are no points. We don’t want this to be the case, as no points is meaningful in our case (means 0 crimes in that grid). So that the raster includes those cells as zero, we use the background = argument and set it to zero:
# Fill the grid with the count of incidents within each cell
m_burglary_raster <- rasterize(manc_burglary,
                                manchester_r,
                                field = 1,
                                fun = "count",
                                background = 0)
We can now plot our results:
# Use base R functionality to plot the raster
plot(m_burglary_raster)
plot(st_geometry(manchester), border='#00000040', add=T)
A grey outline of Manchester is centred inside a rectangle with notches and numbers on the vertical and horizontal axis, representing latitude and longitude. A legend to the right matches a gradient from grey to orange to yellow to green, with numbers from zero to nearly one hundred. The map has small shaded squares covering it, a grey backdrop with many light and dark orange blotches all over, and concentrations reaching yellow and green in the centre, and a few areas southeast from there.
Figure 10.2.1. Raster map of burglary across Manchester
This kind of map is helpful if you want to get a sense of the distribution of crime over the whole city. However, from a crime analyst’s operational point of view, the interest may be at a more granular scale. We want to dive in and observe the micro-places that may be areas of high crime within certain parts of the city. We can do this by focusing on particular neighbourhoods. In Manchester, City Centre ward has one of the highest counts of burglaries - so let’s zoom in on here.
When we read in our files at the start of the chapter, we created the city_centre object, which contains the LSOAs in this ward, and also cc_burglary which contains only those burglaries which occurred in the city centre.
Let’s repeat the steps of creating a raster, and then the grid which we demonstrated above for all Manchester Local Authority, but now for the City Centre only.
# Create empty grid for the City Centre
city_centre_r <- raster(city_centre, resolution = 0.0009)
# Produce raster object with the number of incidents within each cell
burglary_raster_cc <- rasterize(cc_burglary,
                                city_centre_r,
                                field = 1,
                                fun = "count",
                                background = 0)
# Plot results with base R
plot(burglary_raster_cc)
plot(st_geometry(city_centre), border='#00000040', add=T)
A grey outline of the City Centre LSOA, once again inside a rectangle with latitude and longitude markings. A legend with the same colour scheme as before is matched to numbers from zero to twenty five, and shaded rectangles cover much of the map. A grey backdrop covers the whole figure, with many orange rectangles inside the borders of the city centre, with yellows and greens also appearing, mostly in the north and centre.
Figure 10.2.2. Raster map of burglary in City Centre only
You can see in the above map where hot spots might occur. However, you also see that we have created cells of 0 burglaries outside of the boundary of our City Centre ward. This is not quite true, as we didn’t include burglaries which occurred outside of here. Therefore, we should not have in this raster object incidents outside the boundaries of the neighbourhood. Instead, we want to declare those cells as NAs. We can do that with the mask() function in the raster package.
burglary_raster_cc <- mask(burglary_raster_cc, city_centre)
plot(burglary_raster_cc)
The previous shaded map of the City Centre no longer has a grey outline, and instead only has rectangles shaded grey within the borders of the LSOA. The rest of the backdrop is white.
Figure 10.2.3. Raster plot of burglary
Now you can see we indicate more clearly where we have cells which contain no burglaries at all in our data, versus where we do not have data to be able to talk about burglaries.
To better understand what might be going on in our grids with higher burglary count, let’s add some context. We will use mapview for this. This is another great package if you want to give your audience some interactivity. It does what it says in the tin:
it provides functions to very quickly and conveniently create interactive visualisations of spatial data. Its main goal is to fill the gap of quick (not presentation grade) interactive plotting to examine and visually investigate both aspects of spatial data, the geometries and their attributes. It can also be considered a data-driven API for the leaflet package as it will automatically render correct map types, depending on the type of the data (points, lines, polygons, raster) ([Appelhans et al., 2021]).
With very few lines of code, you can get an interactive plot. Here we first will use brewer.pal() from the RColorBrewer package to define a convenient palette (see Chapter 5 for details on this) and then use the mapview() function from the mapview package to plot our raster layers we created above. Below you see how we increase the transparency with the alpha.region argument, and how we use col.regions to change from the default palette to the one we created.
my.palette <- brewer.pal(n = 9, name = "OrRd")
mapview(burglary_raster_cc,
        alpha.regions = 0.7,
        col.regions = my.palette)
We can now explore the underlying environmental backcloth behind the areas with high burglary count and build hypotheses and research questions about what might be going on there; for example whether these are crime attractors, crime generators, or other types of crime places. However, keep in mind that these are illustrations of the counts of observed burglaries, and there is nothing here which declares something a "hot spot". For that, keep reading!