Announcement

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

  • Legend positioning issue

    I hope this is not a repeat question. I'm working on combining two graph panels side by side that will use the same legend. If I position the legend inside one of the panels, the panels are not distorted, but the legend is not (to my mind) positioned intuitively. I would like to position the legend outside the right-hand panel, but when I do so, the right-hand panel is narrowed, making it hard to read and mismatched with the left-hand panel. I'd be grateful for suggestions on how to position the graph outside the right-hand panel and have the panels displayed with identical widths as they are when I place the legend inside. As a secondary bonus question, I'd be grateful for suggestions on how to smooth the curve in the left-hand panel. Lowess doesn't quite do it (perhaps needs more data points?).
    Thanks! -Doug

    clear

    /***********Sets up the data********************/
    set obs 10
    gen year=_n
    la var year "Year"
    gen couns1=0
    replace couns1=3079.18381538462 if (year/5)==round(year/5)
    gen couns2=0
    replace couns2=2*3079.18381538462 if (year/5)==round(year/5)
    gen onetime=0
    replace onetime=94098.1487 if year==1
    gen yrcostN75=onetime+couns1+75*220.157112683779
    gen yrcostN151=onetime+couns1+151*220.157112683779
    gen yrcostN302=onetime+couns2+302*220.157112683779
    foreach x of numlist 75 151 302 {
    gen cumtotcostN`x'yr = sum(yrcostN`x')
    gen cumN`x'=year*`x'
    gen perperscostN`x'yr=cumtotcostN`x'yr/cumN`x'
    replace cumtotcostN`x'yr=cumtotcostN`x'yr/1000
    la var perperscostN`x'yr "`x' patients/year"
    la var cumtotcostN`x'yr "`x' patients/year"
    }
    /************************************************/

    /*Pay attention here for graphics issues*/
    /*Legend inside panel*/

    twoway line cumtotcostN75yr cumtotcostN151yr cumtotcostN302yr year, legend(ring(0) position(10)) saving(cumulative, replace) ytitle(`"(`=char(36)'1,000s)"') title("B. Cumulative Total Costs")
    twoway line perperscostN75yr perperscostN151yr perperscostN302yr year, saving(perperson, replace) legend(off) ytitle("(`=char(36)')") title("A. Per Person Costs")
    gr combine perperson.gph cumulative.gph

    /*Legend outside panel*/

    twoway line cumtotcostN75yr cumtotcostN151yr cumtotcostN302yr year, legend(ring(1) position(3)) saving(cumulative, replace) ytitle(`"(`=char(36)'1,000s)"') title("B. Cumulative Total Costs")
    twoway line perperscostN75yr perperscostN151yr perperscostN302yr year, saving(perperson, replace) legend(off) ytitle("(`=char(36)')") title("A. Per Person Costs")
    gr combine perperson.gph cumulative.gph


    ​​​​​​​

  • #2
    As far as I am aware, there is no way to do this natively in stata. There is a user written package "grc1leg2" that will allow you to do what you are asking. Replacing "gr combine perperson.gph cumulative.gph" in your "/*Legend outside panel*/" block of code with the code below worked on my end. Hope it works for you too!

    Best,
    Alex

    Code:
    ssc install grc1leg2
    grc1leg2 perperson.gph cumulative.gph

    Comment


    • #3
      Statalist won't let me post the code, but the main idea is in

      SJ-20-4 gr0085 . The by() option of graph can work better than graph combine
      . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
      4/20 SJ 20(4):1016--1027 (no commands)
      describes whether to use the by() option of graph or
      graph combine for paneled figures
      Last edited by Nick Cox; 27 Oct 2025, 16:05.

      Comment


      • #4
        Here is the graph:

        Comment


        • #5
          I will try posting the code later. The forum software is objecting to characters that aren't discernibly in my code at all. Can anyone construe why a do-file that runs fails to be posted with this message?

          Error while saving content: SyntaxError: Unexpected token 'P', " Please ena"... is not valid JSON
          Last edited by Nick Cox; 27 Oct 2025, 16:21.

          Comment


          • #6
            Nick Cox, thanks so much for pointing me to the SJ article and sending your code. It worked like a charm. I'm having the same issue posting the code, and indeed had the same issue when I posted my original code. Initially, it failed using Safari, but then worked with Firefox. No such luck this time, though. Puzzling indeed.
            -Doug

            Comment


            • #7
              Multiple attempts at posting the code have failed in different browsers on different machines. Sorry about that. but I can't see that anything is wrong on my side.

              Now the graph in #4 is displaying as an icon on some browsers and as desired on others. Here it is again.

              Click image for larger version

Name:	nocombine.png
Views:	1
Size:	45.9 KB
ID:	1782719

              Comment


              • #8
                Doug Levy and Nick Cox,

                Thank you both for the alert on the odd posting issue. I found the rule that was triggering the exception and it has been modified appropriately. Please let me know if you notice any other issues posting to the forums.

                -Pete

                Comment


                • #9
                  Pete Huckelba (StataCorp) Thanks very much for your close attention!

                  Here's another go at posting the code: And it worked too.

                  Code:
                  clear
                  
                  /***********Sets up the data********************/
                  set obs 10
                  gen year=_n
                  la var year "Year"
                  gen couns1=0
                  replace couns1=3079.18381538462 if (year/5)==round(year/5)
                  gen couns2=0
                  replace couns2=2*3079.18381538462 if (year/5)==round(year/5)
                  gen onetime=0
                  replace onetime=94098.1487 if year==1
                  gen yrcostN75=onetime+couns1+75*220.157112683779
                  gen yrcostN151=onetime+couns1+151*220.157112683779
                  gen yrcostN302=onetime+couns2+302*220.157112683779
                  foreach x of numlist 75 151 302 {
                  gen cumtotcostN`x'yr = sum(yrcostN`x')
                  gen cumN`x'=year*`x'
                  gen perperscostN`x'yr=cumtotcostN`x'yr/cumN`x'
                  replace cumtotcostN`x'yr=cumtotcostN`x'yr/1000
                  la var perperscostN`x'yr "`x' patients/year"
                  la var cumtotcostN`x'yr "`x' patients/year"
                  }
                  /************************************************/
                  
                  * new code starts here
                  rename (cumtotcostN*) (b75 b151 b302)
                  rename (perperscostN*) (a75 a151 a302)
                  
                  stack a* year b* year, into(cost75 cost151 cost302 year) clear
                  
                  label def _stack 1 "A. Per Person Costs ($)" 2 "B. Cumulative Total Costs (`=char(36)'1,000s)"
                  label val _stack _stack
                  
                  foreach x in 75 151 302 {
                      label var cost`x' "`x' patients/year"
                  }
                  twoway line cost* year, by(_stack, note("") yrescale legend(pos(12))) ///
                  legend(order(1 2 3) row(1)) xla(1 5 10)
                  Last edited by Nick Cox; 28 Oct 2025, 09:13.

                  Comment

                  Working...
                  X