You are on page 1of 1

International Education Statistics

Analysis by Friedrich Huebler

31 August 2012 About the author


Guide to creating maps with Stata
Name: Friedrich Huebler
Most charts and maps on this site were created with the Stata statistical software package. This guide explains how maps like those with adult and
youth literacy rates in 2010 can be created with Stata. The article supersedes an earlier version from 2005 and introduces updated maps with current Occupation: Analysis of
country borders. For example, South Sudan, which seceded from Sudan in 2011, is shown as a separate country on the new maps. The instructions international education statistics
below are for Stata version 9 or later. Users of Stata 8 are referred to the guide from 2005. The creation of maps is not supported in older versions of
E-Mail: fhuebler@gmail.com
Stata.
Website: huebler.info,
Requirements
huebler.blogspot.com
Stata version 9.2 or later.
spmap: Stata module for drawing thematic maps, by Maurizio Pisati. spmap can be installed in Stata with this command:
ssc install spmap Translate
shp2dta: Stata module for converting shapefiles to Stata format, by Kevin Crow. shp2dta can be installed in Stata with this command:
Select Language ​ ▼
ssc install shp2dta
Shapefile: A shapefile is a data format for geographic information systems. For the maps in Figures 1 and 2, please download this public domain
shapefile from Natural Earth: Search this site
ne_110m_admin_0_countries.zip (184 KB, world map with country borders, scale 1:110,000,000)
Note: The instructions are accurate for Natural Earth maps version 2.0.0, the most recent version of the maps available in April 2014. Search

Step 1: Convert shapefile to Stata format


Blog archive
Unzip ne_110m_admin_0_countries.zip to a folder that is visible to Stata, for example the current working directory of Stata. The archive
contains six files: ► 2017 (2)
ne_110m_admin_0_countries.dbf
ne_110m_admin_0_countries.prj ► 2016 (2)
ne_110m_admin_0_countries.shp
► 2015 (6)
ne_110m_admin_0_countries.shx
ne_110m_admin_0_countries.README.html ► 2014 (3)
ne_110m_admin_0_countries.VERSION.txt
Start Stata and run this command: ► 2013 (5)
shp2dta using ne_110m_admin_0_countries, data(worlddata) coor(worldcoor) genid(id)
Two new files will be created: worlddata.dta (with the country names and other information) and worldcoor.dta (with the coordinates of the ▼ 2012 (10)
country boundaries). ► October (1)
If you plan to superimpose labels on a map, for example country names, run the following command instead, which adds centroid coordinates to
the file worlddata.dta: ► September (1)
shp2dta using ne_110m_admin_0_countries, data(worlddata) coor(worldcoor) genid(id) genc(c)
Please refer to the spmap documentation to learn more about labels. ▼ August (1)
The DBF, PRJ, SHP, and SHX files are no longer needed and can be deleted. Guide to creating maps
with Stata
Step 2: Draw map with Stata

Open worlddata.dta in Stata. ► July (1)


For the example maps, create a variable with the length of each country's name. The Stata command for this is:
generate length = length(admin) ► June (1)
Draw a map that indicates the length of all country names with this command:
► May (1)
spmap length using worldcoor.dta, id(id)
The default map (Figure 1) is grayscale, it shows Antarctica, there are four classes for the length of the country names, the legend is very small, ► April (1)
and the legend values are arranged from high to low.
► March (1)
Figure 1: Length of country names (small scale map, default style)
► February (1)

► January (1)

► 2011 (11)

► 2010 (13)

► 2009 (15)

► 2008 (31)

► 2007 (22)

► 2006 (20)

► 2005 (34)

Click image to enlarge. Creative Commons License

A second map without Antarctica, with a blue palette, five classes, and with a bigger legend with values arranged from low to high (Figure 2) can
Labels
be drawn with this command:
spmap length using worldcoor.dta if admin!="Antarctica", id(id) fcolor(Blues) clnumber(5) legend(symy(*2) symx(*2) size(*2)) legorder(lohi) Africa (62)
Darker colors on the map indicate longer country names, ranging from 4 (for example Cuba and Fiji) to 35 characters (French Southern and
Antarctic Lands). Asia (63)
Please read the Stata help file for spmap to learn about the many additional options for customization of maps. attainment (10)
Figure 2: Length of country names (small scale map, blue palette) blog (8)
Bolivia (4)
Brazil (4)
Burkina Faso (1)
child labor (4)
conflict (1)
DHS (35)
disparity (21)
education (143)
EFA (11)
Europe (4)
Click image to enlarge.
gender (48)
Alternative maps with more detail
India (13)
The shapefile that was used for Figures 1 and 2 was designed for small maps. It contains the borders for 177 countries and territories and does not Indonesia (2)
include smaller geographic units like Hong Kong, Monaco, or St. Vincent and the Grenadines. As an alternative to the small scale map in Figures 1 and
2, Natural Earth offers shapefiles with more detail that were designed for larger maps. Japan (1)

To create the map in Figure 3, download this shapefile from Natural Earth, which has information for 241 countries and territories: Latin America (49)
ne_50m_admin_0_countries.zip (799 KB, world map with country borders, scale 1:50,000,000) Liberia (2)
Unzip ne_50m_admin_0_countries.zip to a folder that is visible to Stata.
Run this Stata command to convert the shapefile to Stata format: literacy (21)
shp2dta using ne_50m_admin_0_countries, data(worlddata2) coor(worldcoor2) genid(id)
map (35)
If you need Stata files with centroids, run this command instead:
shp2dta using ne_50m_admin_0_countries, data(worlddata2) coor(worldcoor2) genid(id) genc(c) MDG (55)
Open worlddata2.dta in Stata.
MICS (25)
Create a variable with the length of each country's name:
generate length = length(admin) Nepal (8)
Draw the map in Figure 3:
Nigeria (9)
spmap length using worldcoor2.dta if admin!="Antarctica", id(id) fcolor(Blues) clnumber(5) legend(symy(*2) symx(*2) size(*2)) legorder(lohi)
The map takes longer to draw than the map in Figures 1 and 2 because it is more detailed and shows more geographic units. The names of the poverty (25)
countries and territories on the map have a length up to 40 characters (South Georgia and South Sandwich Islands).
preschool (8)
Figure 3: Length of country names (medium scale map) primary (70)
publication (33)
quality (4)
SDG (2)
secondary (47)
software (24)
Stata (24)
statistics (136)
survey (56)
tertiary (5)
UNESCO (86)
Click image to enlarge.
UNICEF (30)
To create the map in Figure 4, download this shapefile from Natural Earth, which has information for 255 countries and territories, including
USA (4)
small islands like the Ashmore and Cartier Islands:
ne_10m_admin_0_countries.zip (5.1 MB, world map with country borders, scale 1:10,000,000) world (89)
Unzip ne_10m_admin_0_countries.zip to a folder that is visible to Stata.
Run this Stata command to convert the shapefile to Stata format:
shp2dta using ne_10m_admin_0_countries, data(worlddata3) coor(worldcoor3) genid(id) Links
If you need Stata files with centroids, run this command instead:
shp2dta using ne_10m_admin_0_countries, data(worlddata3) coor(worldcoor3) genid(id) genc(c) UNESCO Institute for Statistics
Open worlddata3.dta in Stata. (UIS)
Create a variable with the length of each country's name:
UIS Data Centre
generate length = length(ADMIN)
Draw the map in Figure 4: UNESCO
spmap length using worldcoor3.dta if ADMIN!="Antarctica", id(id) fcolor(Blues) clnumber(5) legend(symy(*2) symx(*2) size(*2)) legorder(lohi)
UNICEF
The map takes longer to draw than the maps in Figures 1, 2 and 3 because it has the largest amount of detail. The differences between the maps
in Figures 3 and 4 can be seen by clicking on the images to enlarge them. Figure 4 has more islands and more detailed shorelines. The names of World Bank
the countries and territories on the map in Figure 4 have a length up to 40 characters (South Georgia and South Sandwich Islands).
UN Sustainable Development
Figure 4: Length of country names (large scale map) Goals

Click image to enlarge.

Software used in this guide

Stata: statistical software package


spmap: Stata module for drawing thematic maps, by Maurizio Pisati
shp2dta: Stata module for converting shapefiles to Stata format, by Kevin Crow
ne_110m_admin_0_countries.zip: small scale (1:110,000,000) Natural Earth world map with country borders (184 KB)
ne_50m_admin_0_countries.zip: medium scale (1:50,000,000) Natural Earth world map with country borders (799 KB)
ne_10m_admin_0_countries.zip: large scale (1:10,000,000) Natural Earth world map with country borders (5.1 MB)

Related articles

Adult and youth literacy in 2010


Guide to creating maps with Stata (previous version of this guide, from 2005)
Guide to integrating Stata and external text editors

External links

Natural Earth maps (public domain shapefiles)


Stata FAQ: How do I graph data onto a map with spmap?
Shapefile (Wikipedia)
Geographic information system (Wikipedia)
Centroid (Wikipedia)

Friedrich Huebler, 31 August 2012 (edited 18 April 2015), Creative Commons License
Permanent URL: http://huebler.blogspot.com/2012/08/stata-maps.html

Posted by Friedrich Huebler on Friday, August 31, 2012


Labels: map, software, Stata

32 comments:

Ryan Pollard said...


brilliant tutorial on something visually lovely. would love to see more of these!
May 28, 2013 3:51 PM

Ryan Pollard said...


is there an easy way to just display europe, as opposed to specifying each of the countries you don't want displaying?

thanks.
May 28, 2013 3:54 PM

Friedrich Huebler said...


You can use a shapefile that only has European countries. A Google search turns up several free shapefiles for Europe.
June 02, 2013 9:37 PM

Nigma said...
Cheers for the tutorial.

I have the somewhat of the same problem.


When using the world map "10m-admin-0-countries" and entering a couple of countries spmap crops itself on minimal size (as it did, when you
excluded the "Antarctica"). However, countries like "France" make the map grow global, because all the old collonies are included. Is there a way to tell
stata/spmap to focus/crop to a specific area?
June 25, 2013 8:32 AM

Friedrich Huebler said...


It is possible to focus on a specific area with the plotregion(margin()) option. This can be demonstrated with a map from Natural Earth.

First, download the countries dataset from the 1:110m Cultural Vectors page (direct link, 184 KB).

Unzip the downloaded file and convert the map to Stata format with this command: shp2dta using ne_110m_admin_0_countries, data(worlddata)
coor(worldcoor) genid(id)

Open the file worlddata.dta in Stata and generate a variable with the length of country names with this command: generate length = length(admin)

The following command creates a map of the entire world: spmap length using worldcoor.dta, id(id)

This command creates a map of France, Portugal and Spain that also shows the French area in South America: spmap length using worldcoor.dta if
admin=="France" | admin=="Portugal" | admin=="Spain", id(id)

We can now limit this map to Europe by adding the plotregion(margin()) option to the command above: spmap length using worldcoor.dta if
admin=="France" | admin=="Portugal" | admin=="Spain", id(id) plotregion(margin(-240 0 -170 0))

The appropriate margins can be found through trial and error. The command above adds a negative margin on the left side and on the bottom of the
map, which means that the map is cropped on those sides. For additional information see the Stata documentation (help region_options).
August 28, 2013 9:48 PM

Yangki Imade Suara said...


Hi Huebler,

It seems to download the shapefile from Natural Earth should be


http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/110m/cultural/ne_110m_admin_0_countries.zip

Regards,
Yangki
April 28, 2014 2:51 PM

Friedrich Huebler said...


Yangki, thank you for informing me that the links had changed. I updated the article and all links, where necessary. The instructions now apply to the
Natural Earth maps version 2.0.0, the most recent version available in April 2014.
April 28, 2014 10:44 PM

Greg said...
Can anyone recommend any shapefiles that only have European countries. I agree google search identifies several but they are not all as easy to use
as the natural earth file in this example. Links would be greatly appreciated.
August 22, 2014 11:18 AM

Friedrich Huebler said...


Greg, you can use shapefiles from Natural Earth and draw a map that is limited to Europe. See my comment of 28 August 2013 for an example using
Stata.
August 22, 2014 3:31 PM

Anonymous said...
Thanks Friedrich this worked well.

Is there anyway to change the colour of the ocean ?

Greg
September 09, 2014 6:55 AM

Friedrich Huebler said...


Greg, you can change the color of the ocean with the plotregion option. For example, to change the color of the ocean in Figure 1 from the default
white to blue, use this command:

spmap length using worldcoor.dta, id(id) plotregion(fcolor(blue))


September 17, 2014 10:09 PM

Sebastian said...
Is it possible to get rid of the small islands without deleting countries like Malta?

I would like to creat a map from the EU and the small islands are a little irritating.

Thanks in advance!
September 30, 2014 8:35 AM

Friedrich Huebler said...


Sebastian, you may be able to suppress small islands with the if condition in Stata. See the example for Figure 2, which uses if to hide Antarctica from
the map. As an alternative you could use the 1:110m map from Natural Earth (shown in Figures 1 and 2), but that wouldn't include Malta.
September 30, 2014 10:36 PM

Anonymous said...
Thank you for the tutorial.

I would like to generate a map where countries that meet some condition, e.g. having a particular law, are shaded, while countries that do not, remain
colorless.

I have a binary variable (0's and 1's) in my dataset, but when I use it as the "attribute," Stata thinks the 0's are another "class," and shades them light
gray, while shading the 1's as dark gray. How can I leave the 0's unshaded while shading the 1's?

Thank you!
October 27, 2014 4:48 PM

Friedrich Huebler said...


I am afraid this question goes beyond my knowledge of spmap. I would recommend that you ask your question on Statalist.
October 27, 2014 6:05 PM

Nina said...
Hi Friedrich,

Thanks for the tutorial.

I am trying to create a map of the world with different colours for each country.

I typed in this code:


spmap length using worldcoor2.dta if admin=="Malaysia", id(id) fcolor(Blues) clnumber(5) legend(symy(*2) symx(*2) size(*2)) legorder(lohi)

And got an error code:


nquantiles() must be less than or equal to number of observations plus one

Could you please assist? Thanks!


November 04, 2014 2:59 AM

Friedrich Huebler said...


Nina, you wrote that you are trying to create a map of the world but your command draws only a map with one country, Malaysia. If you select more
countries you shouldn't get the error message you reported. Example:

spmap length using worldcoor2.dta if admin=="Malaysia" | admin=="Indonesia" | admin=="Brunei" | admin=="Timor-Leste" | admin=="Papua New
Guinea", id(id) fcolor(Blues) clnumber(5) legend(symy(*2) symx(*2) size(*2)) legorder(lohi)
November 04, 2014 9:13 AM

Nina said...
Thanks a lot!

Would it be possible to use a different colour for a each country?


November 04, 2014 5:02 PM

Friedrich Huebler said...


Nina, you can assign a different color to each country. The attribute (the variable "length" in the example in my previous comment) would have to
contain unique values for each country and in the fcolor() option you would have to specify about 200 different colors, one for each country on the
map. See help spmap for additional information.
November 05, 2014 11:36 AM

Nina said...
Thank you! I appreciate your help.
November 05, 2014 7:23 PM

Unknown said...
Hi Huebler,

Thank you for the tutorial, helps a lot.

I want to change my legend label to appear shorter than the actual data, for example, only presenting it in 2 digits decimal.

What command option would work? thanks!


November 06, 2014 11:30 PM

Friedrich Huebler said...


Please see help legend_options in Stata, especially the section "Content suboptions for use with legend() and plegend()".
November 07, 2014 10:58 AM

Anonymous said...
Hi,
excuse me for my ignorance in this matter, i just recently started to use stata. can you give me a example of a visible folder?
When i use the following command:
"shp2dta using ne_110m_admin_0_countries, data(worlddata) coor(worldcoor) genid(id)"
error r(601) appears...

Kind regards,
John
March 13, 2015 1:53 PM

Anonymous said...
Dear Huebler,

I am struggling to get off the touchline.

I am intending to publish a list of point coordinates in the ocean (which I have as latitude and longitude columns in my dataset).

However, when trying to follow the example's first line,


'shp2dta using... etc. (...) gen(id)' I get the error
file already exists.

What I am doing wrong? I have one single dataset where all the latitude and longitude data is present.
March 27, 2015 7:55 AM

Naomi y Mati said...


Dear Friedrich,
First of all let me thank you for sharing your maps and teaching the community how to do them.
Basically, I want to draw a map fusing two sets of colors as the index I want to represent goes from -1 to 1. So I want a color for the negative
countries, another color for the positive ones. Also, I was wondering if you know how to fix the range each shade of color has. For example, I want one
degree of blue going from 0 to 0.25, another a bit stronger, from 0.26 to 0.50 and so on.
Any help will be strongly appreciated,
Best regards,
David
April 15, 2015 8:20 AM

Friedrich Huebler said...


I modified the article to make clearer what is meant by a "folder that is visible to Stata". An example for such a folder is the current working directory
of Stata, shown in the lower left corner of the Stata window. The Stata command pwd also displays the name of the current working directory.
April 18, 2015 5:58 PM

Friedrich Huebler said...


The command shp2dta using ne_110m_admin_0_countries, data(worlddata) coor(worldcoor) genid(id) leads to a "file already exists" error
message if the files worlddata.dta and/or worldcoor.dta already exist in the same folder. To avoid the error, there are three options: (a) delete the files
worlddata.dta and worldcoor.dta before running the command, (b) use different names in the data() and coor() options, or (c) use the replace
option of the shp2dta command (see help shp2dta).
April 18, 2015 8:10 PM

Friedrich Huebler said...


Regarding the colors, in the examples in my article the color is determined by the variable "id". You can assign values to this variable based on an
another variable, for example replace id = 5 if indicator>=0 & indicator<0.26 (where "indicator" is the variable the colors in the map refer to).
Then, you use the fcolor option of spmap to assign colors in the order of the values in the "id" variable. For example, if "id" has 8 different values
(from 1 to 8) and 5 represents 0 to 0.25, then the fcolor option would list 8 different colors.

I cannot go into more detail here but this is all documented in help spmap. Quote from the spmap help file: "fcolor(colorlist) specifies the list of fill
colors of the base map polygons. When no choropleth map is drawn, the list should include only one element. On the other hand, when a choropleth
map is drawn, the list should be either composed of k elements, or represented by the name of a predefined color scheme."

In the examples in the article I use fcolor with the Blues color scheme but I could also have specified five different colors.
April 18, 2015 8:30 PM

Unknown said...
Hi, please let me know, how I can show country name on the map. My another question is that how I can draw regional map, for instance, South Asia?
I would like to give you thanks in advance.
March 05, 2016 10:36 PM

Unknown said...
Hi, Please let me know how I can show country's name in the place of the country and how I can draw regional diagram, for instance, South Asia, in
the map.

I would like to give you thanks in advance.


March 05, 2016 10:40 PM

Friedrich Huebler said...


Labels are described in detail in the spmap help file. Enter the command help spmap in Stata and then go to the section describing the "Option
label() suboptions". The help file also contains examples that show how labels can be added to a map.

You have two options if you want to draw a regional map, for example for South Asia. You can either use a shapefile for the region, or you can limit the
map to specific countries with the help of if. The second approach is used in the examples in the article to exclude Antarctica from the map (compare
Figure 1 and Figure 2).
March 06, 2016 10:11 PM

Anonymous said...
Thanks a lot!!!!
February 16, 2017 3:17 PM

Post a Comment

Newer Post Home Older Post

Subscribe to: Post Comments (Atom)

You might also like