Announcement

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

  • Creating a map with missing data

    I'm using spmap, shp2dta and colorpalette from SSC.

    I'm trying to create a world map using spmap, with each country colored with their respective data (variable total_worker). Below is the code that I'm using to create the map:

    Code:
    colorpalette viridis, n(12) nograph reverse
    local colors `r(p)'
    drop if country=="Antarctica"
    spmap total_worker using world_coord, id(id) clbreaks(0 5001 20001 50001 150001 1000000 50000000) clmethod(custom) ///
    legend(pos(7) size(2.8) region(fcolor(gs15)) label(2 "0 to 5000") label(3 "5001 to 20000")  label(4 "20001 to 50000" )  label(5 "50001 to 150000" ) label(6 "150001 to 1000000" ) label(7 "1000000+" )) ///
    fcolor("`colors'")  ocolor(white ..) osize(0.05 ..) title("2017", size(5))
    Here world_coord is the coordinates that I created using the command:

    Code:
    shp2dta using "world_countries_2020", database(world_db) coordinates(world_coord) genid(id) replace
    However, variable total_worker contains missing variable for some countries. Right now the graph looks like the following:

    Click image for larger version

Name:	worker_2017.jpg
Views:	1
Size:	32.4 KB
ID:	1718617


    I'd want the white "No data" countries to be grey in color, and have the outline color to be white, just like other countries. How can I customize the color of the countries that has missing variable value?

  • #2
    Cross-posted at Stack Overflow: https://stackoverflow.com/questions/...-data-in-stata

    Comment


    • #3
      spmap is from SSC, as you are asked to explain. As always, the best way to get useful advice is to provide a data example as recommended in FAQ Advice #12. I will sympathize with you and provide a reproducible example on your behalf based on https://www.statalist.org/forums/for...choropleth-map. But this one is not difficult, a glance at the documentation would point you directly to the solution.

      ndfcolor(colorstyle) fill color of empty (no data) base map polygons
      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 "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, "Kilifi")
      by ADM1_EN: replace tag=_n==int(_N/2) & inlist(ADM1_EN, "Meru") if !tag
      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) ndfcolor(gray%20 ..) fcolor(Blues) ///
      ocolor(none ..) clnumber(5) osize(medium) ///
      legorder(lohi) legend(pos(1) rows(1) colgap(0) stack  symxsize(11.5)) ///        
      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:	136.9 KB
ID:	1718706

      Last edited by Andrew Musau; 28 Jun 2023, 12:35.

      Comment


      • #4
        Thanks Andrew for your helpful and detailed response! With a slight changes to the code you provided, I was able to customize the map as I want it. I appreciate you taking your time to help!

        Comment

        Working...
        X