Announcement

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

  • Plotting only a part of a map (such as a plotregion, or defined by x-y) using -spmap- or the graph editor

    Hello everyone.

    This time I am involved in producing maps through spmap. I've had no major problems so far, but now I can't understand how to ask STATA to show in the graph only a portion of the map. Let me be clearer: given a map of Europe, I would like to have plotted only a particular area, such as the Alps, which has been defined by a single line running around the alpine region. Looking into the graph editor, such line creates a plotregion of its own, which would be perfect, but STATA will not allow me to have on the graph only that particular plotregion, representing instead the whole of Europe. Of course, I could also use X and Y coordinates to select the region I am interested in, but here arises another issue: actually, I have asked stata to "select" only a certain region within the suboptions of "line", and STATA managed to produce a map of the selected area, the Alpine one in my case, but all other lines defining regional borders were in a mess, lacking the original order, having made a selection on the region to be plotted.

    Thus, I would like STATA to show me only a certain part of the map, but without re-computing lines all around, which produces a huge mess in my graph. Thanks to anyone who is reading, I hope we can find a solution here. I can attach images if necessary.

  • #2
    Can you just identify the polygons that make up the area you want to focus on and use an if expression

    Code:
    spmap using basemap.dta if alps == 1 ,  id(id)

    Comment


    • #3
      Originally posted by Scott Merryman View Post
      Can you just identify the polygons that make up the area you want to focus on and use an if expression

      Code:
      spmap using basemap.dta if alps == 1 , id(id)
      Thanks for your kind reply. Actually, I did not make clear that I have only the alpine regions on the basemap.dta (as you called it), while I have the whole of Europe on the coordinates.dta. However, I have just tried using the if option, and the only result is that spmap will not produce information (colors on regions) for unselected regions, while reproducing the whole of Europe on map as usual.

      Comment


      • #4
        Hello everyone.

        I am pleased to say I have the solution to this problem. I have tried for long, with no success, dropping _X and _Y coordinates which were out of the region I wanted to plot on the map. However, I was making a mistake: I was also dropping missing _X and _Y coordinates which were in the area to be plotted, not allowing STATA to represent correctly regions with more than a single portion of territory (such as any region with a part of detached territory from the mainland, for example the Veneto region with the numerous islands of the bay). Now the command works perfectly: the solution is to allow another if condition where missing _X and _Y values are kept if in the target area to be plotted.

        Comment


        • #5

          I am glad you found a solution.

          Here is another way that might be of interest to other people. Today I needed to focus on a particular area in a map. I saved the map as a SVG file, opened it in Chrome, zoomed in on the area of interest and then used the windows snip-tool to clip the relevant area. Below is the changing intensity of hurricane Beulah (1967) as it entered southern Texas.


          Click image for larger version

Name:	map1.PNG
Views:	1
Size:	72.8 KB
ID:	1484904

          Comment


          • #6
            Nice map! Even though I'd have no idea on how to make that...

            Anyway, I wanted to say that the solution previously found is imperfect: in the case you had borders like in the USA, that would not work nicely, because when you select _X and _Y coordinates you just ask STATA to keep values within a certain area, but of course these values might be far away from actual borders of the selected area, resulting in missing borders of great lenght. As a matter of fact, you have many _X and _Y coordinates in Europe because borders and non-linear or linear for very short lenghts, so in almost any case you will have _X and _Y coordinates close to the borders of the selected area, so maps will just need to be cut a bit and will look nice anyway. As I was saying, this would not work for the USA, where there are very long linear borders.

            Comment


            • #7
              Originally posted by Andrea Berni View Post
              Hello everyone.

              I am pleased to say I have the solution to this problem. I have tried for long, with no success, dropping _X and _Y coordinates which were out of the region I wanted to plot on the map. However, I was making a mistake: I was also dropping missing _X and _Y coordinates which were in the area to be plotted, not allowing STATA to represent correctly regions with more than a single portion of territory (such as any region with a part of detached territory from the mainland, for example the Veneto region with the numerous islands of the bay). Now the command works perfectly: the solution is to allow another if condition where missing _X and _Y values are kept if in the target area to be plotted.
              Hi Andrea,

              I have a similar issue. I want to plot point data on a map using the country shapefile. My goal is to select certain regions within the country. But the map produced marks the region I selected but also plots the data for the entire country. I wonder if you know a way to deal with this.

              Here is my code:

              Code:
              use "C:\Users\asmobari\Box Sync\Projects\Geolocations\District Shape Files\district_data", clear 
                  spmap using "C:\Users\asmobari\Box Sync\Projects\Geolocations\District Shape Files\district_coordinates" if dist_id == 32, id(dist_id) ///
                  fcolor(eggshell) ocolor(dkgreen) osize(thin) ///
                  point(data("C:\Users\asmobari\Box Sync\Projects\Prizoner Release\Stata Files\Dta Files\AFG_Conflict_with_District_shp") x(_X) y(_Y) ///
                  size(*0.6) fcolor(sienna) ocolor(white) ///
                  osize(vvthin))
              and here is what I am getting:

              Click image for larger version

Name:	Graph.png
Views:	1
Size:	210.7 KB
ID:	1583381

              Comment


              • #8
                Originally posted by Andrea Berni View Post
                Hello everyone.

                I am pleased to say I have the solution to this problem. I have tried for long, with no success, dropping _X and _Y coordinates which were out of the region I wanted to plot on the map. However, I was making a mistake: I was also dropping missing _X and _Y coordinates which were in the area to be plotted, not allowing STATA to represent correctly regions with more than a single portion of territory (such as any region with a part of detached territory from the mainland, for example the Veneto region with the numerous islands of the bay). Now the command works perfectly: the solution is to allow another if condition where missing _X and _Y values are kept if in the target area to be plotted.
                Hi Andrea,
                I am facing a similar problem that you mentioned earlier in this thread(#1) and for which you found a solution. I am using spmap with the point option, and while many coordinates in the dataset containing the points lie inside the boundary of the map, there are quite a few points which lie outside the map boundary. I am trying to find a way to remove the points that lie outside the map boundary. Would really appreciate a solution to this issue. Andrea mentions in his solution post that he allows for another if condition where missing _X and _Y values are kept if they are not in the target area being plotted - but can you help me with this particular step? Thank you so much for your time.

                So far this is the code I have -
                Code:
                use ${User}/district_names, clear
                levelsof District, local(levels)
                foreach l of local levels {
                        use ${shapefiles}/`l'_db, clear
                         spmap ratio_st using ${shapefiles}/`l'_shape, id(newid) fcolor(Greys) osize(vthin) mocolor(gs6) legenda(on) legtitle(Proportion of ST in population) title("Map for district `l'") point(data(${User}/gps_schools) select(keep if District == "`l'") by(school) xcoord(Lon_) ycoord(Lat_) size(vtiny vtiny) fcolor(blue red) legenda(on) legtitle(RTE school) legtitle(RTE school))        
                         graph export ${graphs}/District_`l'.png, replace }
                And this is what it produces -

                Click image for larger version

Name:	THANE.png
Views:	1
Size:	273.4 KB
ID:	1668790
                Last edited by Monica Agarwal; 11 Jun 2022, 09:36.

                Comment

                Working...
                X