Announcement

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

  • Odd looking map using spmap

    Hi all,

    I am studying the geographical distribution across the US of female registered apprentices. I would like to create a heat map, with colours getting darker as the fraction of registered apprentices who are female increases. My data are at the bottom of the post. This is the code I used to create the base map:


    Code:
    *downloaded from https://www.census.gov/geographies/mapping-files/time-series/geo/carto-boundary-file.html
    global geo "C:\Users\maxen\OneDrive\Desktop\PhD ETHZ\Dissertation\3rd Thesis Paper"
    spshape2dta "$geo/cb_2021_us_state_500k", replace saving(usa_state)
    
    use usa_state, clear
    use usa_state_shp, clear
      merge m:1 _ID using usa_state  // to get the identifiers
      drop _merge 
    bys _ID: keep if [_n]==_N
        save "$geo/usa_state_shp", replace
    This is the code I ran to obtain the heat map:
    Code:
    spmap fraction_female using usa_state_shp, id(_ID) clm(custom) clb(0(4)33.4)  fcolor(Heat)
    And I obtained a very weird looking juxtaposition of planes:

    Click image for larger version

Name:	Planes_heat_bizarre_juxtaposition.PNG
Views:	3
Size:	53.3 KB
ID:	1671111




    Code:
    input byte _ID double(_X _Y) str2 STUSPS float fraction_female
     1  49.02787128888888  -92.08864903414468 "WY" 12.534905
     2  .6057272888888755  -85.39691352525129 "AK" 11.479944
     3  71.47544894003143   -97.5811933302781 "MD"  5.350404
     4  6.336089599999997 -135.90882478523687 "AS"         0
     5  60.71614648888888  -100.0871682947566 "AR"  3.791729
     6 54.009828694937596   -88.0635092613424 "ND" 3.5963066
     7   74.1058450051695  -97.23149612350693 "DE" 1.8789144
     8 230.83951502222223 -118.34808378241493 "GU" 20.253164
     9  50.45321031111111 -104.39470203636344 "NM"  3.030672
    10  46.89567146666667  -99.11300170409012 "UT"  3.777716
    11  69.23150357072505  -98.59377201512844 "WV"  30.06316
    12  55.42987093333333  -96.91340704183914 "KS"  7.014288
    13  67.12521600000001 -101.41434469530365 "GA"  8.620851
    14  62.82310471111111 -104.58474232187172 "MS" 13.690063
    15  58.85211804444444  -87.94999434832852 "MN" 4.3460264
    16  76.40373546666666  -94.20976098904711 "NH"  9.969101
    17  59.89436515555555  -96.37722803660961 "MO"  9.910444
    18  44.62743111111111  -92.69331628139834 "ID" 11.696406
    19 54.003286044444444  -92.09201796278641 "SD"  7.949657
    20  70.74141084444445  -96.06894698385321 "PA"  6.625429
    21 54.753731555555554 -100.06312548284674 "OK"  3.419425
    22          65.352704  -98.83308884149741 "IN"  8.745133
    23  74.26908302222223  -97.25967829058163 "NJ"  6.856187
    24  54.00645792350507  -95.82016655641485 "NE"  9.910556
    25  50.44604231111111  -98.20346381611235 "CO"  8.889801
    26  71.28024746666667    -94.798538114969 "NY" 10.020782
    27   45.4761670022265  -88.45312691127808 "MT" 13.968958
    28   64.3047047111111  -100.0401303056136 "KY"  8.533903
    29  75.77760711111111  -93.04213907329684 "VT"  14.52514
    30  73.15928248888889   -97.8979187299625 "DC"  2.955665
    31  70.66681315555556 -109.48614454061408 "FL"  13.45012
    32  42.66207797468658  -97.58769344334392 "NV"  8.214585
    33 231.03069937777778 -113.06731694435379 "MP"         0
    34  63.77923342222222 -101.39870436109314 "TN"  5.393526
    35  59.27938918599851  -94.32365055815427 "IA" 3.5876575
    36  65.08570524444445 -104.04885037688568 "AL"  5.043982
    37  63.70265884444444  -90.49251856080303 "MI"  7.377975
    38   39.5316615111111  -96.50350034939278 "CA"  2.845142
    39           75.57136  -95.88611067824696 "CT" 3.7974684
    40  46.35285759999999 -103.53270352326808 "AZ"  5.918295
    41 52.163214933333336 -104.04481826371185 "TX"  3.979268
    42  68.49805226666666  -99.99540028869471 "VA"  19.67362
    43  62.92410737777778  -96.74796288668045 "IL"  3.694091
    44 1.1853980444444343   -106.927211020208 "HI"  5.281467
    45 39.420732444444454  -94.22153859615949 "OR" 4.3137255
    46  76.89765849441095  -95.68746238552183 "RI"  6.463878
    47  81.71707591111111 -114.73201703017544 "VI"         0
    48  68.72610844444445 -101.66167718753307 "SC"  22.41447
    49  39.27988764444444  -88.80060382967854 "WA" 16.857914
    50  79.68377528888888 -114.92083362374068 "PR" 33.333332
    51 61.124873244444444 -103.37655052640878 "LA"  11.48805
    52  77.45422008888889   -91.7800524712069 "ME" 29.588015
    53  67.68344248888889  -97.62961592054371 "OH"  5.933706
    54  61.94623004444444   -91.4541702776673 "WI"  8.695652
    55  68.04089543582201 -101.38207922367356 "NC"  3.430079
    56  75.72754346666667  -94.94896627060787 "MA"   6.13563


    Attached Files

  • #2
    I forgot to ask my question: does anyone know what went wrong and how I could obtain a proper heat map of the United States, Alaska and Hawaii?

    Comment


    • #3
      I'm not familiar with spmap, but I do know grmap. I'll look when I'm home from the vision doctor place.

      I should also note that I don't recommend mapping the full US at once, cuz Hawaii and Alaska are super far from the mainland and the map will likely look funny as a result. Or, you could use the boundary box command.

      Comment


      • #4
        Okay I'm back from the eye doctor. This is what I've done.
        Code:
        cd E:\Test\Map
        
        copy "https://www2.census.gov/geo/tiger/GENZ2018/shp/cb_2018_us_state_500k.zip" ///
        "cb_2018_us_state_500k.zip", replace
        
        
        
        
        unzipfile cb_2018_us_state_500k, replace
        
        clear
        input byte _ID double(_X _Y) str2 STUSPS float fraction_female
         1  49.02787128888888  -92.08864903414468 "WY" 12.534905
         2  .6057272888888755  -85.39691352525129 "AK" 11.479944
         3  71.47544894003143   -97.5811933302781 "MD"  5.350404
         4  6.336089599999997 -135.90882478523687 "AS"         0
         5  60.71614648888888  -100.0871682947566 "AR"  3.791729
         6 54.009828694937596   -88.0635092613424 "ND" 3.5963066
         7   74.1058450051695  -97.23149612350693 "DE" 1.8789144
         8 230.83951502222223 -118.34808378241493 "GU" 20.253164
         9  50.45321031111111 -104.39470203636344 "NM"  3.030672
        10  46.89567146666667  -99.11300170409012 "UT"  3.777716
        11  69.23150357072505  -98.59377201512844 "WV"  30.06316
        12  55.42987093333333  -96.91340704183914 "KS"  7.014288
        13  67.12521600000001 -101.41434469530365 "GA"  8.620851
        14  62.82310471111111 -104.58474232187172 "MS" 13.690063
        15  58.85211804444444  -87.94999434832852 "MN" 4.3460264
        16  76.40373546666666  -94.20976098904711 "NH"  9.969101
        17  59.89436515555555  -96.37722803660961 "MO"  9.910444
        18  44.62743111111111  -92.69331628139834 "ID" 11.696406
        19 54.003286044444444  -92.09201796278641 "SD"  7.949657
        20  70.74141084444445  -96.06894698385321 "PA"  6.625429
        21 54.753731555555554 -100.06312548284674 "OK"  3.419425
        22          65.352704  -98.83308884149741 "IN"  8.745133
        23  74.26908302222223  -97.25967829058163 "NJ"  6.856187
        24  54.00645792350507  -95.82016655641485 "NE"  9.910556
        25  50.44604231111111  -98.20346381611235 "CO"  8.889801
        26  71.28024746666667    -94.798538114969 "NY" 10.020782
        27   45.4761670022265  -88.45312691127808 "MT" 13.968958
        28   64.3047047111111  -100.0401303056136 "KY"  8.533903
        29  75.77760711111111  -93.04213907329684 "VT"  14.52514
        30  73.15928248888889   -97.8979187299625 "DC"  2.955665
        31  70.66681315555556 -109.48614454061408 "FL"  13.45012
        32  42.66207797468658  -97.58769344334392 "NV"  8.214585
        33 231.03069937777778 -113.06731694435379 "MP"         0
        34  63.77923342222222 -101.39870436109314 "TN"  5.393526
        35  59.27938918599851  -94.32365055815427 "IA" 3.5876575
        36  65.08570524444445 -104.04885037688568 "AL"  5.043982
        37  63.70265884444444  -90.49251856080303 "MI"  7.377975
        38   39.5316615111111  -96.50350034939278 "CA"  2.845142
        39           75.57136  -95.88611067824696 "CT" 3.7974684
        40  46.35285759999999 -103.53270352326808 "AZ"  5.918295
        41 52.163214933333336 -104.04481826371185 "TX"  3.979268
        42  68.49805226666666  -99.99540028869471 "VA"  19.67362
        43  62.92410737777778  -96.74796288668045 "IL"  3.694091
        44 1.1853980444444343   -106.927211020208 "HI"  5.281467
        45 39.420732444444454  -94.22153859615949 "OR" 4.3137255
        46  76.89765849441095  -95.68746238552183 "RI"  6.463878
        47  81.71707591111111 -114.73201703017544 "VI"         0
        48  68.72610844444445 -101.66167718753307 "SC"  22.41447
        49  39.27988764444444  -88.80060382967854 "WA" 16.857914
        50  79.68377528888888 -114.92083362374068 "PR" 33.333332
        51 61.124873244444444 -103.37655052640878 "LA"  11.48805
        52  77.45422008888889   -91.7800524712069 "ME" 29.588015
        53  67.68344248888889  -97.62961592054371 "OH"  5.933706
        54  61.94623004444444   -91.4541702776673 "WI"  8.695652
        55  68.04089543582201 -101.38207922367356 "NC"  3.430079
        56  75.72754346666667  -94.94896627060787 "MA"   6.13563
        end
        sa testdata, replace
        
        spset _ID, coord(_X _Y)
        spset, modify shpfile(states_shp.dta)
        
        
        grmap fraction_female, ///
            fcolor(Greens2) ///
            clmethod(quantile) ///
            clnumber(4) ///
            ndfcolor(black)
        I can't replicate the problem here. Presumably it is an idiosyncrasy with spmap.

        My advice to you is this: unless you need to do some sexy type of map like... this guy Asjad Naqvi , who goes WILD with Stata mapping (you'd think it wasn't done in Stata at all), my advice for you is to use grmap. It's already a pretty powerful tool (even if it isn't like R), and it should be enough for what you'd need Maxence Morlet



        UPDATE: Note that my graph looks funny, as suspected. So you'll need to do a little trimming.
        Attached Files
        Last edited by Jared Greathouse; 27 Jun 2022, 10:43.

        Comment


        • #5
          Thank you Jared Greathouse grmap is just the official version of spmap so it won't get a different output.

          Maxence Morlet you don't need to merge anything after running the spshape2dta command. You are messing up the usa_state_shp shapefile, which you should not touch under any circumstances if you don't know what you are doing. In short, it has a certain sort order which allows the boundaries to be generated correctly. Also read the help file for spmap carefully.

          Here is what you should do:

          Step 1:

          spshape2dta "$geo/cb_2021_us_state_500k", replace saving(usa_state)

          Step 2: Go straight to mapping

          spmap fraction_female using usa_state_shp, id(_ID) clm(custom) clb(0(4)33.4) fcolor(Heat)


          I have a guide here which is a slow intro to mapping with Stata: https://medium.com/the-stata-guide/c...s-fbd4fe2642f6

          Comment


          • #6
            You're most welcome. I didn't know they were equivalent.


            And yes, the data_shp file is a very-bad no-touch-touch file, I've only needed to interact with it when I was doing this.


            The only merging you would need to do, is merging your outcome data to your shapefile (the dataset, not the coordinate file). In fact, that's usually how I make panel data these days, I just get a nice looking shapefile and do a little data management and boom.

            Comment


            • #7
              Thanks both for your recommendations! I'm quite new to spatial analysis using Stata, it's a great tool!

              I'll read up on the grmap command, and also give the intro to mapping a read!

              Thanks again both

              Comment


              • #8
                Another example:
                Code:
                clear*
                copy "https://www2.census.gov/geo/tiger/GENZ2018/shp/cb_2018_us_state_20m.zip" map.zip, replace
                
                unzipfile map.zip,replace
                spshape2dta cb_2018_us_state_20m,replace
                
                //drop outlying territories
                use cb_2018_us_state_20m.dta ,clear
                drop if inlist(STATEFP ,  "72" , "78", "69", "66", "60")
                save, replace
                
                use cb_2018_us_state_20m_shp,clear
                // drop Hawaii and Alaska islands that wrapped around
                replace _X = cond(_X > 0, ., _X) if inlist(_ID, 26, 49)
                geo2xy _Y _X , replace  
                save, replace
                
                use cb_2018_us_state_20m.dta ,clear
                
                gen x = runiform()
                colorpalette viridis, n(15) nograph
                local colors `r(p)'
                
                spmap x using cb_2018_us_state_20m_shp , id(_ID) fcolor("`colors'") /// 
                    osize(0.02 ..) cln(15) legend(off)
                Click image for larger version

Name:	map1.png
Views:	1
Size:	64.1 KB
ID:	1671155


                Comment

                Working...
                X