I am not a expert user in stata but working on a coordinates dataset for district boundaries in Pakistan and trying to create x and y cartesian coordinates for point/labels in those districts. Purpose of this exercise to develop choropleth maps of districts and labels on districts. I can create choropleth maps but unable to label districts because my dataset dont have x and y coordinates for district centers.
use "uscoord.dta", clear
geo2xy _Y _X, projection( mercator) replace
local lon0 `r(lon0)'
local f `r(f)'
local a `r(a)'
save "uscoord_mercator.dta", replace
use usdb,clear
gen x = runiform()
save "usdb2",replace
gen labtype = 1
append using usdb2
replace labtype = 2 if labtype==.
replace ADM2_EN = string(_ID, "%3.0f") if labtype ==2
gen x_c=runiform()
gen y_c=runiform()
keep x_c y_c ADM2_EN labtype
geo2xy y_c x_c, projection( mercator, `a' `f' `lon0' ) replace
save maplabels, replace
clear
import excel "corana_cases.xlsx", sheet("District") firstrow
merge 1:1 _ID using usdb2
drop if _merge!=3
spmap cases using uscoord, id(_ID) fcolor(BuRd) clmethod(custom) clbreaks(1 10 20 30 40 500) ndlabel("No Cases") legend(position(5)) title("District Wise Confirmed COVID-19 Cases") subtitle("26 March 2020") ///
label(data(maplabels) xcoord(x_c) ycoord(y_c) ///
label(ADM2_EN) by(labtype) size(*0.85 ..) pos(12 0) )
But when i run this code, x and y coordinates are generated in negative values and map is not created.
I will be thankful for support.
use "uscoord.dta", clear
geo2xy _Y _X, projection( mercator) replace
local lon0 `r(lon0)'
local f `r(f)'
local a `r(a)'
save "uscoord_mercator.dta", replace
use usdb,clear
gen x = runiform()
save "usdb2",replace
gen labtype = 1
append using usdb2
replace labtype = 2 if labtype==.
replace ADM2_EN = string(_ID, "%3.0f") if labtype ==2
gen x_c=runiform()
gen y_c=runiform()
keep x_c y_c ADM2_EN labtype
geo2xy y_c x_c, projection( mercator, `a' `f' `lon0' ) replace
save maplabels, replace
clear
import excel "corana_cases.xlsx", sheet("District") firstrow
merge 1:1 _ID using usdb2
drop if _merge!=3
spmap cases using uscoord, id(_ID) fcolor(BuRd) clmethod(custom) clbreaks(1 10 20 30 40 500) ndlabel("No Cases") legend(position(5)) title("District Wise Confirmed COVID-19 Cases") subtitle("26 March 2020") ///
label(data(maplabels) xcoord(x_c) ycoord(y_c) ///
label(ADM2_EN) by(labtype) size(*0.85 ..) pos(12 0) )
But when i run this code, x and y coordinates are generated in negative values and map is not created.
I will be thankful for support.
Comment