I recently came across the need to display some data with latitude/logitude information on a map (number of respondents to a survey on the Queensland/New South Wales border). Stata has a nice tool to display information about regions on a map called spmap (spmap on Stata FAQ), but I could not find how to do what I wanted.
But I found that with help from shp2dta (used for spmap as well) this becomes quite straight forward.
1. First you have to download the shape data for the region you are interested in. A quick google search will usually take you to some sort of government data that you can download for free. I got the data for Australia from the ABS (abs map shape file).
2. Install shp2dta in Stata, switch to the directory holding the shape files (there are a number of them), and apply shp2dta:
ssc install shp2dta
shp2dta using STE11aAust, data(aust_data) coor(aust_coordinates) genid(id)
3. Bring it together (Note: I want to illustrate where respondents come from; each response has a postcode that I matched with some postcode latitude (lat) and longitude (lon) data).
/*
plot the longitude/latitude data on a map
*/
// append the corresponding coordinates file created with shp2dta to your dataset
// ssc install shp2dta
append using ausmap/aust_coordinates.dta
// setup boundaries for the map you are interested in
local north = -24.5
local south = -34.6
local east = 155
local west = 147
// cut the map to size, note you can't just replace those with missing except islands
replace _X = `west' if _X < `west'
replace _X = . if _X > 155 // get rid of a small off-shore island
replace _Y = `south' if _Y < `south'
replace _Y = `north' if _Y > `north'
// remove outliers from original data
replace lon = . if lon < `west'
replace lat = . if lat < `south'
// basic plot for the map select only NSW (1) and QLD (3) from all the shapes
tw (area _Y _X if _ID==1 | _ID==3, cmissing(n) nodropbase) ///
(scatter lat lon, jitter(1)), name(simple, replace)
graph export simple.png, replace

4. Well, all that is left to do is make this graph prettier…
/*
now lets make it pretty by fixing the size and aspect ratio, draw
weighted circles below the scatter data and complete the frame
*/
// calculate aspect ratio and width and height of the graph
local ar = ( `north' - `south' )/( `east' - `west' )
local dx = ( `east' - `west' )
local dy = ( `north' - `south' )
// put the coordinates for the frame in the dataset
gen n = _n
replace _ID = -99 if n == 1
replace _Y = `north' if n == 1
replace _X = `west' if n == 1
replace _ID = -99 if n == 2
replace _Y = `north' if n == 2
replace _X = `east' if n == 2
replace _ID = -99 if n == 3
replace _Y = `south' if n == 3
replace _X = `east' if n == 3
replace _ID = -99 if n == 4
replace _Y = `south' if n == 4
replace _X = `west' if n == 4
replace _ID = -99 if n == 5
replace _Y = `north' if n == 5
replace _X = `west' if n == 5
// plot the graph with a few beautifications
tw (scatter lat lon [w=numberPerCode] if state == "QLD", mcolor(maroon*0.5)) ///
(scatter lat lon [w=numberPerCode] if state == "NSW", mcolor(blue*0.5)) ///
(area _Y _X if _ID==1 | _ID==3, cmissing(n) nodropbase fcolor(none) lwidth(0.15) lcolor(black)) ///
(area _Y _X if _ID==-99, cmissing(n) nodropbase fcolor(none) lwidth(0.20) lcolor(black)) ///
(scatter lat lon if state == "NSW", mcolor(blue) msize(0.3) jitter(1)) ///
(scatter lat lon if state == "QLD", mcolor(maroon) msize(0.3) jitter(1)) ///
, aspectratio(`ar') legend(off) yscale(off) xscale(off) ylabel(,nogrid) ysize(`dy') xsize(`dx') ///
graphregion(color(white)) bgcolor(white) name(pretty, replace)
graph export respondant_map.png, width(2048) replace
graph export respondant_map.eps, replace
