Announcement

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

  • Overlapping map

    Hi,

    I have the first map and I want to outline with with a different color the municipalities in the center (identified with the variable coastal). When I try to do that I get the second map. I am using the following code:

    spmap hom1 using "coords.dta", id(id) fcolor("`colors'") ocolor(white ..) ///
    osize(0.05 ..) clmethod(custom) clbreaks(0 1 5 15 50 100 200 400) ///
    legend(pos(5) size(1.8) region(fcolor(gs15)))

    preserve

    keep if coastal==2

    rename id _ID
    keep _ID

    merge 1:m _ID using "coords.dta"

    keep if _merge==3

    save coords2.dta, replace

    restore

    spmap hom1 using "coords.dta", id(id) fcolor("`colors'") ocolor(white ..) ///
    osize(0.05 ..) clmethod(custom) clbreaks(0 1 5 15 50 100 200 400) ///
    legend(pos(5) size(1.8) region(fcolor(gs15))) ///
    polygon(data("coords2.dta") ocolor(maroon) osize(.1))


    Click image for larger version

Name:	mapa2018a2019.png
Views:	1
Size:	341.4 KB
ID:	1765105
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	476.5 KB
ID:	1765106

    Thanks,

    Tessa

  • #2
    The issue is your coordinates file. The sort order is getting jumbled after the merge. I suggest you use spshape2dta to create the coordinates file and sort on _ID and shape_order. Note: spmap is from SSC. Here's a worked example with a random selection that shows the issue and solution.

    Code:
    copy "https://www2.census.gov/geo/tiger/TIGER2024/COUSUB/tl_2024_06_cousub.zip" ., replace
    unzipfile tl_2024_06_cousub.zip
    spshape2dta tl_2024_06_cousub, replace
    
    use tl_2024_06_cousub in 1/50, clear
    merge 1:m _ID using tl_2024_06_cousub_shp, keep(matched) nogen  
    save selected.dta, replace
    
    use tl_2024_06_cousub, clear
    spmap using tl_2024_06_cousub_shp.dta, id(_ID) polygon(data(selected.dta) ocolor(red)) title("Not wanted") name(not_sort, replace)
    
    use tl_2024_06_cousub in 1/50, clear
    merge 1:m _ID using tl_2024_06_cousub_shp, keep(matched) nogen
    sort _ID shape_order 
    save selected.dta, replace
    
    use tl_2024_06_cousub, clear
    spmap using tl_2024_06_cousub_shp.dta, id(_ID) polygon(data(selected.dta) ocolor(red)) title("Wanted") name(IS_sorted, replace)
    gr combine IS_sorted not_sort
    Result:
    Click image for larger version

Name:	exmaple.png
Views:	1
Size:	821.9 KB
ID:	1765164

    Last edited by Justin Niakamal; 06 Oct 2024, 13:57.

    Comment


    • #3
      Thanks Justin, when I do that i get the following map. Is not quite what I need because I just want the municipalities to be overlined and here they are colored.

      I just added after the merge the following:

      sort _ID _X _Y

      Those are the variables in my coordinates file.



      Click image for larger version

Name:	Graph.png
Views:	1
Size:	467.1 KB
ID:	1765220


      Thanks.
      Last edited by Tessa May; 07 Oct 2024, 11:21.

      Comment


      • #4
        sort _ID _X _Y
        That will not solve the issue. I suggested you use spshape2dta because it has a shape_order variable, but I suspect you're using shp2dta (SSC). You need to add a sort variable to your coordinates file. Your code above has the coordinates sorted within each polygon _ID.

        Code:
        use coords.dta, clear
        gen `c(obs_t)'  id_sort = _n
        save coords_sort_id.dta, replace 
        
        preserve
        
        keep if coastal==2
        
        rename id _ID
        keep _ID
        
        merge 1:m _ID using "coords_sort_id.dta"
        keep if _merge==3
        sort  id_sort
        
        save coords2.dta, replace
        
        restore

        Comment


        • #5
          You are right! I decided to use spshape2dta and just adding the sort _ID shape_order worked perfectly.

          Thanks,

          Tessa

          Comment

          Working...
          X