Section 3.5 Interactive mapping with tmap
So far we have been producing static maps with
tmap. But this package also allows for interactive mapping by linking with leaflet. To change whether the plotted maps are static or interactive, we need to use the tmap_mode() function. The default is tmap_mode("plot"), which corresponds to static maps. If we want to change to interactive display, we need to change the argument we pass to tmap_mode("view").
tmap_mode("view")
## tmap mode set to interactive viewing
When you use
tmap, R will remember the mode you want to use. So once you specify tmap_mode("view"), all the subsequent maps will be interactive. It is only when you want to change this behaviour that you would need another tmap_mode() call. When using the interactive view, we can also add a basemap with the tm_basemap() function and passing as an argument a particular source for the basemap. Here we specify OpenStreetMap, but there are many other choices.
Let’s explore the distribution of the two alternative definitions of crime rates in an interactive way.
tm_shape(manchester) +
tm_fill("crimr1", style="jenks", palette= "Reds",
title = "Crime per residential pop", alpha = 0.6) +
tm_basemap(leaflet::providers$OpenStreetMap)
If you are following along, you can now scroll down to see that the crime rate is highest in the city centre of Manchester, but there are also pockets of high level of the crime rate in the North East of the city, Harpurhey and Moston (areas of high levels of deprivation) and in the LSOA farthest to the South (where the international airport is located).
How does this change if we use the crime rate that uses the workday population?
tm_shape(manchester) +
tm_fill("crimr2", style="jenks", palette= "Oranges",
title = "Crime per workday pop", alpha = 0.8) +
tm_basemap(leaflet::providers$OpenStreetMap)
Things look different, don’t they? For starters look at the values in the labels for the various classes. They are much less extreme. One of the reasons why we see such extreme rates in the first map is linked to the very large discrepancy between the residential population and the workday population in some parts of Manchester, like the international airport and the city centre (that attract very large volume of visitors, but have few residents). The LSOA with the highest crime rate (when using the residential population as the denominator) is E01033658. We can filter to find the number of crimes (in the
count variable) that took place here.
manchester %>% filter(code=="E01033658") %>% select(code, count, respop, wkdpop)
| code | count | respop | wkdpop |
|---|---|---|---|
| E01033658 | 744 | 1303 | 42253 |
We can see in this area there were 744 crime incidents for a residential population of 1303, but the workday population is as high as 42253 people. So, of course, using these different denominators is bound to have an impact in the resulting rate. As noted earlier, the most appropriate denominator represents some measure of the population at risk.
Once again, we can rely on some advice from [Cameron (2005)]:
In the final analysis, although choropleth maps are very useful for visualizing spatial distributions, using them for hot spot analyses of crime has certain disadvantages. First, attention is often focused on the relative size of an area, so large areas tend to dominate the map. Second, choropleth maps involve the aggregation of data within statistical or administrative areas that may not correspond to the actual underlying spatial distribution of the data.
