Section 4.2 Transforming polygons: cartograms
When you have meaningful spatial units of analysis in your polygons, for example you are interested specifically in Local Authorities, it might make sense to aggregate the points into these meaningful polygons to create thematic maps. However, while thematic maps are an accessible and visually appealing method for displaying spatial information, they can also be highly misleading. Irregularly shaped polygons and large differences in the size of areas being mapped can introduce misrepresentation. The message that analysts and researchers want to get across might be lost, or even worse, it may misdirect the viewers to erroneous conclusions. [Field (2016)] provides a helpful discussion of the problem illustrating the case with UK election maps. It is worth reading.
Fortunately, there are many methods in R to enhance the legibility of geographic information and the interpretability of what it is trying to communicate. Selecting the appropriate method might depend on the research question being posed (e.g., clustering) and the data itself. Here we focus on cartograms. A cartogram is a method of distortion whereby distinct definable spatial regions on a map (e.g., countries, provinces, etc.) are distorted in some way through being weighted by some quantitative value (e.g., population count, carbon emissions, etc.) ([Kirk, 2016]).
Let’s explore this using the example of the results of the 2016 EU referendum which took place in the United Kingdom, where citizens voted to leave the European Union (in a process termed “Brexit”). We can look at the data on voting results at Local Authority level. It is well known that those who voted to remain in the EU predominantly clustered in the Local Authorities within London. A simple thematic map does not necessarily communicate this well because Local Authorities are both small and densely populated in London, compared to much larger elsewhere.
You can download the full set of EU referendum result data as a csv from the Electoral Commission website. We’ve already done this and included it in our data supplied with the book. Let’s read it straight into R:
eu_ref <- read_csv("data/EU-referendum-result-data.csv")
We also need a spatial object to join our attribute table to, in order to map it. In Appendix C: Sourcing geographical data for crime analysis, we detail how one might go about finding such spatial data, if it is not already provided. For now, we can use the data which comes with this book from the supplementary materials. Specifically the shapefile for English Local Authorities. This file is called
england_lad_2011_gen.shp and is found in the England_lad_2011_gen sub-folder.
las <- st_read("data/England_lad_2011_gen/england_lad_2011_gen.shp")
We can now join the EU referendum data using the attribute operation
left_join(), as we have illustrated in detail in Chapter 1.
eu_sf <- left_join(las, eu_ref, by = c("name" = "Area"))
#make sure we are in British National Grid Projection
eu_sf <- st_transform(eu_sf, 27700)
Now we can have a look at these data:
ggplot() +
geom_sf(data = eu_sf, aes(fill = Pct_Leave)) +
theme_void()

The Local Authorities (LAs) vary greatly in their shape and size, and in the case of smaller LAs the result is barely visible. In this case, we cannot really see what was happening with the EU referendum in London, for example. This is the sort of situation where augmenting our polygons may be handy. Cartograms offer one way to achieve this.
There are different types of cartograms. Density-Equalizing (contiguous) Cartograms are your traditional cartograms. In density-equalizing cartograms, map features bulge out a specific variable. Even though it distorts each feature, it remains connected during its creation. On the other hand, you can have Non-Contiguous Cartograms, where features in non-contiguous cartograms don’t have to stay connected. Finally, Dorling Cartograms (named after Professor Danny Dorling ([Dorling, 1991])) use shapes like circles and rectangles to depict areas. These types of cartograms make it easy to recognize patterns.
We can explore cartograms using the
cartogram package. Within that we will use the cartogram() function. In this function, we will specify two parameters: first the shp =, which asks for the shape file (it can be a SpatialPolygonDataFrame or an sf object), and second, weight =, which asks for the variable which it should use to distort the polygon by.
In our data set we have a variable
Electorate which refers to the total number of registered electors in that Local Authority. It serves to give an indicator of the total number of people who were eligible to vote in the referendum. We can use this variable to distort our polygons and create our cartogram.
# construct a cartogram using the percentage voting leave
eu_cartogram <- cartogram_cont(eu_sf, "Electorate")
If you run this, it might take a long time. This function, while it looks nice and simple, is actually very computationally taxing for your computer. For those interested, you may like to take the time while R works this out for you to read up on the maths behind this transformation in [Dougenik, Chrisman, and Niemeyer (1985)] (it’s got a fun name: a rubber sheet distortion algorithm!).
# construct a cartogram using the percentage voting leave
eu_cartogram <- cartogram_cont(eu_sf, "Electorate", itermax = 5)
I do have a tip for you if you want to make sure the process does not take too long You can set another parameter in the cartogram function which is the
itermax= parameter. This specifies the maximum number of iterations we are happy to sit through for our cartogram. If you do not specify an alternative the default is set to 15. Let’s set to 5 for the sake of speed:
This will be faster (but may not result in the best possible cartogram output). Once your cartogram has been created, you can now plot again the referendum results, but using the electorate to change the size of the Local Authority:
ggplot() +
geom_sf(data = eu_cartogram, aes(fill = Pct_Leave)) +
theme_void()

We can now see London much better, and see that darker-coloured cluster where a much smaller percentage of people voted leave. We can eyeball where London may be, as this continuous area cartogram tries to maintain some fidelity to our original shapes, while weighting them by some variable of interest, in our case the electorate in each Local Authority.
Subsection 4.2.1 Dorling cartogram
While the continuous area cartogram we created above tries to maintain some fidelity to our original shapes, other approaches take more freedom when applying transformations. Sometimes, maintaining a resemblance to the original geometry of each polygon may not be important. In that case, you might be interested in creating a Dorling cartogram. We can achieve this by using the
cartogram_dorling() function. Again we specify the sf object, and the variable which we use to scale our areas.
# construct a Dorling cartogram using the percentage voting leave
eu_dorling_cartogram <- cartogram_dorling(eu_sf, "Electorate")
ggplot() +
geom_sf(data = eu_dorling_cartogram, aes(fill = Pct_Leave)) +
theme_void()

This map has transformed each Local Authority’s shape into a circle, where the radius is determined as a function of the variable we supplied, which is our Electorate in each LA. The shading here is again provided by the percentage of people who voted to leave in each area, with lighter values indicating more people voting to leave, and darker values indicating fewer people voting to leave the EU. However, the relations of these Local Authorities is tough to maintain here, and we may be hard-pressed to identify London’s boroughs out of this collection. However, Dorling cartograms may have their use and place. You may read up more about them in [Dorling (1996)].
