Announcement

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

  • grinset command to create inset plots available from SSC

    I always thought that there was no easy way to place a small graph on top of another graph in Stata, but there is, using graph combine and some _gm_edit commands, as has been shown by Matthew Tibbles and Eric Melse in the latest issue of the Stata Journal (Tibbles and Melse 2023). I now put together a convenience command streamlining the process. Type

    Code:
    . ssc install grinset, replace
    to obtain the command. See https://github.com/benjann/grinset/ for an example.

    ben

    Tibbles, Matthew, Eric Melse. 2023. A note on creating inset plots using graph twoway. The Stata Journal 23(1): 265-275. https://doi.org/10.1177/1536867X231162022


  • #2
    For those visitors interested in another example of using grinset, note that this is provided under #3 in an earlier post from Brennan McLachlan requesting for a possible solution to create an inset plot in an existing tsline graph.
    It is really great that Ben so quickly contributed his grinset convenience command to the user community and both Matthew and I thank him for that.
    http://publicationslist.org/eric.melse

    Comment


    • #3
      Ben Jann is it possible to make grinset compatible with spmap (SSC)? Ideally something along the lines of

      Code:
      clear all 
      
      copy "http://fmwww.bc.edu/repec/bocode/i/Italy-RegionsCoordinates.dta" ., replace 
      use "http://fmwww.bc.edu/repec/bocode/i/Italy-RegionsData.dta", clear 
      
      spmap using "Italy-RegionsCoordinates.dta", id(id)
      grinset r=5: spmap using "Italy-RegionsCoordinates.dta" if id == 1, id(id)
      
      
      exit
      Right now this throws an error back.

      Comment


      • #4
        It seems that spmap does not support some of the specific graph options that grinset needs to submit to the graph command, so the only way to make grinset compatible with spmap would be to change the code of spmap. However, I just released a new geoplot command that works with grinset. Example:

        Code:
        ssc install geoplot
        ssc install palettes, replace // if not already installed
        ssc install colrspace, replace // if not already installed
        ssc install moremata, replace // if not already installed
        local url http://fmwww.bc.edu/repec/bocode/i/
        geoframe create regions  `url'Italy-RegionsData.dta, id(id) coord(xcoord ycoord) ///
                         shpfile(`url'Italy-RegionsCoordinates.dta)
        geoplot (area regions)
        grinset r=5: geoplot (area regions if id==1)
        But maybe geoplot is flexible enough so that, depending on application, grinset is not really needed. Continuing the example:

        Code:
        // select region 1 and put it into new frame
        geoframe select if id==1, into(region1)
        // shift and rescale
        local scale 1.5
        local xshift 700000
        local yshift -50000
        local CX = xcoord[1]
        local CY = ycoord[1]
        // - shift centroid
        replace xcoord = xcoord + `xshift'
        replace ycoord = ycoord + `yshift'
        // - shift shape coordinates
        frame region1_shp {
            replace _X = (_X-`CX')*`scale' + `CX' + `xshift'
            replace _Y = (_Y-`CY')*`scale' + `CY' + `yshift'
        }
        geoplot (area regions) (area region1, color(Coral) lc(gray)) ///
            (label region1 region, color(black)) , tight
        Maybe I should add some options to geoplot such that such moving around and resizing parts of a map can be done on the fly...

        ben

        Comment


        • #5
          The newest geoplot update now has support for such zooms. Examples:

          Code:
          local url http://fmwww.bc.edu/repec/bocode/i/
          local url shp/it/
          geoframe create regions  `url'Italy-RegionsData.dta, id(id) coord(xcoord ycoord) ///
                           shpfile(`url'Italy-RegionsCoordinates.dta)
          
          geoplot (area regions)                                           /// 1
              (area regions         if id==1, fc(Coral*.5) lc(gray))       /// 2
              (label regions region if id==1, color(black))                /// 3
              (area regions         if id==1, fc(Coral) lc(gray))          /// 4
              (pie regions relig1 relig2 relig3 if id==1, lab(, reverse))  /// 5
              (area regions         if id==15, fc(SkyBlue*.5) lc(gray))    /// 6
              (label regions region if id==15, color(black))               /// 7
              (area regions         if id==15, fc(SkyBlue) lc(gray))       /// 8
              (pie regions relig1 relig2 relig3 if id==15, offset(45 45))  /// 9
              , tight legend(pos(sw) rowgap(1)) ///
                zoom(4/5: 5 100 210, box    pad(5) connect(lp(dash))) ///
                zoom(8/9: 5 180 255, circle pad(5) connect(lp(dash)))
          Click image for larger version

Name:	Graph.png
Views:	1
Size:	107.0 KB
ID:	1719245


          The circles and frames added by zoom() are on top of the graph, so adding fill would cover the other objects. If you want fill, you can use geoframe bbox to create bounding boxes etc. and then add them to are regular layers, about as follows:

          Code:
          geoframe bbox bbox, by(id) circle pad(5)
          frame bbox: rename _ID id
          geoplot (area regions)                                           /// 1
              (area regions         if id==1, fc(Coral*.5) lc(gray))       /// 2
              (label regions region if id==1, color(black))                /// 3
              (area bbox            if id==1, fc(gs14) lc(gray))           /// 3
              (area regions         if id==1, fc(Coral) lc(gray))          /// 4
              (pie regions relig1 relig2 relig3 if id==1, lab(, reverse))  /// 5
              , tight legend(pos(se) rowgap(1)) zoom(4/6: 6 90 210)
          Click image for larger version

Name:	Graph2.png
Views:	1
Size:	126.2 KB
ID:	1719246


          ben

          Comment


          • #6
            (Sorry, delete the second local url ... command in the example.)

            Comment

            Working...
            X