Announcement

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

  • Moving legend to empty space in GRAPH COMBINE

    Dear members,

    I am trying to move the legends of some plots to an empty space in
    Code:
    GRAPH COMBINE
    .

    I have the individual plots already generated and saved in disk. So I am not willing to change the actual plots, but their appearance in GRAPH COMBINE.

    I am providing a sample code here:
    Code:
    * use https://www.stata-press.com/data/r16/uslifeexp, clear
    sysuse uslifeexp, clear
    generate diff = le_wm - le_bm
    label var diff "Difference"
    
    local ys = "if (year < 1930)"
    line le_wm year `ys' , yaxis(1 2) xaxis(1 2) ///
    || line le_bm year `ys' ///
    || line diff year `ys' ///
    || lfit diff year `ys' ///
    ||, ///
    ylabel(0(5)20, axis(2) gmin angle(horizontal)) ///
    ylabel(0 20(10)80, gmax angle(horizontal)) ///
    ytitle("", axis(2)) ///
    xlabel(1918, axis(2)) xtitle("", axis(2)) ///
    ytitle("Life expectancy at birth (years)") ///
    ylabel(, axis(2) grid) ///
    title("White and black life expectancy") ///
    subtitle("USA, 1900-1929") ///
    note("Source: National Vital Statistics, Vol 50, No. 6" ///
    "(1918 dip caused by 1918 Influenza Pandemic)") ///
    legend(label(1 "White males") label(2 "Black males"))
    
    graph rename p1, replace
    
    local ys = "if (year >= 1930 & year < 1970)"
    line le_wm year `ys' , yaxis(1 2) xaxis(1 2) ///
    || line le_bm year `ys' ///
    || line diff year `ys' ///
    || lfit diff year `ys' ///
    ||, ///
    ylabel(0(5)20, axis(2) gmin angle(horizontal)) ///
    ylabel(0 20(10)80, gmax angle(horizontal)) ///
    ytitle("", axis(2)) ///
    ytitle("Life expectancy at birth (years)") ///
    ylabel(, axis(2) grid) ///
    title("White and black life expectancy") ///
    subtitle("USA, 1930-1969") ///
    note("Source: National Vital Statistics, Vol 50, No. 6") ///
    legend(label(1 "White males") label(2 "Black males"))
    
    graph rename p2, replace
    
    local ys = "if (year >= 1970)"
    line le_wm year `ys' , yaxis(1 2) xaxis(1 2) ///
    || line le_bm year `ys' ///
    || line diff year `ys' ///
    || lfit diff year `ys' ///
    ||, ///
    ylabel(0(5)20, axis(2) gmin angle(horizontal)) ///
    ylabel(0 20(10)80, gmax angle(horizontal)) ///
    ytitle("", axis(2)) ///
    ytitle("Life expectancy at birth (years)") ///
    ylabel(, axis(2) grid) ///
    title("White and black life expectancy") ///
    subtitle("USA, 1970-1999") ///
    note("Source: National Vital Statistics, Vol 50, No. 6") ///
    legend(label(1 "White males") label(2 "Black males"))
    
    graph rename p3, replace
    
    // ******************* ONLY CHANGE AFTER THIS POINT ******************************
    
    graph combine p1 p2 p3, ///
            col(2) iscale(*.6)
            
    graph display , ysize(11) xsize(8.5)
    The plot I want to accomplish is more or less like that:
    Click image for larger version

Name:	Graph.jpg
Views:	1
Size:	135.1 KB
ID:	1595608


    Any ideas are welcome (even if they involve regenerating the original plots -- it takes a while, but if necessary, so be it!)

    Best regards,

    Iuri.

  • #2
    Use the -by()- option of twoway. For more details on why this may work better, see the following Stata tip by Nick Cox. Without exactly replicating all the details in your graph, here is an illustration.

    Code:
    sysuse uslifeexp, clear
    gen plot= cond(year<1930, 1, cond(inrange(year, 1930, 1970), 2, 3))
    tw (line le_wm year, by(plot, yrescale)) (line le_bm year, by(plot, ///
    note("") rescale legend(at(4) pos(0))) leg(rows(2))  xtitle(""))
    Res.:
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	78.3 KB
ID:	1595628

    Comment


    • #3
      Hi Andrew,
      These plots are just an example, not my actual plots. I have run moderated regressions and they don't fit the way you describe. As I mentioned in my first post, I don't need a new "plot" command. I want a way to combine existing plots (not these, similar to those) in a new format.
      Thanks anyway.
      Iuri.

      Comment


      • #4
        There are a number of community-contributed commands that will give you 1 legend. Whether they allow placement within the plot region, I do not know. See, for example,

        Code:
        net describe grc1leg2, from(http://digital.cgdev.org/doc/stata/MO/Misc)

        Comment


        • #5
          Andrew,
          GRC1LEG2 does the trick! Thank you!

          You need to use ring(0) and pos(4).

          Iuri.
          Last edited by Iuri Gavronski; 01 Mar 2021, 07:39.

          Comment

          Working...
          X