Making Choropleth Maps in Stata
A Choropleth map uses different colours or shading for different areas of a map to indicate average values for each area. For example – a map of Australia with each state as a separate area, each area coloured to represent the proportion of the population of Australia who live there. This example is shown below.
These maps are easy to make in Stata provided you have the GIS mapping shapefiles for the country or region you want to map. And of course you will need the data you want to make your choropleth map with. In the case of the map of Australia above, the population data is easily obtainable through Wikipedia, and the map files can be accessed through the Australian Bureau of Statistics.
Once you have your GIS shapefiles and your choropleth data, you can easily import the map files with the command spshape2dta and either frame-link or merge the choropleth data to create one spatial dataset. With this dataset set up, you use the grmap command to generate your map. Below I will go through the steps required to make the population map above.
Step 1: Find the GIS mapping shapefiles
For Australia, GIS shapefiles can be obtained through the ABS website. Here is the link where they can be found - https://www.abs.gov.au/websitedbs/D3310114.nsf/home/Digital+Boundaries
There are many different types of boundaries saved here, however we are only interested in the state and territory boundaries for our map. To get these, click on the “Volume 1 – Main Structure and Greater Capital City Statistical Areas” link.
This takes you to a new webpage with some green/grey tabs across the top. The first one is “Summary” which is the default tab you load in on. Next to that is a tab called “Downloads”. Click on this tab.
You should see a long list of Data Cubes, which are zip files that contain different map files. We are looking for the state boundaries in ESRI shapefile format.
It will be listed as “State and Territory (STE) ASGS Ed 2016 Digital Boundaries in ESRI Shapefile Format” and there will be a ZIP-file link on the right. Click on the zip file to download.
You will need a zip-file extraction program to open this zip file. If you do not have one already, there is a free program called 7ZIP for Windows which can be used to open zip files. For Mac you have a utility app called Archive Utility which can open and extract zip files. For a Mac, if you simply double-click on the zip file the Archive Utility will automatically extract all the files and place them in the same folder as the zip file.
The zip file you downloaded from ABS should be called “1270055001_ste_2016_aust_shape.zip” or something similar. There are a whole bunch of files in the zip, however we are only interested in the database file (.dbf) and shapefile (.shp). Save these files in a location you know. I have saved mine in a folder called AUS, located in a folder called Maps in my Documents folder.
Step 2: Import the GIS mapping shapefiles using spshape2dta
The ESRI shapefile and database file I downloaded are called STE_2016_AUST.dbf and STE_2016_AUST.shp and they are saved in folder AUS located in a folder called Maps in my Documents folder. In order for this to work the database file and shapefile should have exactly the same name, just with different file extensions (one .dbf and one .shp). All ESRI files you download should be formatted like this, but if they are not make sure to rename them correctly. To import and convert these to Stata datasets I use the following command:
This gives the following output in the command pane:
You will see from this that two Stata datasets are created. Make sure these always stay in the same folder together (if you move one you need to move both). The file you want to use is the regular file, in this case “STE_2016_AUST.dta”. DO NOT touch the file with “_shp.dta” on the end. This is what creates the map, any alterations will mean the map cannot be drawn.
Now that the datasets have been converted in Stata, I am going to load the region dataset and draw a blank map of Australia to make sure my spatial data has imported correctly. In the command pane:
Step 3: Merging or frame-linking choropleth data
There are many ways to get the population data from Wikipedia. You can input the data manually; write a Java/C/C++ Stata plugin to scrape the data; use Python in Stata v16 interactively to scrape the data; import using Microsoft Excel; or use an online tool.
For this example I use the online tool https://wikitable2csv.ggor.de/. I input the webpage https://en.wikipedia.org/wiki/States_and_territories_of_Australia, click convert, and download tables 2 and 3 as a csv. To then get this into Stata I use the following commands:
Now I need to merge the data together. There are two ways of doing this, either with merge or by using the new frame link commands in Stata v16. For this example I am just going to merge the two files:
Step 4: Generating a choropleth map
Now that I have my dataset together and set (either through merging or frame-linking), it is as simple as giving the command grmap along with the variable name that I would like to map. It is worth noting here that if this is the first time you have ever used grmap on your current Stata you will need to activate it first with command grmap, activate. I did this for my version earlier in Step 2. Now let’s generate our population map! In the command pane:
Now this map looks pretty good. However, if you are familiar with the population sizes for states and territories in Australia you may have noticed a problem with this map. The Australian Capital Territory (ACT), the small territory located entirely within New South Wales (NSW), has a population close to that of Tasmania (TAS). The ACT should be coloured a light blue, the same as TAS (the island below the rest of the country). But in this map it’s colour is the same as NSW.
Because of the way our map is generated, it has coloured NSW over the top of the ACT. This issue can be fixed, however it requires the creation of a second map using only the GIS map data for the ACT. Fortunately this is relatively easy to do. You will also need to specify your own colours in order for this to work. I am going to use various intensities of the colour navy for this map. In the command pane I type:
Now our map is complete and is correctly coloured. Usually you will not have a territory completely inside another region, so for most maps you draw with this feature it will work without any of the extra steps.