While there are no native functions or plugins for QGIS that will automate the creation of a non-contiguous cartogram, here is one proposed workaround for creating such a map using data defined labels.
The following QGIS tutorial is adapted from a non-contiguous map experiment created by Kenneth Field using ArcGIS. This article presents a rough idea of how one might mimic a non-contiguous cartogram using QGIS. For those that want to follow along with this tutorial, a .zip file containing the state polygon, car theft point file, and QGIS Layer Style File is can be downloaded here: car-files.
A non-contiguous cartogram shows geographic features distinct from neighboring features. In this example, the map shows each state separated out from neighboring states. Each state is then resized based on one attribute and color coded based on another attribute. The data used for the map is “Hot Wheels 2015“. The annual report from the National Insurance Crime Bureau lists the top make and model stolen within each state for 2015. To make the map, the states were either enlarged or shrunken based on the number of car thefts for the number one car stolen in that state. The states were then color coded based on the make of the number one car. The end map:
This map process involves using label points in order to create the illusion of a non-contiguous cartogram. The first step is to set the base map which is a polygon layer of the individual states. While it’s not necessary to include a base layer, I chose to do so in order to provide a spatial comparison of the state features. The state layer I used was a shapefile downloaded from the US Census Bureau.
Creating a Point Layer
From the state layer, we want to extract the centroid for each polygon. To do so, go to Vector –> Geometry Tools –> Polygon Centroids.
From the GUI, make sure that your state layer is selected as the polygon and then browse to the folder where you want to save the newly created point layer and give it a name. The new point layer will automatically be added to QGIS as long as the “add result to canvas” box is checked.
Joining Attributes in QGIS
Now that you have a point file containing the center of each state, you will need to join the car theft information to it. The car theft data is stored in CSV format. To join this file to the newly created point file you will need to install the MMQGIS plugin if you don’t already have it. To install go to Plugins –> Manage and Install Plugins…
In the GUI, search for MMQGIS, click on the plugin and select install plugin. Now you can join the car theft file. To do so, go to MMQGIS –> Combine –> Attributes Join from CSV file.
In the attributes GUI you will need to first browse and select the CSV file. The select the field in the CSV file that has the state name. Next select the point layer and select the field name that contains the matching state names. Finally, type in a location and name for the joined shapefile and the location and name of the file containing any unmatched data.
Hit OK and the new shapefile with the joined attributes will be added to the canvas in QGIS.
Now it’s time to stylize the map to mimic a non-contiguous cartogram.
To replicate the state shapes as a label, the first task is to download StateFace. StateFace is a set of icons of the fifty states, the District of Columbia, and one showing the continental United States. Created by nonprofit news source, ProPublica, the source shapefiles used to create StateFace were adapted from Natural Earth data. The font set can be downloaded from ProRepublica’s github page. As you can see from the keyboard map below for Stateface, each state shape is linked to a lower or upper case letter:
Once you have downloaded and installed the StateFace font on your computer, you can now create a field that links the state shape to the keystroke. The easiest way is to create a CSV file containing two columns, the first being the state name and the second with the corresponding keystroke. You can then join this to your car point file. Click on the “+” to see the table.
|District of Columbia||y|
Label the Point File with State Shapes
Now you’re ready to label your point file with the state shapes. To do so, right click on your point layer and select properties.
In the properties window, select style. Turn off the styling for the point shapefile by selecting “No symbols” and hit apply. Keep the property window open for now but you should notice the points disappear from the canvas.
Now we want to select StateFace to be label used for this layer. Right below Style select the Labels menu option from the properties window. First select “Show labels for this layer” (1). Then select the field that contains the keystroke information for matching the states to the corresponding StateFace font (2). Finally, select the font as StateFace (3) and hit OK.
Your labeling should now look like this with each state now labeled with the same sized font:
Change Labeling with Data Defined Expressions
Now its time to stylize the labels using data defined expressions. This will let us stylize the labeling two different ways based on attribute data. First we want to enlarge or shrink the size of the state features based on how many cars of the top make/model had been stolen in each state. To make things simple, I had created a field named “Interval” that categorized the values into:
- Under 300 cars stolen = Small
- 400 – 999 cars stolen = Medium
- 1,000 – 4,000 cars stolen = Large
- More than 7,000 cars stolen = Extra Large
Using the values in this field, I can create data defined conditions for how large the StateFace font should be. To do so, go back to the layer properties –> labels and select the icon next to the font size box. Then select “Edit..” under Expression.
In the Expression Editor, set up the definitions for how the font should be sized. This case statement pulls data from the field Interval and sets the font size based on the value within that field:
CASE WHEN "Interval" = 'Small' THEN 10 WHEN "Interval" = 'Medium' THEN 20 WHEN "Interval" = 'Large' THEN 40 WHEN "Interval" = 'Extra Large' THEN 120 END
Then hit OK.
You should now see your map with varying sizes for each state shape. You can adjust font sizes by changing the numerical value after each THEN to increase or decrease.
Now let’s change the font colors based on the number one type of make stolen from each state. Head back to the label section in the layer properties. Select the icon next to the font color box and then edit to pull up the Expression dialog box. Again, use a CASE statement to read attribute values from a the Make field to change the font color. The font values listed here are RGB.
CASE WHEN "MAKE" = 'Ford' THEN color_rgb(255, 0, 0) WHEN "MAKE" = 'Chevrolet' THEN color_rgb(253, 252, 115) WHEN "MAKE" = 'Honda' THEN color_rgb(0, 0, 255) WHEN "MAKE" = 'Dodge' THEN color_rgb(0, 255, 0) END
Hit ok and now the layer is color coded to you can see patterns of which car make is the most stolen in each state. Notice any geographic patterns?
Finally, add any map elements to your map such as labels, title, etc. As my legend, I created a pie chart in a separate application and then added that as an image in Composer. A lot of manual tweaking can be incorporated to move any overlapping features if desired.