Announcement

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

  • How to add state names and labels using -spmap-?

    Hi all, I am trying to plot a U.S. map using the data I have. So far I was able to get a map like the following:

    Code:
    spmap pct_gedtimehi using uscoord if id !=1 & id!=4 & id!=13 & id!=53 & id!=54 & id!=55, id(id) ///
    fcolor(BuRd) ocolor(white ..) osize(thin ..) legend(position(8))
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	33.8 KB
ID:	1395568



    But I want to add state names on the map as well as the data values for each state. How can I do that?
    To give you a sense of what the data looks like, below I grab a couple of data for you:

    Code:
     list id NAME pct_gedtimehi in 1/9
    
         +--------------------------------+
         | id             NAME   pct_ge~i |
         |--------------------------------|
      1. |  1           Alaska       0.63 |
      2. |  2          Alabama       0.87 |
      3. |  3         Arkansas       0.55 |
      4. |  4   American Samoa       0.93 |
      5. |  5          Arizona       0.63 |
         |--------------------------------|
      6. |  6       California       0.55 |
      7. |  7         Colorado       0.75 |
      8. |  8      Connecticut       0.76 |
      9. | 10         Delaware       0.69 |
         +--------------------------------+
    Thanks!

  • #2
    You can use the by() suboption with the label option. You will need to first create a data set with the text and data values you wish to use as labels.

    For example:
    Code:
    use "Italy-RegionsData.dta", clear
    gen labtype =1
    append using "Italy-RegionsData.dta"
    replace labtype = 2 if labtype==.
    replace region = string(relig1 , "%4.1f") if labtype == 2
    keep xcoor ycoor region labtype
    save spmaplabels.dta,replace
    
    use "Italy-RegionsData.dta", clear
    spmap relig1 using "Italy-RegionsCoordinates.dta", id(id)  /// 
      fcolor(BuRd) ocolor(white ..) /// 
      label(data(spmaplabels) xcoord(xcoord)  ycoord(ycoord) ///
      label(region) by(labtype) size(*0.85 ..) pos(12 0) )
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	34.7 KB
ID:	1395575

    Comment


    • #3
      Hi Scott, thanks for your reply. Where can I find the xcoor and ycoor? In the dataset that I used to plot the map, there is no variables such as xcoor or ycoor, could you let me know where did you get these? Are they from the uscoord file?

      Comment


      • #4
        I guess I assumed the files that contained the boundary data also had the centroids.

        Using the shape files from https://www.census.gov/geo/maps-data...cbf_state.html and Richard Picard's geo2xy (from SSC) to created projections from the unprojected shape files.

        Code:
        cd "C:\Users\Scott\Desktop\tmp"
        shp2dta using cb_2016_us_state_20m, database(state_data) ///
            coordinates(state_coordinates) genid(id) gencentroids(c) replace
        
        use "state_coordinates.dta", clear
        geo2xy _Y _X,   projection( mercator) replace
        local lon0 = r(lon0)
        local f = r(f)
        local a = r(a)
        save "state_coordinates_mercator.dta", replace
        use state_data,clear
        quietly destring STATEFP, generate(st)
        //keep contiguous US
        drop if st==2 | st==15 | st>56
        gen x = runiform()
        
        save "state_data2",replace
        gen labtype  = 1
        append using state_data2
        replace labtype = 2 if labtype==.
        replace NAME = string(x, "%3.2f") if labtype ==2
        keep x_c y_c NAME labtype
        geo2xy  y_c x_c,   projection( mercator, `a' `f' `lon0' ) replace
        save maplabels, replace
        
        use state_data2,clear
        
        spmap x using "state_coordinates_mercator.dta", id(id) /// 
          fcolor(BuRd) ocolor(white ..) /// 
           label(data(maplabels) xcoord(x_c)  ycoord(y_c) ///
          label(NAME) by(labtype)  size(*0.85 ..) pos(12 0) )
        Click image for larger version

Name:	usmap.png
Views:	1
Size:	75.1 KB
ID:	1395585

        Comment


        • #5
          Thanks Scott, that helps.

          Comment


          • #6
            Hi everyone (and Scott Merryman, Robert Picard )


            Do you know how I can move the name of states in the top-left corner of #4 (e.g., New Hampshire, Delaware...) for easy to see? Like the following map:

            ​​​​​​​
            Last edited by Linh Nguyen; 08 Sep 2018, 06:04.
            --------------------
            (Stata 15.1 MP)

            Comment


            • #7
              See this thread from a few months ago.

              Comment


              • #8
                Thanks Robert
                --------------------
                (Stata 15.1 MP)

                Comment


                • #9
                  Hi,

                  On using spmap to graph maps with both text and numeric labels by Scott, I tried doing the same thing but kept on getting an error code:

                  use "liberia_data4.dta", clear
                  gen lab=1
                  append liberia_data4.dta
                  replace lab = 2 if lab==.

                  replace per_overage = string ( percentage_overage ,"%4.1f") if lab==2
                  string not found
                  r(111);


                  The variable per_overage is numeric. I would greatly appreciate any help.

                  Best,
                  M

                  Comment


                  • #10
                    I suspect you mean percentage_overage is numeric, because you are converting its values to string. Try removing the space between "string" and the first parenthesis.

                    replace per_overage = string( percentage_overage ,"%4.1f") if lab==2
                    David Radwin
                    Senior Researcher, California Competes
                    californiacompetes.org
                    Pronouns: He/Him

                    Comment


                    • #11
                      Originally posted by David Radwin View Post
                      I suspect you mean percentage_overage is numeric, because you are converting its values to string. Try removing the space between "string" and the first parenthesis.

                      replace per_overage = string( percentage_overage ,"%4.1f") if lab==2
                      Thank you David for the response! And yes, I did mean percentage_overage over there.

                      Maybe Scott's input isn't as relevant to my case as I thought it was. To come to what I want to do, I would like to graph a map of the percentage of overage students at the county level. I would like both the label of the county and its corresponding percentage_overage value. The data is in this form (I have cropped out certain variables from my dataset for simplicity here):
                      id x_c y_c county percentage_overage
                      1 -10.8155 6.698635 A 98.7786
                      2 -9.64671 6.948652 B 95.31998
                      3 -10.3107 7.39673 C 82.0696
                      5 -9.81485 6.217867 D 92.32929
                      4 -11.0354 7.093503 E 98.08186
                      6 -8.2063 5.939131 F 96.1993
                      7 -8.18662 4.779776 G 100
                      8 -9.86518 7.978003 H 96.72065
                      9 -10.2087 6.514746 I 91.62863
                      10 -7.76482 4.72878 J 99.1875
                      11 -10.548 6.54193 K 84.71515
                      12 -8.77266 6.803093 L 94.29958
                      13 -9.38665 5.878731 M 99.90253
                      14 -7.8724 5.287213 N 90.01772
                      15 -8.81485 5.347509 O 94.05312

                      Comment


                      • #12

                        how can I give the name of a place in a city.
                        Click image for larger version

Name:	contoh 2.png
Views:	1
Size:	24.4 KB
ID:	1513820

                        I mean it will turn out like this
                        Click image for larger version

Name:	peta 26 puskesmas.png
Views:	1
Size:	77.1 KB
ID:	1513821
                        I already have the coordinates of these places. Thanks you for helpfull

                        Comment


                        • #13
                          Robert Picard can you help me?

                          Comment


                          • #14
                            Scott Merryman, thank for providing an excellent example. Am trying to run same stuff to include value and label on the map but experienced difficulties. I tired to follow the example you gave but not working.

                            ***import the shapefile downloaded
                            cd "C:\Users\USER\Desktop\map\nga_admbnda_adm1_os gof"

                            **install shp2dta to convert shapefiles to stata format


                            ****convert shapefile to two *.dta files: ngdata.dta and ngcoord.dta

                            **import the attributes table i.e. the data you want to plot
                            import excel "data.xls", firstrow
                            save GIStraining

                            ***merge GIStraining.data to the ngdata.data
                            merge 1:1 id using "GIStraining.dta"
                            keep if _merge==3

                            **keep and save the variables you need for Creating the Map
                            keep id x_coord y_coord name data
                            save Spatial_analysis

                            * install the spmap produces the map


                            *****recoding the number of cases to a categorical variable
                            recode data 1/99=1 100/999=2 1000/4999=3 5000/max=4, gen (grp3)
                            label define grp4 1 "1-99" 2 "100-999" 3 "1,000-4,999" 4 "{&ge}5,000 "
                            label values grp3 grp4
                            tab grp3

                            ***Creating the map using the categorical variable
                            spmap grp3 using ngcoord.dta, id(id) ///
                            clmethod(unique) ///
                            label(label(name) xcoord(x_coord) ycoord(y_coord) size(vsmall)) ///
                            fcolor(Reds) legtitle("Number of Cases") ///
                            title("xxxx")
                            graph export map.png, replace

                            Please help - same applies to Africa Map.
                            Last edited by Bamidele Moyo; 19 Jun 2020, 05:57.

                            Comment


                            • #15
                              If you want both a value and label on map you need to use the -by()- option within -label()-.

                              Comment

                              Working...
                              X