Skip to main content

Section 5.1 Data representation

Subsection 5.1.1 Thematic maps

We’ve been working with thematic maps thus far in this book. There are many decisions that go into making a thematic map, which we have explored at length in the previous chapters, such as how (and whether) to bin your data (Chapter 3) and how (or whether) to transform your polygons (Chapter 4). These are important considerations on how to represent your data to your audience, and require a technical understanding, not only an aesthetic one. So please do read over those chapters carefully when thinking about how to represent your data.
We’ve covered a few approaches to mapping (using ggplot2, tmap, and leaflet), but here we will continue with ggplot2 package to plot our thematic maps, specifically a choropleth map. We will map our sf objects using the geom_sf() function. To shade each polygon with the values of a specific variable, we use the fill = argument within the aes() (aesthetics) function. Most simply:
library(ggplot2)

map <- ggplot(data = hu_dd) + # specify data to use
  geom_sf(aes(fill = total_breath_tests)) # specify aestetics

map
A map of Hungary broken into its counties, shaded in various shades of blue, with grey borders between them. The background is a grey grid, with the axes labeled with longitudes and latitudes. A legend to the right is labeled ’total breath tests’, with a gradient of light blue to dark blue, with notches every ten thousand values. The lightest blue is just over thirty thousand. Lighter colours appear in counties to the east and some parts of the west.
Figure 5.1.1. Quick thematic map with ggplot2
Maps made with ggplot are automatically placed upon a grid reference to our data (see our brief overview of building plots with ggplot in Chapter 1). To remove this, we can use the theme_void() theme, which will strip this away.
map <- map +
  theme_void()  # remove grid

map
The previous map no longer has axes, their labels, or the grey grid in the background.
Figure 5.1.2. Add theme_void() to ggplot map
We can change the colour and size of the borders of our polygons with arguments inside the geom_sf() function, but outside the aes() function, as long as we’re not using our data to define these. For example, we can change the line width (lwd =) to 0, eliminating bordering lines between our polygons:
ggplot(data = hu_dd) +
  geom_sf(aes(fill = total_breath_tests), lwd = 0) +  # specify line width
  theme_void()
The previous map, now with the borders between the counties being much thinner.
Figure 5.1.3. Thematic map with border lines removed
Or we can change the colour of the borders with the col = argument:
ggplot(data = hu_dd) +
  geom_sf(aes(fill = total_breath_tests), lwd = 0.5,
          col = "white") + # specify border colour
  theme_void()
The map has now regained its thick borders, but they are coloured white.
Figure 5.1.4. Thematic map with white borders
Here we have a continuous fill for our values; however, we can employ our learning from Chapter 3 and apply a classification system, such as quantiles. To do this, we might create a new variable which contains the quantiles of our numeric variable, and then use that as our fill =.
# create new variable for quantiles
hu_dd <- hu_dd %>%
  mutate(total_quantiles = cut(total_breath_tests,
                               breaks = round(quantile(total_breath_tests),0),
                               include.lowest = TRUE, dig.lab=10))

# plot this new variable
ggplot(data = hu_dd) +
  geom_sf(aes(fill = total_quantiles), lwd = 0.5, col = "white") +
  theme_void()
The map still has the white borders between counties, but now those counties have been coloured in four bright colours, red, green, blue, and purple. The legend has changed from a gradient to four colour boxes with labels for their ranges in the form x comma y, and is now labeled ’total quantiles’.
Figure 5.1.5. Map of quantiles with default colour scheme
The colour scheme is terrible, but we will talk about colour in the next section, so we can forgive that for now.

Subsection 5.1.2 Symbols

You might not want to display your map as a choropleth map. You may want to use symbols. Again we explored this in Chapter 3 where you used the tmap package for this. Here is another way you can use the graduated symbol map with ggplot(). You can take the centroid of each county polygon using the st_centroid() function from the sf package, and then when mapping with geom_sf(), within the aes() function specify the size = argument to the variable you wish to visualise:
ggplot(data = hu_dd) +
  geom_sf() +
  geom_sf(data = st_centroid(hu_dd),  #get centroids
          aes(size = total_breath_tests)) +  # variable for size
  theme_void()
The map of Hungary has lost its bright colours, and now has a black border between counties. The counties are all shaded the same shade of grey, and each one has a solid black circle in its centre. The circles vary in sizes, from a small point to much larger. A legend on the right, labeled ’total breath tests’, shows three example circles and corresponding numbers, ten thousand, twenty thousand, and thirty thousand. The largest circles in counties on the map seem to be a similar size to the thirty thousand example circle.
Figure 5.1.6. Graduated symbol map
Like with the thematic map, you can play around with colour and shape:
ggplot(data = hu_dd) +
  geom_sf(fill = "light yellow",   # specify polygon fill colour
          col = "white") +    # specify border colour
  geom_sf(data = st_centroid(hu_dd),
          aes(size = total_breath_tests),
          col = "orange") +  # specify symbol colour
  theme_void()
The previous map now has white borders between counties again, and the counties are shaded a light yellow. The circles on the map and in the legend are orange.
Figure 5.1.7. Graduated symbol map with adjusted colours
You can also change the symbol itself with the shape = parameter. For example, you could use a triangle, like below, or to any other symbol.
ggplot(data = hu_dd) +
  geom_sf(fill = "light yellow",
          col = "white") +
  geom_sf(data = st_centroid(hu_dd),
          aes(size = total_breath_tests),
          col = "orange",
          shape = 17) + # set shape to be a triangle
  theme_void()
The previous map of Hungary, in the same colour scheme, has had its circles replaced with triangles of corresponding sizes.
Figure 5.1.8. Graduated symbol map with triangle shape
Or to any other symbol. The possible values that you can use for the shape argument are the numbers 0 to 25, and the numbers 32 to 127. Only shapes 21 to 25 are filled (and thus are affected by the fill colour), the rest are just drawn in the outline colour. Shapes 32 to 127 correspond to the corresponding ASCII characters. For example, if we wanted to use the exclamation mark, the corresponding value is 33. How you choose to represent your data will depend on your decisions to the questions asked above about audience, message, integrity, and so on.

Subsection 5.1.3 Rate vs. count

In Chapter 3 we have discussed this already in great detail, so do consult this, but it is important that your data are meaningful and easy to interpret. We might, in this case for example, want to consider the rate of positive breath tests per test carried out in each county. To compute this, we might want to consider the proportion of positive results on the breathalyser tests (where the person had been drinking and their result is over the limit). To compute this, we can simply divide the positive results by the total test, and multiply by 100. We also include the round() function in there, as we don’t need much precision in this case.
hu_dd <- hu_dd %>%
  mutate(pos_rate = round(positive_breath_tests/total_breath_tests*100,1))
We can see the county with the highers proportion of test yielding drink drivers is Pest megye with 3 %, while the county with the lowest is Hajdú-Bihar with 0.2 %. We can visualise this rate on our thematic map in exactly the same way as the count data, but using our new variable in the fill = argument:
ggplot(data = hu_dd) +
  geom_sf(aes(fill = pos_rate), lwd = 0.5, col = "white") +
  theme_void()
The map of Hungary, shaded in blues similar to before. A legend to the right, labeled ’pos rate’, matches lighter shades of blue to higher numbers. A vertical strip along the centre of the country is much lighter than the east or west.
Figure 5.1.9. Map with rate instead of count