Announcement

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

  • spmap resulting in blank spaces

    I am trying to produce a world map with countries grouped into 3 categories. I can get the following command to draw a perfect map, which includes all countries. However, when I add any attribute, the resulting map drops about a dozen countries. Why?

    This code works fine:
    spmap using global_coord , id(id)

    I start loosing countries with either of these codes:
    spmap type using global_coord, id(id) fcolor(Blues) ndfcolor(red) clmethod(unique)
    spmap type using global_coord , id(id) fcolor(Blues2) ocolor(BuRd)

    Every country has complete data available for the variables involved, so why is spmap dropping countries when I include an attribute?

    Thanks!

  • #2

    Are you sure that you uploaded your database?
    Code:
    use global_db.dta, clear  
    
    spmap type using global_coord , id(id) fcolor(Blues2) ocolor(BuRd)

    Comment


    • #3
      Yes, I double checked that I loaded the correct file. This is driving me crazy.

      Comment


      • #4
        What do the maps look like? Section 12 of the FAQ explains how you can attach images to a post. Please include the exact commands that you used to create the maps.

        Comment


        • #5
          You can found a map here:

          http://www.naturalearthdata.com/http..._countries.zip

          I use the following code


          Code:
          wbopendata, language(en - English) indicator(it.cel.sets.p2) long clear latest
          rename countryname country
          sort country
          save telephone, replace
          
          shp2dta using world_adm0, database("wdb.dta") coordinates("wcoord.dta") genid(id) gencentroids(stub) replace
          
          use wdb, clear
          rename NAME country
          
          sort country
          save "wdb.dta", replace
          merge country using  telephone.dta
          spmap  it_cel_sets_p2 using wcoord , id(id) fcolor(Blues2) ocolor(BuRd)
          I get this graph

          graph_telephone.gph

          Graph.pdf

          Comment


          • #6

            You are not matching some countries because of country name differences. You have 2-digit ISO country codes in both sources so the following example uses those codes to match countries. There are a few duplicates in both files; I just drop the duplicates.

            Also, the shapefile contains geographic coordinates (lat/lon). I show how to use geo2xy (from SSC) to convert from geographic to cartesian coordinates. The map will have the same proportions as Google Maps.

            Code:
            * I saved the dataset created by:
            * wbopendata, language(en - English) indicator(it.cel.sets.p2) long clear latest
            * into "wopendata.dta"
            
            * drop two observations without a 2-digit ISO country code
            use "wopendata.dta", clear
            list if mi(iso2code)
            drop if mi(iso2code)
            isid iso2code
            save "wopendata_fixed.dta", replace
            
            * implement the same fix with the shapefile database
            shp2dta using "ne_110m_admin_0_countries/ne_110m_admin_0_countries.shp", ///
                database("wdb.dta") coordinates("wcoord.dta") genid(id) replace
            use "wdb.dta", clear
            list name iso_a2 if iso_a2 == "-99"
            drop if iso_a2 == "-99"
            isid iso_a2
            save "wdb_fixed.dta", replace
            
            * You should never make a map using unprojected coordinates. Use -geo2xy-
            * to convert to x,y using the same projection as Google Maps. To install,
            * type: ssc install geo2xy
            use "wcoord.dta", clear
            replace _Y = -90 if _Y < -90    // fix invalid coordinates
            replace _X = 180 if _X > 180    // fix invalid coordinates
            
            geo2xy _Y _X, replace
            save "wcoord_xy.dta", replace
            
            * now that we have a unique identifier in both dataset, merge the wbopendata
            * with the shapefile database
            use "wopendata_fixed.dta", clear
            rename iso2code iso_a2
            merge 1:1 iso_a2 using "wdb_fixed.dta"
            
            * these are countries that have no match
            list countryname iso_a2 if _merge == 1    // from "wopendata_fixed.dta"
            list name iso_a2 if _merge == 2         // from "wdb_fixed.dta"
            
            * make a map of countries that have matched
            spmap  it_cel_sets_p2 if _merge == 3 using "wcoord_xy.dta" , id(id) fcolor(Blues2) ocolor(BuRd)
            graph export "it_cel_sets_p2.png", replace
            Here's the resulting map

            Click image for larger version

Name:	it_cel_sets_p2.png
Views:	1
Size:	809.2 KB
ID:	1328755

            Last edited by Robert Picard; 29 Feb 2016, 18:35.

            Comment


            • #7
              Layton, has your problem been solved?

              Comment


              • #8
                I am still working on the problem. I will try Robert's approach today and see if I can achieve the desired results.

                So far, here is what I am doing:

                use "C:\mapping_data.dta", clear
                spmap using global_coord, id(id)

                Click image for larger version

Name:	
Views:	2
Size:	53.5 KB
ID:	1328818

                However, when I add any variable using the following code, countries start to disappear. I don't understand how one map is perfect but the addition of any new variable causes chaos:

                spmap type using global_coord, id(id) fcolor(Blues) ndfcolor(red) clmethod(unique)

                Click image for larger version

Name:	
Views:	2
Size:	54.7 KB
ID:	1328819



                I can't find any duplicate measures but maybe I am missing something. Like I said, I will try Richard's approach and see what happens.

                Thanks!

                Comment


                • #9
                  I did not notice that #5 was from Patricio and not Layton. I'm stumped as to why polygons disappear like that.

                  Comment


                  • #10
                    Layton, even your original map is incomplete. Greenland, Iceland, the Western Sahara and many islands in the Pacific, Atlantic and Indian Oceans are missing. Can you try a different shapefile?

                    Comment


                    • #11
                      Yeah, I am working on a new shapefile now.

                      Comment


                      • #12
                        There must have been a problem with the shapefile. It looks like everything is better now! Thanks so much for all the suggestions!

                        Comment


                        • #13
                          I have a similar problem:

                          I get a map where all countries appear although I have missing data for most countries. For a year in which I have data for at least 68 countries I get a map in which most countries do not appear at all. I cannot make sense of that at all.

                          My code is

                          Code:
                          use master_0901.dta, clear
                          
                          
                          *harmonize cnames
                          replace cname="Cyprus" if cname=="Cyprus (1975-)"
                          replace cname="Swaziland" if cname=="Eswatini (former Swaziland)"
                          replace cname="Ivory Coast" if cname=="Cote d'Ivoire"
                          
                          keep if year == 2017
                          
                          save maps_data, replace
                          
                          use db, clear
                          
                          *merge
                          merge m:1 cname using maps_data
                          
                          tab cname if _m==2
                          
                          drop _m
                          
                          
                          *SDG10
                          *wdi_gini
                          spmap wdi_nersf using coord, id(id) fcolor(Blues)  ndocolor(black)   ndpattern(solid)  clmethod(custom) clbreaks(0 20 30 40 50 60)  legorder(hilo)  leg(pos(9)) title("Gini Index", size(medium))
                          It worked perfectly for 10 other variables just now. Is there any obvious mistake I am overseeing?
                          Last edited by Julian Schaper; 17 Nov 2021, 04:25.

                          Comment


                          • #14
                            My apologies. I found the typing error

                            Comment

                            Working...
                            X