Skip to main content

Section 1.9 Mapping crime data as points

So how can we use this for spatial data? We can use the geom_sf() function to do so. Using geom_sf is slightly different to other geometries, for example how we used geom_col() above. First we initiate the plot with the ggplot() function but don’t include the data in there. Instead, it is in the geometry where we add the data. And second we don’t need to specify the mapping of x and y, since this is in the geometry column of our spatial object. Like so:
ggplot() +
  geom_sf(data = crimes_sf)
A plot of black points on a grey grid, the horizontal axis being longitude ranging from about two point seven degrees west to two degrees west, and the vertical axis latitudes ranging from about fifty three point three five degrees north to fifty three point six five degrees north. The points form a blob in the general shape of Greater Manchester.
Figure 1.9.1. Using geom sf for plotting
And here we have a map of each point in our dataset, each recorded crime in June 2019 in Greater Manchester. Would you call this a map though? While it is presenting spatial data, there is not a lot of meaning being communicated. Point maps generally can be messy, and their uses are specific to certain situations and cases, usually when you have fewer points. But here, these points are especially devoid of any meaning, as they are floating in a graph grid. So let’s give it a basemap.
We can do this by adding a layer to our graph object. Specifically, we will use the annotation_map_tile() from the ggspatial package. This provides us with a static Open Street Map layer behind our data, giving it (some) more context. Remember to load the package (and install if you haven’t already). And then use the annotation_map_tile() function, making sure to place it before the geom_sf points layer, so the background map is placed first, and the points on top of that:
library(ggspatial)

ggplot() +
 annotation_map_tile() +
  geom_sf(data = crimes_sf)
The previous plot, with a map background replacing the grey grid, showing Manchester and its surroundings.
Figure 1.9.2. Adding a basemap
So what you see above behind the points is what we call a basemap. The term basemap is seen often in GIS and refers to a collection of GIS data and/or orthorectified imagery that form the background setting for a map. The function of the basemap is to provide background detail necessary to orient the location of the map. Basemaps also add to the aesthetic appeal of a map. Basemaps are essentially reference maps that may give us context and help with the interpretation. You can see above the Open Street Map Basemap. This is one option, but there are others.
Let’s leave the points for now and move on to how we might map our lines and polygons.