Announcement

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

  • #31
    Hi Michael Stepner, thank you for creating this resource. I am trying create a U.S. state map with the values of the variable I am mapping placed in or near the states (do not need state name, only the value of the variable). Is there a guide to doing that?

    The data I am using has 51 values, one for each state + DC, with only three variables: state name, state fips, and the variable of interest. This code produces the graph without the labels:

    Code:
    maptile var, geo(state) geoid(state) fcolor(BuYlRd) nq(6) leg(1)
    I believe I need to merge on a file with state coordinates, and use spopt to specify the x and y coordinates, but I wondered if you could point me to any code that does this or an example? Thanks, again.

    Comment


    • #32
      Hi Jake Mortenson, you're on the right track! I haven't done this myself, so I don't have any code to point you to. But your plan to use spopt() with the label_suboptions of spmap is the right way to do it.

      You could create a dataset with the labels you want to use and the lat/lon coordinates you want to place them at. Then you'll need to transform the lat/lon coordinates into x,y coordinates using the same transformation that I've used to generate the state map template. You can grab that from the state Source Files zip on the maptile website: check out reshape_us.do within the zip file. Then construct your spopt() argument appropriately.

      Comment


      • #33
        Great, thanks!
        Last edited by Jake Mortenson; 09 Feb 2018, 09:42.

        Comment


        • #34
          OK, hopefully not too many more follow-ups, but am running into a strange issue. Here is the code I have run:

          Code:
            use DIRECTORY/ado/personal/maptile_geographies/state_coords_clean, clear
                         
                          bysort _ID: egen x=median(_X)
                          bysort _ID: egen y=median(_Y)
                         
                          collapse (lastnm) x y,by(_ID)
                         
                          preserve
                         
                                          use DIRECTORY/ado/personal/maptile_geographies/state_database_clean, clear
                                         
                                          keep statefips _polygonid
                                         
                                          rename _polygonid _ID
                                         
                                          save DIRECTORY/ado/personal/maptile_geographies/state_id_fips, replace
                                         
                          restore
                         
                          merge 1:1 _ID using DIRECTORY/ado/personal/maptile_geographies/state_id_fips, update replace nogen
                         
                          * this file contains state, statefips, and VAR (variable of interest)
                          merge 1:1 statefips using data/MYDATA, update replace nogen            
                         
                          replace VAR=round(VAR*100,0.1)
                         
                          * center the labels on the state
                          * al
                          replace x=x+0.75 if statefips==1
                          replace y=y+2 if statefips==1
                         
                          * ak
                          replace x=x+0.5 if statefips==2
                          replace y=y+3 if statefips==2
                        
                          keep x y state VAR
           
                          #delimit;
                          maptile VAR, geo(state) geoid(state) legd(1)  nq(5)
                                          spopt(
                                                          label(xcoord(x) ycoord(y) size(small) label(diff))
                                                          );
          When I run this, I get an error associated with the maptile command:
          .1" "2552360 invalid name
          When I include an fcolor() option (e.g. Blues), it runs, but does not process the fcolor, saying it is not found in class color. Any idea what I am doing wrong?

          Comment


          • #35
            [Uninstalled and re-installed maptile and the geostate geographies, and the problem went away.]

            OK, no more posts from me with questions, next post will contain the code I used to add the labels.

            Comment


            • #36
              Jake Mortenson: Sounds like you had a busy day and debugged all the issues you encountered. I'm looking forward to seeing the fruits of your labors!

              Comment


              • #37
                OK, the code I used to center the labels is below, along with the resulting picture. I generated simulated data as a starting point, and others should be able to run the code once they specify their Stata directory.

                My approach for centering the labels on the states was very rudimentary and labor-intensive. I think a centroid can be calculated when reshaping the files using shp2dta, which would presumably make much of the small manual adjustments unnecessary. Any idea how to do that?

                Code:
                * sample code to place labels on states using maptile 
                * Jake Mortenson
                
                cap log close
                clear all
                set more off
                local direct="YOUR-STATA-DIRECTORY"
                cd `direct'
                
                    
                use ado/personal/maptile_geographies/state_coords_clean, clear
                    
                    bysort _ID: egen x=median(_X)
                    bysort _ID: egen y=median(_Y)
                    
                collapse (lastnm) x y,by(_ID)
                    
                    preserve
                    
                        use ado/personal/maptile_geographies/state_database_clean, clear
                        
                        keep statefips _polygonid
                        
                        rename _polygonid _ID
                        
                        save ado/personal/maptile_geographies/state_id_fips, replace
                        
                    restore
                    
                merge 1:1 _ID using ado/personal/maptile_geographies/state_id_fips, update replace nogen
                
                
                set seed 8675309    
                gen var=runiform(-5,5)
                    
                    replace var=round(var,0.1)
                    
                    * center the labels on the state
                    * al
                    replace x=x+0.75 if statefips==1
                    replace y=y+2 if statefips==1
                    
                    * ak
                    replace x=x+0.5 if statefips==2
                    replace y=y+3 if statefips==2
                    
                    * az
                    replace x=x+2.5 if statefips==4
                    replace y=y+0.5 if statefips==4
                
                    * ar
                    replace x=x-0.75 if statefips==5
                    replace y=y-0.5 if statefips==5
                
                    * ca
                    replace x=x+1.5 if statefips==6
                    replace y=y+0.5 if statefips==6
                
                    * co
                    replace x=x-2.25 if statefips==8
                
                    * ct
                    replace x=x+0.15 if statefips==9
                    replace y=y+0.55 if statefips==9
                
                    * de TOO SMALL, out to sea!
                    replace y=y-0.85 if statefips==10
                    replace x=x+1.5 if statefips==10
                    
                    * dc TOO SMALL
                    replace y=. if statefips==11
                    replace x=. if statefips==11
                
                    * fl
                    replace x=x+0.75 if statefips==12
                
                    * ga
                    replace x=x-0.5 if statefips==13
                    replace y=y+0.5 if statefips==13
                
                    * hi
                    replace x=x+3.5 if statefips==15
                    replace y=y-1.75 if statefips==15
                
                    * id
                    replace y=y-2 if statefips==16
                    
                    * il is fine
                    
                    * in
                    replace x=x+0.4 if statefips==18
                    replace y=y+1.25 if statefips==18
                    
                    * ia is fine
                    
                    * ks
                    replace x=x-0.5 if statefips==20
                    
                    * ky
                    replace x=x+1 if statefips==21
                    replace y=y-0.25 if statefips==21
                    
                    * la
                    replace x=x-1.75 if statefips==22
                    replace y=y+1.0 if statefips==22
                
                    * me
                    replace x=x-0.5 if statefips==23
                    replace y=y+1.75 if statefips==23
                
                    * md TOO SMALL out to sea
                    replace x=x+2 if statefips==24
                    replace y=y-1 if statefips==24
                
                    * ma
                    replace x=x+1.25 if statefips==25
                    replace y=y+1 if statefips==25
                    
                    * mi
                    replace x=x+1.5 if statefips==26
                    replace y=y-4 if statefips==26
                
                    * mn
                    replace x=x-2 if statefips==27
                    replace y=y-1 if statefips==27
                    
                    * ms
                    replace x=x+0.5 if statefips==28
                    replace y=y+1.5 if statefips==28
                    
                    * mo is fine
                    
                    * mt
                    replace x=x+2.5 if statefips==30
                    
                    * ne
                    replace x=x-0.5 if statefips==31
                    replace y=y+0.75 if statefips==31
                
                    * nv
                    replace x=x-1.75 if statefips==32
                    replace y=y+2.5 if statefips==32
                    
                    * nh
                    replace x=x-0.35 if statefips==33
                    replace y=y-0.35 if statefips==33
                    
                    * nj OUT TO SEA
                    replace x=x+1.6 if statefips==34
                    replace y=y-0.25 if statefips==34
                
                    * nm
                    replace x=x-0.75 if statefips==35
                    replace y=y+1.5 if statefips==35
                
                    * ny
                    replace x=x-1.25 if statefips==36
                    replace y=y+2.25 if statefips==36
                    
                    * nc
                    replace x=x-2 if statefips==37
                    
                    * nd
                    replace x=x+0.85 if statefips==38
                    replace y=y+1.35 if statefips==38
                    
                    * oh
                    replace x=x-0.35 if statefips==39
                    replace y=y-1.25 if statefips==39
                    
                    * ok
                    replace y=y-1 if statefips==40
                    
                    * or
                    replace x=x+2.5 if statefips==41
                    replace y=y-1.5 if statefips==41
                    
                    * pa is fine
                    
                    * ri
                    replace x=x+0.35 if statefips==44
                    replace y=y-0.8 if statefips==44    
                    
                    * sc is fine
                    
                    * sd
                    replace x=x-0.75 if statefips==46
                    replace y=y-0.5 if statefips==46
                
                    * tn
                    replace y=y-0.35 if statefips==47
                
                    * tx
                    replace x=x-1.25 if statefips==48
                    replace y=y+2.75 if statefips==48
                
                    * ut is fine
                    
                    * vt
                    replace x=x-0.25 if statefips==50
                    replace y=y+2.1 if statefips==50
                
                    * va
                    replace x=x-1.5 if statefips==51
                    replace y=y+0.25 if statefips==51
                    
                    * wa
                    replace x=x+2 if statefips==53
                    replace y=y-1.25 if statefips==53
                    
                    * wv
                    replace x=x-0.25 if statefips==54
                    replace y=y-0 if statefips==54
                    
                    * wi
                    replace x=x-1.75 if statefips==55
                    replace y=y-0.5 if statefips==55
                    
                    * wy
                    replace x=x-1.5 if statefips==56
                    
                    keep x y state var statefips
                
                #delimit;
                maptile var, geo(state) geoid(state) legd(1) fcolor(BuYlRd) cutvalues(-4(2)4)
                    spopt(
                        label(xcoord(x) ycoord(y) size(small) label(var))
                        )    
                    savegraph(sample_state_label.png) replace;
                Click image for larger version

Name:	sample_state_label.png
Views:	1
Size:	477.3 KB
ID:	1429569

                Comment


                • #38
                  Nice work Jake Mortenson! The product looks really nice.

                  If you wanted to generate centroids, you could download the state Source Files zip on the maptile website, and adapt build_state.do. You'd need to add the gencentroids() option to the -shp2dta- statement, and project the resulting variables from lat/lon to x/y space.

                  Comment


                  • #39
                    This is a heads up post for anyone who is googling a bug in maptile where they get an error message saying:

                    Code:
                    option la() not allowed
                    I've received two reports of this bug by email. Both people encountering it were using Stata 15.0 on Windows 7.

                    Solution: Upgrading from Stata 15.0 to Stata 15.1 solves the problem.

                    Comment


                    • #40
                      A further clarification about the option la() not allowed error in maptile: it doesn't only happen on Windows 7. But it is consistently fixed by upgrading Stata from 15.0 to 15.1, according to the reports I've received.

                      Thank you to Greg Bruich and Puneet for figuring this out with me, since I haven't had access to Stata 15.0 to debug the issue.

                      Comment


                      • #41
                        Hello Michael Stepner, I have been tasked at work with creating a map of zip codes of research participants accessing care at our clinical site. I'll be honest, I'm a total noob when it comes to STATA only taking one stats class where it was the main program we used for analysis. Anyhow, I installed maptile and the corresponding zip5 geography ado and ran my code, but I keep getting the "variable zip5 not found" error after running the line of code, maptile zipfreq, geography(zip5). Do you have any idea how to resolve this? Thanks in advance!


                        Comment


                        • #42
                          Hello Duard Gali, does your dataset contain a variable named "zip5" containing the 5-digit zip codes associated with each "zipfreq" that you want to plot on a map?

                          The introductory slide deck posted on the maptile website may help you. It walks through how to structure your data to produce a map for the first time.

                          Comment


                          • #43
                            Michael Stepner Beautiful, that did it! Thank you for your help!

                            Comment


                            • #44
                              Originally posted by Michael Stepner View Post
                              maptile makes it easy to map a variable in Stata. It is now available in the SSC, with thanks to Kit Baum.

                              maptile generates choropleth maps. It divides the areas on the map into groups and shades them in increasing intensity according to the value of the variable being plotted. The map itself is drawn using geography templates, which can be downloaded here. These templates are easy to create and share.

                              Check out the maptile website for further details!



                              maptile is built on top of Maurizio Pisati’s spmap. spmap is an outstanding program which runs behind-the-scenes to draw each map when you run maptile. Without spmap, maptile would not exist.

                              A key contribution of maptile is its very accessible syntax. As its help file states, “spmap gives the user full control over the formatting of almost every map element, thus allowing the production of highly customized maps”. When using maptile, most of these customizations are stored away in the geography template. As a result, the syntax for making highly customized maps using maptile can be very simple. Additionally, the geography templates can be easily shared and used by others.


                              Thanks so much for the wonderful package! Are there geography templates for countries other than Canada and USA yet, particularly Asia and African countries?

                              Comment


                              • #45
                                Originally posted by Sonnen Blume View Post


                                Thanks so much for the wonderful package! Are there geography templates for countries other than Canada and USA yet, particularly Asia and African countries?
                                None yet, but only because no one has made them!

                                Comment

                                Working...
                                X