Announcement

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

  • Spmap command

    Hello, below is my main dataset used for creating a map using the 'spmap' command in STATA.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(ward district)
    25744 71101
    25747 71101
    25774 71101
    25813 71103
    25819 71103
    25822 71103
    25840 71103
    25843 71103
    25846 71103
    25888 71105
    25894 71105
    25900 71105
    25903 71105
    25912 71105
    25915 71105
    25942 71113
    25948 71113
    25951 71113
    25960 71113
    25972 71107
    25987 71107
    25741 71101
    25756 71101
    25777 71109
    25780 71109
    25783 71109
    25786 71109
    25792 71109
    25804 71109
    25807 71109
    25810 71109
    25816 71103
    25825 71103
    25828 71103
    25831 71103
    25834 71103
    25837 71103
    25852 71103
    25855 71103
    25858 71111
    25861 71111
    25864 71111
    25867 71111
    25870 71111
    25873 71111
    25876 71111
    25879 71111
    25885 71111
    25891 71105
    25897 71105
    25927 71105
    25930 71105
    25936 71105
    25939 71105
    25945 71113
    25957 71113
    25963 71107
    25966 71107
    25969 71107
    25975 71107
    25978 71107
    25981 71107
    25984 71107
    25990 71107
    end

    The basemap dataset containing the spatial data is very large and I'm not sure how to attach it here. But I will attach add pictures for visualization.

    I can create the map for the wards in my dataset using this code:
    Code:
    spmap using "$shape/2017 shapefile/VNM_consistent_wards_mainland/Consistent_wards_coord.dta", ///
    id(ward)
    You wont be able to access that basemap dataset but the command generates this output, each polygon corresponds to a ward:
    Click image for larger version

Name:	Screenshot 2025-02-22 at 4.49.58 PM.png
Views:	1
Size:	208.3 KB
ID:	1773280


    Some of these wards belong to certain districts (variable "district") how do I highlight the outer boundaries of wards which make up certain districts with different colors? And for neighboring districts perhaps their shared border outline could be dashed, with each dash alternating the colors of both districts.

    Please let me know if I could be of more assistance in illustrating my problem.

    Thanks,
    Jad







  • #2
    spmap is from SSC, as you are asked to explain (FAQ Advice #12). Create a variable to identify the wards. Since neighboring wards share borders, consider using transparency to make overlapping borders between different groups more apparent. Otherwise, you only need the options -ocolor()- and -opattern()-.

    Code:
    copy http://www2.census.gov/geo/tiger/GENZ2015/shp/cb_2015_us_state_500k.zip cb_2015_us_state_500k.zip
    unzipfile cb_2015_us_state_500k.zip, replace
    shp2dta using  cb_2015_us_state_500k, data("us_data")  coor("us_coordinates") ///
    genid(id) gencentroids(c) replace
    use us_data, clear
    quietly destring STATEFP, generate(fips)
    drop if fips > 56
    save usstates,replace
    
    //Determine IDs for Hawaii and Alaska
    use usstates,clear
    l fips id  if fips == 2 | fips == 15
    
    // Alaska is reduced in size by 60% and then both Alaska and Hawaii are re-positioned under the southwestern states.
    //Drop northwest islands of Hawaii and western part of Aleutian Islands
    use us_coordinates,clear
    
    gen order = _n
    drop if _X < -165 & _X != . &  _ID == 50
    replace _X = _X  + 55  if  _X != .  &  _ID == 50
    replace _Y = _Y  + 4  if _Y != .  &  _ID == 50
    
    replace _X = _X*.4  -55 if  _X !=  . &  _ID == 15
    replace _Y = _Y*.4  + 1 if _Y != .  & _ID == 15
    drop if _X > -10 & _X != . & _ID == 15
    sort order
    sort _ID
    drop order
    
    save us_coordinates2,replace
    
    clear
    input byte _ID float(x_c y_c) str10 state float X
    15 -152.22098 64.217476 "AK"  3.012048
    25  -86.82837 32.789825 "AL"  19.62733
     7  -92.43925  34.89974 "AR"         0
    22  -111.6644 34.293102 "AZ" .10857763
    54 -119.61073 37.246075 "CA"  .4787962
    10  -105.5478  38.99854 "CO"  1.645692
    45 -72.725716  41.62027 "CT"       100
    41  -75.50023  38.99154 "DE"        10
    38  -82.49748 28.628437 "FL"  13.51883
     6  -83.44633  32.64922 "GA" 1.4010508
    50 -156.36305 20.254757 "HI" 11.305242
    20  -93.50006  42.07462 "IA" 1.1914544
    36 -114.65937  44.38908 "ID"         0
    17  -89.19839  40.06503 "IL"  .8257279
    14  -86.27565  39.90814 "IN" 1.2314225
    39  -98.38021   38.4847 "KS" 1.5873016
     5  -85.29046 37.526665 "KY"  5.178571
    33  -91.98564  31.05839 "LA"  .8701472
    35  -71.79853  42.25332 "MA"  2.857143
    48  -76.77244   39.0369 "MD" 1.8120046
    49  -69.22665   45.3714 "ME"  54.38596
    34  -85.43715  44.35067 "MI" 13.861868
    43  -94.30901  46.31633 "MN"         0
    28  -92.47739   38.3676 "MO" .16798656
     9  -89.66521  32.75086 "MS" 35.737705
    19 -109.64512  47.03353 "MT"         0
    47  -100.4693  47.44633 "ND" .24390244
     1  -99.81079  41.52714 "NE"  .2967359
    40   -71.5776  43.68563 "NH"  42.57907
    46 -74.660904  40.18424 "NJ" 1.8258427
     3 -106.10837 34.421368 "NM"  5.263158
    16 -116.65522  39.35623 "NV" .14925373
    55  -75.50312  42.94038 "NY"         0
    24  -82.79011  40.29176 "OH" 2.0053475
    30  -97.50829  35.58348 "OK" 14.251781
    27  -120.5554  43.93665 "OR"         0
     8  -77.79955  40.87383 "PA"  2.656609
    53  -71.55364  41.67609 "RI"         0
    21  -80.89611  33.90795 "SC"  39.30754
     4  -100.2305  44.43616 "SD"         0
    12  -86.34338  35.84298 "TN" 2.1995287
    42  -99.35069 31.484453 "TX" 1.8089565
    11  -111.6782  39.32379 "UT" 1.4285715
    31  -78.81226  37.51502 "VA"  43.19527
    18  -72.66266  44.07519 "VT" 4.0816326
     2 -120.44686  47.38096 "WA"         5
    26   -90.0117  44.63802 "WI"       100
    32  -80.61371  38.64259 "WV"   29.8633
    13 -107.55145  42.99963 "WY"         0
    end
    
    tempfile data
    save `data'
    
    
    use usstates,clear
    rename STUSPS state
    merge 1:m state using `data'
    rename _ID _ID2
    rename id _ID
    
    keep if _merge==3
    drop _merge
    
    bys state: keep if _N==_n
    gen Selected= cond(!X, 0, 1)
    levelsof state if Selected
    spmap Selected using us_coordinates2 , id(_ID) ///
    clb(0 0.01 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 2 3 4 5 10 15 20 25 30 35 40 60 80 100) ///
    clm(custom)  legorder(lohi) fcolor(none ) ocolor(red gray%50 ..) opattern(dash solid ..) ///
    clnumber(5) leg(off)
    Res.:

    Code:
     levelsof state if !X
    `"AR"' `"ID"' `"MN"' `"MT"' `"NY"' `"OR"' `"RI"' `"SD"' `"WY
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	75.0 KB
ID:	1773409

    Comment

    Working...
    X