Hi everyone,
I am trying to draw a US map with all the counties using the package spmap. The problem is that when Stata plots the map and I include Alaska and Hawaii, the map appears distorted. The data used can be downloaded from here: https://www.census.gov/geo/maps-data..._counties.html.
The code I am using is the following one:
With this code, Alaska and Hawaii distort the map. I have been trying to modify the uscoord.dta file in the way it was shown in previous posts (for a state level map), but I don't know if I am doing it well or if I should do it in another way. The error Stata gave me is:
. The following part of code would go after the command
The IDs are the ones that correspond to the counties in Hawaii and Alaska according to the usdb.dta file.
I am trying to draw a US map with all the counties using the package spmap. The problem is that when Stata plots the map and I include Alaska and Hawaii, the map appears distorted. The data used can be downloaded from here: https://www.census.gov/geo/maps-data..._counties.html.
The code I am using is the following one:
HTML Code:
cd "../cb_2016_us_county_500k"
ssc install spmap
ssc install shp2dta
shp2dta using cb_2016_us_county_500k, database(usdb) coordinates(uscoord) genid(id)
****
use "usdb.dta", clear
destring STATEFP, gen(STATEFP_new)
drop STATEFP
rename STATEFP_new STATEFP
rename STATEFP state_fips
rename NAME county_str
replace county_str = subinstr(county_str, "Baltimore", "Baltimore City", .) if LSAD=="25"
replace county_str = subinstr(county_str, "St. Louis", "St. Louis City", .) if LSAD=="25"
replace county_str = subinstr(county_str, "Fairfax", "Fairfax City", .) if LSAD=="25"
replace county_str = subinstr(county_str, "Franklin", "Franklin City", .) if LSAD=="25"
replace county_str = subinstr(county_str, "Richmond", "Richmond City", .) if LSAD=="25"
replace county_str = subinstr(county_str, "Roanoke", "Roanoke City", .) if LSAD=="25"
replace county_str = subinstr(county_str, "Doña Ana", "Dona Ana", .)
// Rename the variables to merge
** Merge datasets
merge 1:1 state_fips county_str using "../Crosswalk_county_raw_temp.dta"
// Merge with the dataset that has the data we want to plot.
drop if _merge!=3
drop _merge
drop if state_fips==72 // Dropping Puerto Rico
** Draw the graph
spmap color_num using "uscoord.dta", id(id) ///
fcolor(RdYlBu) ndfcolor(white) osize(thin) clmethod(custom) clbreaks(0 1 2 3 4 5) ///
legtitle(Belt Definitions) legorder(lohi) legend(label(2 "Rust Belt") label(3 "Sun Belt") ///
label(4 "East Coast") label(5 "West Coast") label(6 "Flyover counties") position(2)) ///
graphregion(fcolor(white) lcolor(white) ilcolor(white)) ///
title({bf:Potential Belt Definitions})
With this code, Alaska and Hawaii distort the map. I have been trying to modify the uscoord.dta file in the way it was shown in previous posts (for a state level map), but I don't know if I am doing it well or if I should do it in another way. The error Stata gave me is:
HTML Code:
latitude _Y must be between -90 and 90
HTML Code:
shp2dta using cb_2016_us_county_500k, database(usdb) coordinates(uscoord) genid(id)
HTML Code:
ssc install geo2xy
net get geo2xy
use "../cb_2016_us_county_500k/uscoord.dta", clear
* Alaska
drop if _X > 0 & !mi(_X) & !inlist(_ID, 28,29,30,463,464,465,466,467,468,774,900,953,1054,1076,1158,1278,1369,1370,1371,2135,2276,2277,2278,2279,2280,2281,2527,2556,3073) // land in the Eastern hemisphere
geo2xy _Y _X if !inlist(_ID, 28,29,30,463,464,465,466,467,468,774,900,953,1054,1076,1158,1278,1369,1370,1371,2135,2276,2277,2278,2279,2280,2281,2527,2556,3073), replace proj(albers)
replace _Y = _Y / 3 + 700000 if !inlist(_ID, 28,29,30,463,464,465,466,467,468,774,900,953,1054,1076,1158,1278,1369,1370,1371,2135,2276,2277,2278,2279,2280,2281,2527,2556,3073)
replace _X = _X / 3 - 1700000 if !inlist(_ID, 28,29,30,463,464,465,466,467,468,774,900,953,1054,1076,1158,1278,1369,1370,1371,2135,2276,2277,2278,2279,2280,2281,2527,2556,3073)
* Hawaii
drop if _X < -160 & !mi(_X) & !inlist(_ID,98,359,360,361,2389) // small islands to the west
geo2xy _Y _X if !inlist(_ID,98,359,360,361,2389), replace proj(albers)
replace _Y = _Y / 2 + 1500000 if !inlist(_ID,98,359,360,361,2389)
replace _X = _X / 2 - 800000 if !inlist(_ID,98,359,360,361,2389)
* contiguous US
geo2xy _Y _X if !inlist(_ID,98,359,360,361,2389,28,29,30,463,464,465,466,467,468,774,900,953,1054,1076,1158,1278,1369,1370,1371,2135,2276,2277,2278,2279,2280,2281,2527,2556,3073), replace proj(albers)

Comment