Stata Tutorial: Say It with a Map

Tutorial Time: 5-10 minutes (including download shapefile data and install two Stata modules).

Yesterday I posted my travel maps via Twitter, Facebook and also Path. I just noticed that STATA re tweeted it. Some of my friends via Facebook and STATA users asked me the do-files. So I post this article to make it easy for everyone to create your own travel map.

Preparation:

  • Stata Software, I used Stata 13.0 MP
  • Install spmap: Stata module for for drawing thematic maps by Maurizio Pisati.
  • Install shp2dta: Stata module for converting shapefiles to Stata format, by Kevin Crow.
  • Shapefile: A shapefile is a data format for geographic information systems. Please download this public domain shapefile from Natural Earth: ne_110m_admin_0_countries.zip (184 KB, world map with country borders, scale 1:110,000,000) version 2.0.0
    If you want more countries data

To install spmap and shp2dta, type in your stata command:

ssc install spmap
ssc install shp2dta

Step 1: Convert shapefile to Stata format

  • Unzip ne_110m_admin_0_countries.zip to a folder that is visible to Stata. The archive contains six files:
    ne_110m_admin_0_countries.dbf
    ne_110m_admin_0_countries.prj
    ne_110m_admin_0_countries.shp –> We need this file
    ne_110m_admin_0_countries.shx
    ne_110m_admin_0_countries.README.html
    ne_110m_admin_0_countries.VERSION.txt
  • Start Stata and run this command:
    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 country boundaries).

  • 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 (in my case I don’t use label):
    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.
    The DBF, PRJ, SHP, and SHX files are no longer needed and can be deleted.

Step 2: Create your Travel Data

  • Open worlddata.dta in Stata
    use worlddata.dta, clear
  • Generate a new variable (visited) consist of my visited countries
    You can browse your dataset first before generated your visited countries.

    bro id name

    In total I have visited 15 countries, in this case Singapore is not listed in my visited map because I use shapefile in small scale data, 1:110m (only 177 countries). You can download shapefile data in a medium scale map 1:50m or large scale map 1:10m (up to 247 countries) from Natural Earth website.

    /* name=id */
    /* UAE=4 GERMANY=42 DENMARK=44 INDONESIA=73 INDIA=74 JAPAN=83 REP.KOREA=88 MALAYSIA=112 NETHERLANDS==118 POLAND=128 SENEGAL=142 SINGAPORE=? SWEDEN=152 TURKEY=163 USA=169 */ 
    local visit 4 42 44 73 74 83 88 112 118 128 142 152 163 169
    gen visited=0
    foreach y of local visit{
        replace visited = 1 if id==`y'
    }

Step 3: Create your Map

  • Draw a map that indicates the length of all country names with this command:
    spmap visited using worldcoor.dta, id(id)

    Here’s the result:
    Figure 1

  • Change the color and remove Antartica from the map.
    Note: to change fcolor type help colorstyle in Stata command

    spmap visited using worldcoor.dta if name!="Antarctica", id(id) fcolor(Greens)

    Here’s the map:
    Figure 2

Step 4: Finalise your Map

  • To add the title, note, legend
    spmap visited using worldcoor.dta if name!="Antarctica", id(id) fcolor(Greens) ///
    title("SAY IT WITH A MAP", size(*2) color(green)) ///
    legend(label(2 "Not Yet")) legend(label(3 "Visited")) ///
    note(" ""Generated using Stata Software. Data per April 2014. www.yangkiimadesuara.com", size(*0.75)) legorder(lohi)

    Figure 3

  • If you want to change the color and also update your title?
    Right click on the Map and click Start Graph Editor. Choose which part of the map you want to edit, double click on it, edit it (color, border, etc.) and here’s the final version of my travel map. Visited countries in red, not yet visited in gold color.
    Figure 4

Here’s my complete travel map do-file.

/* YANGKI IMADE SUARA: TRAVEL MAPS version APRIL 2014 */
/* website: www.yangkiimadesuara.com */
/* email: yangki.suara@fe.unpad.ac.id */

/* STEP 1: CONVERT SHAPEFILE TO STATA FORMAT */
shp2dta using ne_110m_admin_0_countries, data(worlddata) coor(worldcoor) genid(id)

/* STEP 2: CREATE YOUR TRAVEL DATA */
use worlddata, clear
/* UAE=4 GERMANY=42 DENMARK=44 INDONESIA=73 INDIA=74 JAPAN=83 REP.KOREA=88 MALAYSIA=112 */
/* NETHERLANDS==118 POLAND=128 SENEGAL=142 SINGAPORE= SWEDEN=152 TURKEY=163 USA=169 */
local visit 4 42 44 73 74 83 88 112 118 128 142 152 163 169
gen visited=0
foreach y of local visit{
 replace visited = 1 if id==`y'
 }

/* STEP 3: DRAW YOUR MAP */
spmap visited using worldcoor.dta if name!="Antarctica", id(id) fcolor(Greens) ///
title("SAY IT WITH A MAP", size(*2) color(green)) ///
legend(label(2 "Not Yet")) legend(label(3 "Visited")) ///
note(" ""Generated using Stata Software. Data per April 2014. www.yangkiimadesuara.com", size(*0.75)) legorder(lohi)

If you have create yours, please share the link to your travel map it in the comment box and or tweet me in  @yangkiimade

References:

Updated. 2 May 2014

Published by Yangki

Founder Made by Suara https://madebysuara.com

3 thoughts on “Stata Tutorial: Say It with a Map

  1. Kang, kenapa pas ngetik use ne_110m_admin_0_countries pertama kali STATA bilang not found? Dan pas ditambahin .shp, ada tertulis “not STATA format”?

    1. Command “use” itu kan untuk ngebuka file dalam format “.dta”
      Jadi pasti gak akan bisa pakai comman use untuk ngebuka file .shp

      Seharusnya commandnya “shp2dta using ne_110m_admin_0_countries, data(worlddata) coor(worldcoor) genid(id)”

      Jadi data koordinat (dalam format .shp) di convert terlebih dahulu ke .dta sehingga bisa dibuka di Stata.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: