Announcement

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

  • Insetting graphs using Stata

    Hi all (and especially Nick Cox),

    I am wondering if there is a way to inset graphs within graphs to create a sort of "zoom-in" effect on a particular point. I currently have the following MWE:

    The data is from FRED and it is basically just Canadian economic series:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int period double(GDP C G I)
     366        85892750000            .         2.2811e+10         1.4491e+10
     456        88065250000            .        22919750000         1.4864e+10
     547         9.0187e+10            .         2.3821e+10        15365250000
     639        91446750000            .        23725750000        15383250000
     731        93870250000            .         2.3918e+10         1.5614e+10
     821         9.4561e+10            .        24696750000        15533250000
     912        95806250000            .        24446250000 16034749999.999998
    1004        97758250000            .         2.4957e+10         1.5905e+10
    1096        98351750000            .        25384250000          1.578e+10
    1186          9.979e+10            .        2.53765e+10 16120000000.000002
    1277       100566250000            .        2.59475e+10        1.63995e+10
    1369       103722750000            .        26095250000 17126999999.999998
    1461       106295750000            .         2.7482e+10        1.78885e+10
    1552       1.066875e+11            .        27019750000        18115750000
    1643       107799250000            .        26815750000        18402250000
    1735       108381250000            .        2.76315e+10        1.95945e+10
    1827        1.11793e+11            .         2.7987e+10        19617250000
    1917        1.12864e+11            .         2.8185e+10        19882250000
    2008        1.14401e+11            .        28511250000        20926750000
    2100       117214750000            .        29152250000        21604250000
    2192       1.194585e+11            .        29686250000        22294250000
    2282        1.21991e+11            .        3.04555e+10        22973750000
    2373       121961750000            .        3.12245e+10        22769250000
    2465       123453750000            .         3.2153e+10        23052250000
    2557       123182250000            . 32207250000.000004        2.31035e+10
    2647       125774250000            . 33601750000.000004        23011250000
    2738       126224250000            . 33881500000.000004        22689750000
    2830        1.26735e+11            .        3.41515e+10         2.2367e+10
    2922       127785750000            .        34425250000        22830750000
    3013       1.312585e+11            .        35217750000        22847750000
    3104 133053750000.00002            .         3.6076e+10        22861250000
    3196 134890999999.99998            .        3.67015e+10         2.3416e+10
    3288 136655499999.99998            .        3.64995e+10        23654750000
    3378 137329000000.00002            .        3.74045e+10        24045750000
    3469       1.388035e+11            .         3.7358e+10        24245750000
    3561       140790250000            .        38033750000        24420750000
    3653          1.417e+11            .         3.9442e+10        2.44595e+10
    3743       141694250000            .        4.08325e+10        23597750000
    3834       143157250000            .        39985750000        24229750000
    3926       1.434125e+11            .        40859750000        24620750000
    4018       142589750000            .         4.0112e+10        2.47925e+10
    4108       146725750000            .        4.14715e+10          2.582e+10
    4199       150805250000            .        42289250000        26421250000
    4291       152472750000            .        42737750000          2.691e+10
    4383         1.5194e+11            .        4.19375e+10        2.69805e+10
    4474         1.5624e+11            .        42541750000        27320250000
    4565       1.566755e+11            .        43530250000        2.71055e+10
    4657       160387750000            .        44186750000        26828250000
    4749        1.64576e+11            .        44112750000        28271750000
    4839        1.66361e+11            .         4.4647e+10        2.88945e+10
    4930       166963250000            .        4.48645e+10        2.94515e+10
    5022       1.700835e+11            .        46583250000        30497250000
    5114       1.713445e+11            .        46233750000        31682750000
    5204       1.732195e+11            .        47463250000        3.06155e+10
    5295       173067250000            .         4.8172e+10         3.0637e+10
    5387       1.722645e+11            .        4.87975e+10        3.04815e+10
    5479        1.71182e+11            .        49187750000        3.07655e+10
    5569       173406750000            .         5.0825e+10        3.16145e+10
    5660       176663250000            .        5.14135e+10        32867250000
    5752        1.78724e+11            .         5.1369e+10 33411749999.999996
    5844        1.81821e+11            .        51034750000 33758249999.999996
    5935       185755250000            .         5.1598e+10        34897250000
    6026       1.865825e+11            .        5.13165e+10         3.4022e+10
    6118        1.86967e+11            .         5.1872e+10 34224249999.999996
    6210       189625750000            .        52712750000        3.47325e+10
    6300       1.906445e+11            .        5.37345e+10        34993750000
    6391        1.91653e+11            .        5.33655e+10        35383750000
    6483       194683250000            .        53779250000        3.51235e+10
    6575       196320250000            .        5.35965e+10        3.49915e+10
    6665        1.97667e+11            .        5.39715e+10        3.58235e+10
    6756       199553750000            .         5.4084e+10         3.5793e+10
    6848       201303750000            .        5.40665e+10        36253250000
    6940       2.029785e+11            .         5.3917e+10        36678250000
    7030       205712750000            .        5.46705e+10         3.7033e+10
    7121       207136250000            .         5.4286e+10        38973250000
    7213        2.08515e+11            .        5.46415e+10        39173250000
    7305        2.09975e+11            .        54987250000        39663250000
    7396       209908250000            .         5.5702e+10        38681250000
    7487       209667250000            .         5.6438e+10         4.0302e+10
    7579        2.12561e+11            .        5.61815e+10        4.14375e+10
    7671       217245750000 109859750000        5.65215e+10        43427750000
    7761       2.196875e+11 1.097075e+11         5.6411e+10        44640750000
    7852       217715250000 1.090945e+11          5.710e+10        4.35725e+10
    7944       2.167185e+11 109304250000        57255750000        43120750000
    8036        2.14169e+11 107629750000         5.6774e+10        41716750000
    8126       211690750000 106755250000        57354250000        38884250000
    8217       209835250000 106358750000         5.7843e+10        37161750000
    8309       207899250000 105989250000        5.80075e+10         3.8039e+10
    8401        2.11377e+11 107460750000         5.7002e+10         3.7857e+10
    8491       2.155315e+11 108732250000        58061750000        38628750000
    8582        2.17942e+11   1.1001e+11        58672750000        3.90255e+10
    8674       2.206885e+11  1.11183e+11        58737750000        39307250000
    8766        2.24485e+11 112105750000        58881250000        3.93985e+10
    8857       228875750000 1.132755e+11        58904250000         3.9353e+10
    8948       229838250000 113408250000        5.95875e+10        3.96925e+10
    9040         2.3348e+11  1.15385e+11        60350250000          3.975e+10
    9132       2.370275e+11 116878250000        61260750000        4.06915e+10
    9222        2.37783e+11 1.179455e+11        6.17865e+10        4.26085e+10
    9313       240799750000  1.20177e+11         6.1306e+10        4.39535e+10
    9405       2.444955e+11 121429750000        62827750000         4.4354e+10
    end
    format %td period
    And the code I am currently using is
    Code:
    // Data Cleaning
    gen quarter = qofd(period)
    drop if quarter < yq(1981, 1)
    tsset quarter, quarterly
    
    foreach v in GDP C G I { //convert to billions for readability
        gen `v'_bil = `v'*1e-9
    }
    
    tsfilter hp cy*=GDP_bil C_bil G_bil I_bil, smooth(1600)
    
    // Graph 1
    twoway tsline cy1 cy2 cy3 cy4, legend(size(medsmall)) ytitle("Billions of real (2015) Canadian dollars") ///
        xtitle("quarter") lpattern(solid dash longdash solid)
        
    // Graph 2 to be inset
    keep if quarter >= yq(2018, 4)
    twoway tsline cy1 cy2 cy3 cy4, legend(size(medsmall)) ytitle("") ///
        xtitle("quarter") lpattern(solid dash longdash solid)
    This creates the first graph (please ignore the axis labels and legend, this isn't a final product):

    Click image for larger version

Name:	Graph1.png
Views:	1
Size:	100.5 KB
ID:	1594096

    And the graph that I want to inset in the bottom left of this graph:

    Click image for larger version

Name:	Graph2.png
Views:	1
Size:	71.7 KB
ID:	1594097

    I have been scouring for some kind of command that would allow me to do this but I can't seem to find one. I imagine it might require some sort of macros approach but I am not well-versed enough to devise the program myself. Any thoughts or comments that could lead me in the right direction? This has to have been done before using Stata.
    Last edited by Brennan McLachlan; 11 Feb 2021, 21:18. Reason: added tags

  • #2
    Hi, Brennan McLachlan ,

    Sorry you didn't get an immediate response on this. I like your idea of the "zoom-in" inset on your graph. Consulting help for "graph combine", you could combine the two graphs, and then use the offset options. Here is some code which achieves part of what you want.

    Code:
    clear
    sysuse auto 
    scatter mpg weight, name(big, replace)
    scatter mpg weight if weight > 3500, name(zoom, replace)  fysize(25)  fxsize(25)
    
    graph combine big zoom zoom, holes(3) imargin(0 0 0 0) name(all3, replace)
    
    _gm_edit .all3.plotregion1.graph3.dragable = 1
    _gm_edit .all3.plotregion1.graph3.DragBy 63.25133512999842 -30.98417329390391
    _gm_edit .all3.plotregion1.graph2.draw_view.setstyle, style(no)
    
    gr dis all3
    An alternative way to achieve your objective, which might be easier, is to use the excellent freeware program IrfanView to do what you want. In only a couple of seconds you can combine your two graphs and save it in an image format like PNG. Here's the PNG version I made.
    Click image for larger version

Name:	Insert_one_png_into_another.png
Views:	1
Size:	121.4 KB
ID:	1602119

    The instructions I followed are in the IrfanView FAQ here:
    https://www.irfanview.com/faq.htm#:~...o%20selection.

    IrfanView has an active user community here:
    https://irfanview-forum.de/

    Comment


    • #3
      Dear Brennan,

      Reading my and Matthew Tibbles paper in the latest issue of The Stata Journal on creating inset plots, Ben Jann put together the convenience command grinset streamlining the process of inserting an inset plot in an existing twoway graph, see for more about grinset here on the StataList.

      Using you example data from #1, I have used the example code to create a workable tsline graph for which a (sensible) inset plot can be created.
      This is a little different from the published example figures in #1 (that I cannot replicate using the data provided) but it does show the practical use of grinset.

      So here is my:
      Code:
      * Use data from #1
      ////////////////////
      format %td period
      
      gen quarter = qofd(period)
      tsset quarter, quarterly
      
      foreach v in GDP G I { //convert to billions for readability
          gen `v'_bil = `v'*1e-9
      }
      
      foreach v in C { //convert to billions for readability
          gen `v'_bil = `v'*1e-9
      }
      
      tsfilter hp cy*=GDP_bil G_bil I_bil C_bil, smooth(1600)
      drop cy1 cy2 cy3
      tsfilter hp cy*=GDP_bil G_bil I_bil, smooth(1600)
      ////////////////////
      
      * Using grinset
      tw tsline cy1 cy2 cy3 cy4, legend(size(medsmall)) ytit("Billions of real (2015) Canadian dollars") ///
          xtit("quarter") lp(solid dash longdash solid) legend(pos(b) c(2) bm(b-3) size(*.9) span) name(g1, replace)
      
      grinset b=26 l=18, size(25 35) scale(0.8) : twoway tsline cy1 cy2 cy3 cy4 if quarter >= yq(1983, 4), xtit("") lp(solid dash longdash solid) legend(off)
      Which results in:
      Click image for larger version

Name:	grinset_Example_1.png
Views:	1
Size:	105.4 KB
ID:	1712800


      I suppose this is what you were looking for when you wrote in #1 that you 'want to inset in the bottom left of [the] graph' in a flexible way using Stata that can be replicated by any user.


      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
      Last edited by ericmelse; 08 May 2023, 14:22.
      http://publicationslist.org/eric.melse

      Comment

      Working...
      X