Announcement

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

  • 3 plots gathered in 1 subplot

    Hi,

    I am struggling to gather 3 plots in 1 unique figure with 3 subplots. I want the subplots to depicts different time periods of the same time series data. I'm also struggling to get all lines to be black (one plain and the other dashed) instead of different colors.

    I'd like the 2 time periods to be the following:
    - entire sample
    - ym between [200 ; 250]
    - ym between [225 ; 275]

    For now, I run 3 time the code using keep to shrink the sample in the second and third plot as follow:
    Code:
    keep if ym>199 & ym<251
    Here is the code I run 3 time with different intervals:

    Code:
    tsset ym
    keep if ym>199 & ym<251
    line TRACAR ORCAR ym

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double(ym TRACAR ORCAR)
    192 -.0033350477000837916 -.0003683217694234371
    193   -.07045587821963419   -.06521660208867314
    194   -.09191838589276918   -.08673469200194306
    195   -.04931443098354621   -.03936599344240034
    196   -.02715327012173765  -.020824142504374435
    197  -.016842488342676973  -.008047950782653579
    198  -.020084465205837943   -.01389383201526732
    199  -.016233098943496338  -.021203868911942744
    200 -.0035931515370219123   -.01828708093517132
    201    .03571690334516507   .012819022862868242
    202    .04455477981820734   .011654163595252115
    203    .08485150376307166    .04743667762435774
    204    .09984026051056252    .05645245801445384
    205    .10889882174362192    .06250662555288229
    206    .09408333428886506   .060906407025724876
    207    .09724075886217153    .06934158203561133
    208    .10286942199289811    .08089841744867378
    209    .10209567085254202    .08651249161073829
    210    .05754845029260604    .04469158442050881
    211    .06638657070537657   .061282307727525034
    212    .05460988250674513    .06586239601108772
    213    .04152441848217898     .0688371759228358
    214     .0438214294083841    .07872040889776405
    215     .0674871893713507     .0977159595529987
    216    .06490711716773598     .1174814795718476
    217    .11062022619837128    .14999485112787375
    218     .1359110146145715    .15769957980524812
    219    .14543813812705447    .17252988410191014
    220     .1483673419547573    .17973978385770514
    221    .12099210505219944     .1740258272730565
    222    .11255135129305215     .1735532944220967
    223    .11492398701724553     .1910387372361875
    224    .13497259566435454    .20138277593959808
    225    .17795803505786037    .18837835235829675
    226    .17746495255437128    .20494295565427204
    227     .2094319405926391    .22772673635365004
    228    .21822306291597904    .23514896897397897
    229     .2548406367727308      .270475823730781
    230    .28753585103992557     .3007595520490594
    231    .28952136377471094    .30012048751904274
    232      .305900475302077     .3142628384852584
    233     .3219734184008402    .34054770817982427
    234    .33806551259525347    .35550978509352427
    235      .320176627847541     .3332741998516111
    236     .3483171980612264     .3363191690086434
    237     .3577985962232909    .35979190217196916
    238     .3542809630115738    .35915773299828113
    239     .3664674395147439     .3783511937582171
    240     .3949898833795509    .41322580466915254
    241    .39505513964152045     .3801585607353542
    242     .3744761128443337    .38148197260561406
    243     .3980570629159491     .4036312151654504
    244     .4000157484726982     .4023994988784179
    245     .4204809129467824    .41989629712318544
    246     .4343275682730585     .4300761287219969
    247     .4465203954056665    .43859389406875576
    248     .4544472776790576     .4560526997400558
    249     .4513158318481973      .457042252241649
    250    .43428545338014796     .4529098012271615
    251    .48364748671069896    .46851259940686074
    252     .4891888644665549     .4853766770303783
    253     .4806621036967232     .4802052187174724
    254     .4909976207538399      .479991249739353
    255     .4952901764776578    .48434629924774075
    256      .502796695777313     .5110708866134684
    257     .5287474544557784     .5338075577619873
    258     .5260108421569143     .5316805781027812
    259     .5501899064632975     .6093968954636813
    260     .5176588528502183      .611569189925044
    261    .48820828761561114      .574271605376266
    262     .5053972122554777      .586187456312362
    263     .5194708731016324     .5973020607573984
    264    .49116969542143574     .5674053378361019
    265     .5144010381475255     .5738188278093684
    266     .5333427336933957     .5783666876254019
    267     .5649628554518026     .6038820318286316
    268     .5459778820914143     .5800562257899741
    269      .573668638434915     .5903994846398564
    270     .5652895376727657     .5883542205884337
    271     .5831608234973406     .6015414719774771
    272     .5902806810861686     .5984882409678033
    273     .5774547977520452     .5985138514979731
    274     .5902279186509635      .616901284979493
    275     .5996878360196861      .625669798245332
    276     .6134235367969734     .6366466304045999
    277     .5963959663981883     .6160419151528446
    278     .5965088885298631      .610828784838504
    279     .6055140731272942     .6134342824420707
    280     .6002134387534613     .6129732327585857
    281     .6157646298670424      .612041277594986
    282     .6239345752621089     .6154118313105509
    283     .6183391080259679     .6179129335445327
    284     .6256579297267545     .6197836663936542
    285     .6265881542303138     .6102512514291559
    286     .6238829846640681     .6103439922528602
    287     .6366666986929777     .5979682539767863
    288     .6345950622403422     .6190820881308037
    289     .6404840963355422     .6103495384995195
    290     .6601283352735469       .60406624306497
    291      .667003569166827     .6142215186413874
    end
    format %tm ym

    Thank you in advance.

  • #2
    Hi,

    I ended up using the package grc1leg, which works exactly like "gr combine" (it does not display the legend 3 times, that's the only difference as far as I understand, syntax should be the same). The vertical size of each graph becomes too small when I combine the 3 graphs. I would like to shrink margins and text to the max in order to maximize the vertical size of each graph. Is there anyone who can help me with that? Thanks.


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double(ym TRACAR ORCAR)
    192 -.0033350477000837916 -.0003683217694234371
    193   -.07045587821963419   -.06521660208867314
    194   -.09191838589276918   -.08673469200194306
    195   -.04931443098354621   -.03936599344240034
    196   -.02715327012173765  -.020824142504374435
    197  -.016842488342676973  -.008047950782653579
    198  -.020084465205837943   -.01389383201526732
    199  -.016233098943496338  -.021203868911942744
    200 -.0035931515370219123   -.01828708093517132
    201    .03571690334516507   .012819022862868242
    202    .04455477981820734   .011654163595252115
    203    .08485150376307166    .04743667762435774
    204    .09984026051056252    .05645245801445384
    205    .10889882174362192    .06250662555288229
    206    .09408333428886506   .060906407025724876
    207    .09724075886217153    .06934158203561133
    208    .10286942199289811    .08089841744867378
    209    .10209567085254202    .08651249161073829
    210    .05754845029260604    .04469158442050881
    211    .06638657070537657   .061282307727525034
    212    .05460988250674513    .06586239601108772
    213    .04152441848217898     .0688371759228358
    214     .0438214294083841    .07872040889776405
    215     .0674871893713507     .0977159595529987
    216    .06490711716773598     .1174814795718476
    217    .11062022619837128    .14999485112787375
    218     .1359110146145715    .15769957980524812
    219    .14543813812705447    .17252988410191014
    220     .1483673419547573    .17973978385770514
    221    .12099210505219944     .1740258272730565
    222    .11255135129305215     .1735532944220967
    223    .11492398701724553     .1910387372361875
    224    .13497259566435454    .20138277593959808
    225    .17795803505786037    .18837835235829675
    226    .17746495255437128    .20494295565427204
    227     .2094319405926391    .22772673635365004
    228    .21822306291597904    .23514896897397897
    229     .2548406367727308      .270475823730781
    230    .28753585103992557     .3007595520490594
    231    .28952136377471094    .30012048751904274
    232      .305900475302077     .3142628384852584
    233     .3219734184008402    .34054770817982427
    234    .33806551259525347    .35550978509352427
    235      .320176627847541     .3332741998516111
    236     .3483171980612264     .3363191690086434
    237     .3577985962232909    .35979190217196916
    238     .3542809630115738    .35915773299828113
    239     .3664674395147439     .3783511937582171
    240     .3949898833795509    .41322580466915254
    241    .39505513964152045     .3801585607353542
    242     .3744761128443337    .38148197260561406
    243     .3980570629159491     .4036312151654504
    244     .4000157484726982     .4023994988784179
    245     .4204809129467824    .41989629712318544
    246     .4343275682730585     .4300761287219969
    247     .4465203954056665    .43859389406875576
    248     .4544472776790576     .4560526997400558
    249     .4513158318481973      .457042252241649
    250    .43428545338014796     .4529098012271615
    251    .48364748671069896    .46851259940686074
    252     .4891888644665549     .4853766770303783
    253     .4806621036967232     .4802052187174724
    254     .4909976207538399      .479991249739353
    255     .4952901764776578    .48434629924774075
    256      .502796695777313     .5110708866134684
    257     .5287474544557784     .5338075577619873
    258     .5260108421569143     .5316805781027812
    259     .5501899064632975     .6093968954636813
    260     .5176588528502183      .611569189925044
    261    .48820828761561114      .574271605376266
    262     .5053972122554777      .586187456312362
    263     .5194708731016324     .5973020607573984
    264    .49116969542143574     .5674053378361019
    265     .5144010381475255     .5738188278093684
    266     .5333427336933957     .5783666876254019
    267     .5649628554518026     .6038820318286316
    268     .5459778820914143     .5800562257899741
    269      .573668638434915     .5903994846398564
    270     .5652895376727657     .5883542205884337
    271     .5831608234973406     .6015414719774771
    272     .5902806810861686     .5984882409678033
    273     .5774547977520452     .5985138514979731
    274     .5902279186509635      .616901284979493
    275     .5996878360196861      .625669798245332
    276     .6134235367969734     .6366466304045999
    277     .5963959663981883     .6160419151528446
    278     .5965088885298631      .610828784838504
    279     .6055140731272942     .6134342824420707
    280     .6002134387534613     .6129732327585857
    281     .6157646298670424      .612041277594986
    282     .6239345752621089     .6154118313105509
    283     .6183391080259679     .6179129335445327
    284     .6256579297267545     .6197836663936542
    285     .6265881542303138     .6102512514291559
    286     .6238829846640681     .6103439922528602
    287     .6366666986929777     .5979682539767863
    288     .6345950622403422     .6190820881308037
    289     .6404840963355422     .6103495384995195
    290     .6601283352735469       .60406624306497
    291      .667003569166827     .6142215186413874
    end
    format %tm ym
    
    preserve
    
    line TRACAR ORCAR ym, saving(full, replace) xtitle("")
    restore
    
    preserve
    keep if ym>199 & ym<251
    line TRACAR ORCAR ym, lpattern(dash dash) saving(dotcom, replace) xtitle("")
    restore
    
    preserve
    keep if ym>224 & ym<276
    line TRACAR ORCAR ym, lpattern(dash dash) saving(GFC, replace) xtitle("")
    restore
    
    
    grc1leg full.gph dotcom.gph GFC.gph, cols(1) ///
    imargin(0 0 0) legendfrom(GFC.gph)

    Comment


    • #3
      grc1leg is from net describe grc1leg, from(http://www.stata.com/users/vwiggins) as you are asked to explain (FAQ Advice #12). I didn't use it here.


      With the data example (thanks!) I did this. For the main device, see https://www.stata-journal.com/articl...article=gr0058 Wanting two graphs dashed and one solid seems not to help anything much, but it can be done.

      Code:
      expand 2 if inrange(ym, 200, 250), gen(toexpand1)
      expand 2 if toexpand1 == 0 & inrange(ym, 225, 275), gen(toexpand2)
      
      gen which = cond(toexpand2 == 1, 2, toexpand1)
      
      clonevar TRACAR2 =  TRACAR if which 
      clonevar ORCAR2 = ORCAR if which  
      replace TRACAR = . if which 
      replace ORCAR = . if which 
      set scheme s1color 
      line TRACAR ORCAR TRACAR2 ORCAR2 ym  , lp(solid solid dash dash) lc(red blue red blue) yla(, ang(h)) by(which,note("") col(1) xrescale) xtitle("") legend(order(1 2)) subtitle("", nobexpand fcolor(none) pos(9))
      Click image for larger version

Name:	durant.png
Views:	1
Size:	36.1 KB
ID:	1610932

      Comment


      • #4
        Hi Nick,

        Somehow, I'm not getting the output you are displaying in the previous post when I run the code. Only the last graph shows up and the 3 graphs do not get combined. Any idea why?

        Comment


        • #5
          I ran my code on your data example. I stopped at your format statement. If you want an explanation, see what you did differently.

          As said, I am not using your approach of producing 3 graphs and combining them.

          Comment


          • #6
            I got it now. Sorry for the confusion Nick.

            This is exactly what I have asked but somehow the top graph appears too small for the paper I'm writing.

            How can I rewrite the code in a way that the top graph takes half of the entire vertical space and the 2 other graphs appear next to each other on the bottom half, next to each other? I'd like to suppress all margins possible and make the legend smaller and/or appear somewhere it does not take as much vertical space. Can you help me with that? Thanks, again, your help is really appreciated.

            Comment


            • #7
              For what you want you need graph combine or possibly grc1leg or grc1leg2 as from
              Code:
              net describe grc1leg2, from(http://digital.cgdev.org/doc/stata/MO/Misc)


              The approach in #3 can't help here.

              Comment


              • #8
                Would you be able to help me write the code with what you find the best fit for my purpose? I have been struggling get the output mentioned in #6. Thanks a lot in advance!

                Comment


                • #9
                  Sorry, no: the task doesn't appeal -- if you want to show extra detail, you should not also want to show it in a cramped space. Furthermore. I don't use either of the extra commands mentioned.
                  Last edited by Nick Cox; 23 May 2021, 02:41.

                  Comment


                  • #10
                    Nick,

                    I have tried to look at graph combine grclleg and grclleg2. The closest I can find is something like this but it does not position the graphs the way I want:
                    Code:
                    sysuse auto, clear
                            gen byte qual = 1*(rep78<3)+2*(rep78==3)+3*(rep78>=4)
                                lab def qual 1 "Low Quality" 2 "Medium" 3 "High Quality"
                                lab value qual qual
                                lab var qual "Quality - Mapping of rep78 into trichotomy"
                            tab rep78 qual
                    
                    
                                grc1leg2 panel1 panel2 panel3,  ///
                                    xcommon ycommon ring(0) pos(5) legendfrom(panel1)    ///
                                    lxoffset(-10) lyoffset(17)  ///
                                    title("Three panels, with legend in a hole")  ///
                                    subtitle("Use -grc1leg2- with options -ring(0) pos(5)- and" /// 
                                            "fine-tuned with legend offset options")  ///
                                    name(grc13_offset, replace)
                    I would like to achieve the following subplot organization:
                    Click image for larger version

Name:	image_22769.png
Views:	1
Size:	92.2 KB
ID:	1611457
                    Obtained as follow:
                    Code:
                     
                     recode ym (200/250 = 1) (225/275 = 2) (else = .),gen(test) line TRACAR ORCAR ym, saving(full, replace) xtitle("") legend(off) plotregion(margin(zero)) line TRACAR ORCAR ym, by(test, note("") ) lpattern(dash dash)saving(dotcom, replace) xtitle("") subtitle("") plotregion(margin(zero)) legend(pos(6) row(1)) plotregion(margin(zero))  gr combine full.gph dotcom.gph,col(1) imargin(0 0) iscale(*0.8)



                    The problem with the graph above is that I cannot fix the x-axis of the 2 bottom graph so that they depict only the period I want for each of them. Please help.

                    Comment


                    • #11
                      Please read #9 again. I don't use grc1leg or grc1leg2 and I don't sympathise with your goal. Sorry if that seems unhelpful but I feel no obligation to make further suggestions here.

                      Comment


                      • #12
                        Combine, and combine again..

                        Code:
                        line TRA OR ym, xti("") fysize(60) name(full, replace)
                        
                        line TRA OR ym if ym>199 & ym<251, lp(- -) xti("") name(dotcom, replace)
                        line TRA OR ym if ym>224 & ym<276, lp(- -) xti("") name(GFC, replace)
                        
                        graph combine dotcom GFC, imargin(zero) iscale(*1.33) name(subs, replace)
                        grc1leg2 full subs, imargin(zero) cols(1) pos(3) ring(0) name(Combine, replace)
                        Last edited by Romalpa Akzo; 02 Jun 2021, 22:33.

                        Comment


                        • #13
                          The output.
                          Click image for larger version

Name:	Combine.png
Views:	1
Size:	78.5 KB
ID:	1612846

                          Comment


                          • #14
                            Thank you so much Romalpa, this is very helpful!

                            Comment


                            • #15
                              Originally posted by Romalpa Akzo View Post
                              The output.
                              [ATTACH=CONFIG]n1612846[/ATTACH]
                              Dear @Romalpa Akzo,
                              Which kind of scheme you use in this graph?


                              Best regards.

                              Raymond Zhang
                              Stata 17.0,MP

                              Comment

                              Working...
                              X