Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Adding labels and using reverse color gradient for choropleth map

    I am working on mapping an outcome variable (% of mothers receiving postnatal care) at the county level in Kenya using srmap and shp2dta both from SSC in Stata 17 on a Mac. I was able to create the map but am struggling with two issues: (1) getting labels with the county name onto the map and (2) making the counties with *low* postnatal care coverage darker. It looks like all the sequential color schemes in Stata run darker with higher values, which usually works but I want to highlight the low coverage areas. I can’t use a divergent scheme because I need all counties to have a color. Looks like a reverse option is not allowed, any ideas on this? I want to present % who received pnc, not % who didn’t so I’m not able to flip the variable as a workaround.

    The more important issue though is getting the county names onto the map. Looks like maybe I need to create another dataset with just the county names and x,y coordinates? Getting confused though, would appreciate any help.

    Below is the code that I have so far. Note: I’m sorry for the inconvenience, but for the below code to run, the shapefiles would have to be downloaded from the internet to your computer and filepath below updated. The shapefiles are in a zipfile (ken_adm_iebc_20191031_SHP.zip) (second link down) at this website: https://data.humdata.org/dataset/cod-ab-ken. I tried to write code to read these shapefiles directly into STATA and unzip for a reproducible example, but I’m not sure it’s possible since there is only a download option and not a unique URL...

    Below is the code I have so far. Thank you very much in advance for any help!

    Code:
    *Generate example dataset
    clear
    set obs 20
    gen id = _n
    set seed 1234
    gen byte pnc = (runiform() < 0.75)
    gen county = ""
    replace county = "Kilifi" in 1/2
    replace county = "Meru" in 3/5
    replace county = "Nyeri" in 5/10
    replace county = "Kajiado" in 11/15
    replace county = "Siaya" in 16/20
    lab define pnc 0 "No" 1 "Yes"
    lab values pnc pnc
     
    *Create dataset with county-level average PNC
    preserve
    sort county
    collapse (mean) pnc, by(county)
    rename county ADM1_EN
    gen pnc_county = round(pnc * 100, 0.01)
     
    save pnc_county_example_formap, replace
    restore
     
    *Create county-level map of pnc
    shp2dta using "$controls/Maps/HUM_Kenya_adm_20Feb2020/ken_admbnda_adm1_iebc_20191031", ///
    database(kenyadb) coordinates(kenyacoord) genid(id) replace
     
    use kenyadb, clear
    merge m:1 ADM1_EN using pnc_county_example_formap
     
    spmap pnc_county using kenyacoord, id(id) fcolor(Reds2) ///
    clnumber(3) ocolor(Black) osize(medium)  ///
    title ("Percent receiving postnatal care by study county", size(small)) ///
    legtitle("% receiving maternal PNC ")  ///  
    legorder(hilo) ///        
    legstyle(2) legend(region(lcolor(dknavy)))    ///
    plotregion(icolor(gs15)) graphregion(icolor(gs15))
    Last edited by Valerie Scott; 01 Apr 2023, 20:02.

  • #2
    I cannot completely follow your first question, but perhaps the following helps. If highlighting a county, you may not necessarily be interested in the wards within the county - so I eliminate these borders.

    Code:
    copy https://data.humdata.org/dataset/2c0b7571-4bef-4347-9b81-b2174c13f9ef/resource/03df9cbb-0b4f-4f22-9eb7-3cbd0157fd3d/download/ken_adm_iebc_20191031_shp.zip ken_adm_iebc_20191031_SHP.zip, replace
    unzipfile ken_adm_iebc_20191031_SHP.zip, replace
    
    *Generate example dataset
    clear
    set obs 20
    gen id = _n
    set seed 1234
    gen byte pnc = (runiform() < 0.75)
    gen county = ""
    replace county = "Kilifi" in 1/2
    replace county = "Meru" in 3/5
    replace county = "Nyeri" in 5/10
    replace county = "Kajiado" in 11/15
    replace county = "Siaya" in 16/20
    lab define pnc 0 "No" 1 "Yes"
    lab values pnc pnc
     
    *Create dataset with county-level average PNC
    preserve
    sort county
    collapse (mean) pnc, by(county)
    rename county ADM1_EN
    gen pnc_county = round(pnc * 100, 0.01)
    
    tempfile  pnc_county_example_formap 
    save `pnc_county_example_formap', replace
    restore
     
    *Create county-level map of pnc
    shp2dta using "C:/Users/709554/Documents/ken_admbnda_adm2_iebc_20191031.shp", ///
    database(kenyadb) coordinates(kenyacoord) genid(id) replace
     
    use kenyadb, clear
    merge m:1 ADM1_EN using `pnc_county_example_formap'
    
    preserve
    keep if _merge==3
    keep id ADM1_EN
    save countynames, replace
    use kenyacoord, clear
    rename (_ID _X _Y) (id xcoord ycoord)
    merge m:1 id using countynames, nogen
    *SEARCH IDEAL COORDINATES TO PLACE LABELS
    bys ADM1_EN (xcoord ycoord): gen tag=_n==int(_N/2.5) & inlist(ADM1_EN, "Meru", "Kilifi")
    by ADM1_EN: replace tag= _n==int(_N/4) & inlist(ADM1_EN, "Siaya") if !tag
    by ADM1_EN: replace tag= _n==int(_N/3) &inlist(ADM1_EN, "Nyeri") if !tag
    by ADM1_EN: replace tag= _n==int(_N/1.7) &inlist(ADM1_EN, "Kajiado") if !tag
    keep if tag
    save countynames, replace
    restore
    
    spmap pnc_county using kenyacoord, id(id) fcolor(Reds2) ///
    ocolor(none ..) clnumber(5) osize(medium)  ///
    title ("Percent receiving postnatal care by study county", size(small)) ///
    legtitle("% receiving maternal PNC ")  ///  
    legorder(hilo) ///        
    legstyle(2) legend(region(lcolor(dknavy)))    ///
    plotregion(icolor(gs15)) graphregion(icolor(gs15)) ndocolor(black*0.5) ///
    label(data(countynames.dta) x(xcoord) y(ycoord) label(ADM1_EN) color(black)) 
    Res.:

    Click image for larger version

Name:	Graph.png
Views:	1
Size:	162.7 KB
ID:	1708150

    Comment


    • #3
      Coming back to #1, if you are asking about varying the color intensity in a descending manner, you can be explicit.


      Code:
      spmap pnc_county using kenyacoord, id(id) fcolor(red red*.8 red*.6 red*.4 red*.2) ///
      ocolor(none ..) clnumber(5) osize(medium)  ///
      title ("Percent receiving postnatal care by study county", size(small)) ///
      legtitle("% receiving maternal PNC ")  ///  
      legorder(hilo) ///        
      legstyle(2) legend(region(lcolor(dknavy)))    ///
      plotregion(icolor(gs15)) graphregion(icolor(gs15)) ndocolor(black*0.5) ///
      label(data(countynames.dta) x(xcoord) y(ycoord) label(ADM1_EN) color(black))
      Res.:


      Click image for larger version

Name:	Graph.png
Views:	1
Size:	162.5 KB
ID:	1708161

      Comment


      • #4
        Thank you so much Andrew Musau, this is really helpful. I'm sorry to reply after such a delay, but looking at this again now and I'm a little confused on this part of the code on searching ideal coordinates for the label placement: tag=_n==int(_N/2.5)....._n==int(_N/4)....._n==int(_N/3)...._n==int(_N/1.7). I understand this creates a variable tag but would you mind clarifying the int(_N/2.5) and rationale for those different denominators?

        Comment


        • #5
          Each region is associated with a number of coordinate pairs in the shape file, where a pair represents a point on the map. So, the method is a trial-and-error approach (I try several values before settling on those that you see). It aims to choose an ideal point in a region to center the label. Better placement in #2 https://www.statalist.org/forums/for...d-text-options

          Comment

          Working...
          X