Announcement

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

  • spmap: Horizontal legend with specified legend text options

    Dear all,
    I'm trying to make a map with a nicer legend. I would like for the legend to look something like this: https://ourworldindata.org/grapher/g...ry=ZAF~FRA~BRA
    I.e. perhaps with more colours than what the map actually entails, and with these kinds of datapoints with numerical information.

    Currently, I have this code:
    Code:
                    spmap ineq_heim using  "\Data\data_NUTS\1000_NUTS2_shp.dta", id(_ID) fcolor(Blues)  title("") osize(vthin) legend(size(*1) position(1) row(1) ring(1)  symx(*3.5) symy(*1.5) rowgap(-2) alignment(top) ) legorder(lohi) ///
                    clmethod(eqint) eirange(${min} ${max}) clnumber(4) name(ineq, replace)
    Which gives me this map:
    Click image for larger version

Name:	mapdiff_ineq_heim2.png
Views:	1
Size:	125.9 KB
ID:	1710546

    I have managed to make the legend horizontal with the "row(1)", but trying to close the gap beween the colour boxes with 'rowgap(#)' seems not to do anything. Additionally I can't figure out how to force the legend information to appear below the colour boxes.

    That is kind of the ideal solution I can think of to get as close as possible to the example that I link to - but I am very open to better looking solutions. It would be really cool if it could be a gradient bar, or something more gradient-adjacent.
    Last edited by Nanna Lauritz; 20 Apr 2023, 05:04.

  • #2
    See

    Code:
    help legend_options
    The -stack- option will vertically stack the text. Then it is a matter of having the key symbols wide enough so that the text can fit underneath. The option -symxsize- controls this. Finally, the option -colgap- controls the gap between columns. On specifying different colors, there are preprogrammed color palettes detailed in -help spmat-, but if you have your own set of colors, see the linked thread from the ensuing example on how you may explicitly include these. To illustrate the former, I use the reproducible example from https://www.statalist.org/forums/for...choropleth-map.

    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, "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) 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))
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	108.1 KB
ID:	1710550




    Comment


    • #3
      That is SO much better! Thank you!

      Comment

      Working...
      X