Announcement

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

  • Generate a variable from specific observations in order to make a map

    Dear Statalisters,

    I have the following data/output table:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str12 varname float vote
    "usa"            .4789
    "slovakia"        .651
    "slovenia"        .687
    "czech"           .567
    "ukraine"         .376
    "indonesia"       .453
    "russia"          .211
    "romania"         .475
    "japan"          .7486
    "bulgaria"        .497
    "austria"         .775
    "germany"         .435
    "iceland"        .5305
    "uk"          .4266668
    "morrocco"     .236364
    "brazil"       .546667
    "switzerland"    .7366
    end

    and my variable vote reports the percentage of votes in governmental elections expected from specific European countries. Most importantly, varname includes some world countries where I'm only interested in the European (Slovakia, Slovenia, Czech Republic, Ukraine, Russia, Romania, Bulgaria, Austria, Germany, Iceland, Switzerland and the UK), which are the most important part of my analysis.

    My plan was to map these variable vote only through observations on European countries in a Europe map as opposed to a world map (possibly using command -spmap- (ssc, I think) or even putting it directly on Scapetoad: a cartogram-making program). For that, I think, I need to create a single variable out of these 12 observations, and not include ones like USA or Indonesia.

    I am at a loss on how to do this. Maybe with -egen-, it has many functionalities that I'm probably unaware of, but I still don't know how to code it.

    If anyone had any advice I would immensely appreciate it. Or alternatively, if anyone knows a way of applying these 12 specific observations to make a map, that would also be great.


    I am using Stata 14 on Mac.
    Thank you so much
    Last edited by Ana Vargas; 14 Oct 2020, 15:24.

  • #2
    I think a bar chart might be more effective in this case, but FWIW here's an example you can possibly leverage.

    Note this requires spmap, colorpalette, and shp2dta (all from SSC).

    Code:
    clear all
    
    // shapefile source https://hub.arcgis.com/datasets/a21fdb46d23e4ef896f31475217cbb08_1
    !curl -L https://opendata.arcgis.com/datasets/a21fdb46d23e4ef896f31475217cbb08_1.zip > a21fdb46d23e4ef896f31475217cbb08_1.zip
    unzipfile "a21fdb46d23e4ef896f31475217cbb08_1.zip"
    local files: dir . files "*.shp"
    shp2dta using `files' , database(dbase) coordinates(coor) replace
    
    
    clear
    input str12 varname float vote
    "usa"            .4789
    "slovakia"        .651
    "slovenia"        .687
    "czech"           .567
    "ukraine"         .376
    "indonesia"       .453
    "russia"          .211
    "romania"         .475
    "japan"          .7486
    "bulgaria"        .497
    "austria"         .775
    "germany"         .435
    "iceland"        .5305
    "uk"          .4266668
    "morrocco"     .236364
    "brazil"       .546667
    "switzerland"    .7366
    end
    
    * remove unwanted
    drop if inlist(lower(varname), "usa", "indonesia", "morrocco", "brazil", "japan" )
    
    gen pct = vote * 100
    clonevar CNTRY_NAME = varname
    replace CNTRY_NAME = "United Kingdom" if CNTRY_NAME == "uk"
    replace CNTRY_NAME = "Czech Republic" if CNTRY_NAME == "czech"
    replace CNTRY_NAME = proper(CNTRY_NAME)
    
    merge 1:1 CNTRY_NAME using "dbase.dta",  assert(using matched) nogen
     
    
    colorpalette HSV heat, n(5) reverse nograph
    local colors `r(p)'
    
    spmap pct   using coor.dta,                                                ///
    id(_ID) ocolor(black ..) osize(vvthin ..)                               ///
    clmethod(custom) clbreaks(0(20)100)  fcolor("`colors'")   ///
    legend(pos(9) size(*0.8)) legstyle(2)                                 ///
    title("Title Goes Here", size(small))                                   ///
    note("Source Goes Here" , size(tiny))

    Comment


    • #3
      Thank you Justin for your help.

      I seem to be facing 2 additional problems:
      1. The merge goes well, all countries are assigned their country code. However, the map still has no color. Nothing is painted. Not really sure if that's a problem of the -colorpalette- command or if -spmap- ins't recognising something or something else. Also -colorpalette- apparently only works in Stata 14.2 and I have Stata 14.0 I suppose, so an error appears telling me to use the equivalent -colorpalette9-, I'm not sure if that's not part of the problem. Here's an image of the problem:
      Screenshot 2020-10-15 at 08.43.09.png

      2. That being said, is there a way I can get only a map of Europe instead of using the entire world map? Like cropping it in Stata or something? I feel like much of the interpretation is left out here.
      Thank you so much in advance

      Comment


      • #4
        Also -colorpalette- apparently only works in Stata 14.2 and I have Stata 14.0 I suppose, so an error appears telling me to use the equivalent -colorpalette9-, I'm not sure if that's not part of the problem
        That's correct, you'll need to make use of colorpalette9 (SSC). See the helpfile for details.

        That being said, is there a way I can get only a map of Europe instead of using the entire world map? Like cropping it in Stata or something? I feel like much of the interpretation is left out here.
        Yes, you'll need to get a "better" shapefile. I am not very familiar with European-area shapefiles (I tried to search around), but I provided this example as a starting point. Unfortunately, Russia isn't well-behaved in this example, so keeping your list of countries will still look warped. I suggest trying to find a shapefile that works and adapt the code I provided.

        Hope this helps.

        Comment


        • #5
          Thank you Justin, for all your advice.

          Comment


          • #6
            Just a quick followup question:

            I have managed to successfully get a map of Europe and plot it and the colours make sense. However, I would like to exclude some islands and regions that are attached to the countries, but are of no interest to me.

            Code:
            shp2dta using `files', database(dbase) coordinates(coor) replace
            
            use coor.dta, clear
            
            geo2xy _Y _X, replace
            save "coor_google.dta", replace
            
            * remove unwanted
            use "/Users/Desktop/MAP.dta", clear
            drop if inlist(lower(varname), "usa", "indonesia", "morrocco", "brazil", "Japan")
            
            gen pct = vote * 100
            clonevar NAME = varname
            replace NAME = "United Kingdom" if NAME == "uk"
            replace NAME = "Czech Republic" if NAME == "czech"
            replace NAME = proper(NAME)
            
            merge 1:1 NAME using "dbase.dta",  assert(using matched) nogen
            
            colorpalette9 HSV heat, n(5) reverse nograph
            local colors `r(p)'
            
            spmap pct using coor_google.dta,                                                ///
            id(_ID) ocolor(BuRd) osize(vvthin vvthin ..)                               ///
            clmethod(custom) clbreaks(0 25 50 75 100) fcolor(`r(p)')  ///
            legend(pos(9) size(*0.8)) legstyle(2)                                 ///
            title("Expected Percentage of Votes in Governmental elections", size(small))
            Just the map looks like this:

            Click image for larger version

Name:	Graph1.jpg
Views:	1
Size:	125.5 KB
ID:	1577425


            I have tried taking out the coordinates that may exclude these lines usingt:

            Code:
            shp2dta using `files', database(dbase) coordinates(coor) replace
            
            use coor.dta, clear
            drop if _X < -25
            drop if _Y < 35
            drop if _Y > 90
            
            geo2xy _Y _X, replace
            save "coor_google.dta", replace
            But it looks like this:

            Click image for larger version

Name:	Graph2.jpg
Views:	1
Size:	199.4 KB
ID:	1577426



            Here's a sample of my coordinates data:

            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input byte _ID double(_X _Y)
            1          19.421177  40.52914000000001
            1 19.424836999999997  40.52871300000001
            1 19.432458999999994  40.51799399999999
            1 19.442835000000002 40.509308000000004
            1 19.439294999999987 40.506302000000005
            1 19.448398999999995  40.50556599999999
            1 19.456761999999998  40.49857299999999
            1  19.46252100000001          40.503117
            1 19.467228000000006          40.516312
            1          19.467985           40.52739
            1 19.471979000000005  40.53410299999999
            1 19.465362999999996 40.546501000000006
            1 19.469054999999997  40.56560099999999
            1  19.46506099999999  40.56876399999999
            1          19.457571  40.57099500000001
            1  19.44181599999999  40.57930400000001
            1           19.41091 40.583552999999995
            1 19.406331999999992  40.58161899999999
            1 19.403637000000003 40.569252000000006
            1 19.403410000000008  40.54901899999999
            1 19.398723999999987  40.54799700000001
            1  19.39715000000001          40.555927
            1          19.387766 40.571113999999994
            1  19.36803599999999  40.59452400000001
            1 19.355387000000007 40.621421999999995
            1 19.351878999999997 40.623012999999986
            1 19.331210999999996  40.64887999999999
            1 19.325655000000012  40.65238600000001
            1 19.321620999999993          40.655777
            1 19.328467999999987          40.666145
            1 19.333690999999988          40.670197
            1          19.343761          40.671604
            1  19.35067699999999  40.67416399999999
            1  19.36158599999999  40.68344099999999
            1 19.371850999999992 40.697959999999995
            1  19.37894399999999 40.711330000000004
            1          19.380764          40.718582
            1  19.38488000000001 40.743919000000005
            1 19.382439000000005  40.75638599999999
            1 19.382620000000003          40.786732
            1          19.385256  40.80208200000001
            1 19.388724999999994  40.80577500000001
            1 19.410635000000013  40.81421700000001
            1  19.41785999999999 40.819323999999995
            1  19.41996800000001 40.824295000000006
            1 19.418727999999987          40.836849
            1  19.42068499999999 40.838134999999994
            1          19.415819  40.84330700000001
            1 19.416357000000005  40.85368299999999
            1 19.418034000000006  40.85724999999999
            1 19.416686999999996  40.86083199999999
            1 19.418288999999987  40.86255299999999
            1 19.430790000000002 40.864124000000004
            1  19.42894000000001  40.87180699999999
            1 19.424487999999997          40.883442
            1 19.416858999999988  40.89140699999999
            1 19.402039000000002  40.89633599999999
            1  19.39362700000001  40.89344399999999
            1 19.393888000000004  40.90357599999999
            1 19.395143999999988  40.91055700000001
            1 19.398050000000012  40.91397900000001
            1 19.401834000000008          40.915165
            1          19.407297 40.912563000000006
            1  19.41051300000001 40.913478999999995
            1 19.412986999999987          40.915493
            1  19.42277100000001          40.917103
            1  19.42433700000001  40.92157700000001
            1 19.430372000000006 40.936477999999994
            1  19.43704199999999          40.941315
            1 19.444891000000013  40.94393199999999
            1 19.460924000000006  40.94643400000001
            1 19.462975999999998  40.94450800000001
            1  19.45916600000001 40.941024999999996
            1 19.453332999999986  40.93924000000001
            1 19.438661999999994  40.93798100000001
            1 19.435620999999998  40.93316999999999
            1  19.43568400000001  40.93018699999999
            1 19.440686999999997  40.92640299999999
            1 19.453068000000002 40.926581999999996
            1 19.457769000000013          40.922775
            1 19.457990999999993  40.91107199999999
            1 19.456225999999987 40.908187999999996
            1           19.44735          40.904121
            1 19.442365999999993  40.89297099999999
            1 19.441280000000006  40.88209499999999
            1  19.44322600000001 40.878555000000006
            1 19.446390000000008 40.877396000000005
            1 19.448941999999988  40.88125199999999
            1 19.446041000000008  40.88518500000001
            1  19.45463000000001          40.894058
            1          19.456818 40.890995000000004
            1 19.454469999999986  40.88301799999999
            1 19.455709000000013  40.87781899999999
            1  19.45962700000001 40.880393999999995
            1  19.46527499999999          40.888599
            1 19.473236000000014 40.887778999999995
            1 19.474616999999995          40.891323
            1 19.480940000000004  40.89153300000001
            1 19.482778999999994 40.886379000000005
            1  19.49493000000001          40.888374
            end

            Additionally I would like to ask if anyone knows any way of exporting a .dbf file from stata (that is, from .dta to .dbf). My goal is, aside from this, to create a cartogram, and programs like QGIS or Scapetoad need to have an "attribute vector" to the map in a .dbf file. Since I am using Stata 14.0, I think I cannot use -export dbase-, since I get the error:

            Code:
            export dbase 􏰀using􏰁 vote, clear 
            export: unknown subcommand "dbase"
            If anyone has any advice for any of this issues, I would appreciate it immensely.

            Comment

            Working...
            X