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

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

theme_void() to ggplot mapWe 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()

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()

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 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()

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()

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()

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()

