The hardware and bandwidth for this mirror is donated by METANET, the Webhosting and Full Service-Cloud Provider.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]metanet.ch.

Using riding_binplot to create tile grid maps

Andrew McCormack

2019-05-08

Description

The riding_binplot() function takes data at the federal riding level as its input and creates a tile grid map to visualize these data with ggplot2.

Overview

Although Canada is a very large country, around 80% of its population is concentrated in urban areas. Because of this, the majority of federal electoral ridings are small and urban while a much smaller number of ridings in sparsely-populated regions account for the majority of Canada’s landmass. Because of this, visualizing statistics at the riding level with standard cloropeth maps is not ideal. The riding_binplot() function of the mapcan package offers an alternative: the tile grid map. Inspired by the statebins package (Rudis 2017), federal riding tile grid maps are a way of visualizing riding-level data when it is not neccessary to accurately represent the ridings geographically.

Using riding_binplot()

Categorical variables

Preparing the data

riding_binplot() requires a data frame that includes a riding-level variable as well as numeric riding code variable. Specify the riding-level variable of interest in the value_col argument. Specify the numeric riding code variable in the riding_col argument. For instance, we may want to create a tile plot for the seat distribution of the 2015 federal election.

Luckily, mapcan comes with built-in federal election data (the federal_election_results data frame) that dates back to 1996 with variables for riding codes/names, provinces, population, voter turnout and the winning party for each riding. Let’s restrict federal_election_results to include only observations (ridings) from the 2015 election. Because we only need (1) a riding characteristic variable and (2) a riding code variable, we can select only these columns from the data.

Note: We remove all irrelevant variables from the data frame to illustrate more clearly how riding_binplot operates. They do not need to be removed in practice.

This new data frame has the two columns that we need: one to specify the riding code (riding_code) and another to specify a riding-level characteristic of interest (party). Let’s look at a sample:

Looking good.

Plotting the data

Use continuous = TRUE for continuous variables and continuous = FALSE for categorical variables. party is a categorical variable, so we will specify continuous = FALSE:

You will notice that the axis text has no substantive significance (it just the coordinates of the tiles). You can remove it, along with the axis ticks and background grid using theme_mapcan function, a ggplot theme that is part of the mapcan package.

You will also notice that the colours provided by default from riding_binplot are not ideal for a tile plot with Canadian political parties. Because riding_binplot simply creates a ggplot object, we can override the riding_binplot scale and add our own:

In this tile grid map, tiles are arranged by province yet, because seats are highly concentrated in urban areas, each tile only roughly corresponds to geographic location of the riding it represents. The arrange = TRUE option provides a better representation of the disribution of variables within provinces when the exact location of the riding is not relevant.

Compared to a standard cloropeth map, the tile grid map gives us a better sense of how the 2015 federal election shaped up:

Continuous variables

Let’s make a riding tile plot with a continuous variable: voter turnout. This variable is included in the federal_election_results dataset, which is part of the mapcan package. Specify continuous = TRUE in riding_binplot so that ggplot uses a continuous fill scale. The default scale is scale_fill_viridis_c() from the viridis package. Though, as demonstrated above, this can be changed easily by overriding the default scale.

Note that the data can be tidied up using dplyr and piped directly into riding_binplot():

Once again, because we care little about the axis grid and axis values, we can use the theme_mapcan() theme:

Like with the election results that are plotted above, we may want to arrange the values of voter turnout within provinces. Fortunately, we can also use the arrange = TRUE argument when the riding-level variable is continuous.

If squares are not your jam, you can also use hexagons with the shape = hexagon argument. Note that the default is shape = square.

Provincial ridings

Currently, only Quebec provincial ridings can be plotted with riding_binplot(). In the near future, riding_binplot() will be able to create bin plots for the other provinces.

To create a provincial riding plot, specify provincial = TRUE (to let riding_binplot know that it is provincial ridings you wish to plot) and province = QC for Quebec.

Reference

Rudis, Bob. 2017. Statebins: Create ’U.s.’ Uniform Square State Cartogram Heatmaps. https://github.com/hrbrmstr/statebins.

These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.