Announcement

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

  • Problem with spmap - Border Lines Disappearing when Variable to be Plotted is 0

    Hi all,

    My data are at the end.

    When I run the following code, it produces a map where there are no visible border lines between contiguous states with a value of X of 0. Please could someone help me produce a map with all state borders visible?

    Also, is there a colour palette for which states with a value of X of 0 could be just white, instead of very lightly red shaded?

    Thanks in advance!

    Code:
    shp2dta using "$data2/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
    
    use us_coordinates2, clear
    bys _X _Y: keep if _N==_n 
    save "$data2/us_coordinates2", replace
    
    use usstates,clear
    spmap using us_coordinates2  , id(id)
    
    rename STUSPS state
    merge 1:m state using "$data2/file_with_data"
    rename id _ID
    
    keep if _merge==3
    drop _merge
    
    bys state: keep if _N==_n
    spmap X 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(Reds2) ocolor(none) clnumber(5)
    Code:
    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

  • #2
    Also, is there a colour palette for which states with a value of X of 0 could be just white, instead of very lightly red shaded?
    You can specify your own colors or create your own palette, including taking an existing palette and adding white as one of the colors. But then, how do you distinguish states with zero values and states with no observations? Your example is really not reproducible without some work. The borders are controlled by the outline color, which you specified as -ocolor(none)- in your code.

    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
    spmap X 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 red*0.5 red*1.0 red*1.5 red*2.0 red*2.5 red*3.0 red*3.5) ocolor(gray ..) ///
    clnumber(5) legend(order(1 "No data" 2 3 4 5 6 7 8 9) pos(5))
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	81.8 KB
ID:	1737017

    Last edited by Andrew Musau; 13 Dec 2023, 10:54.

    Comment


    • #3
      Thanks Andrew! I ran your code and it worked!

      Comment

      Working...
      X