Announcement

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

  • Graph Combine - shared y-label by row?

    I'm using
    Code:
    graph combine
    and would like to have a shared y-label by row instead of one for each graph. I have looked a lot for this with no luck.
    Any ideas of how to do this?

    This is my code:
    Code:
    graph combine Overall Females Males lowedu mededu highedu, ///
    rows(2) ///
    graphregion(fcolor(white))
    And this is the figure for which I would like to have one y-label per row:
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	74.6 KB
ID:	1670737


    Kind regards,
    Jannie

  • #2
    Generate the graphs without y-titles, including the option

    Code:
    ..., ytitle("")
    and then see

    Code:
    help title_options
    after which you can specify an appropriate title in gr combine.

    Comment


    • #3
      You could edit out the axis titles ("labels" here is other software's terminology, not Stata's) that you don't want but then the plot region would expand and give you different-sized graphs

      You could get there with a combination of tricks as discussed in

      https://www.stata-journal.com/articl...article=gr0058

      https://journals.sagepub.com/doi/pdf...36867X20976341


      This is a little fiddly but the logic is not hard to follow.

      Code:
      sysuse auto, clear 
      
      gen long obs = _n 
      
      preserve 
      
      expand 3 
      
      bysort obs : gen which = _n 
      
      * 1 is all observations 
      gen group = 1 if which == 1 
      label def group 1 "All cars"
      
      * 2 is domestic, 3 is foreign 
      replace group = 2 + foreign if which == 2 
      label def group 2 "Domestic" 3 "Foreign", add 
      
      * 4 to 8 are repair record 1 to 5 
      replace group = 3 + rep78 if which == 3 
      label def group 4 "Repair record 1" 5 "Repair record 2" 6 "Repair record 3" 7 "Repair record 4" 8 "Repair record 5", add 
      
      label val group group 
      
      set scheme s1color 
      scatter mpg weight, by(group, note("")) ms(Oh)
      Click image for larger version

Name:	composite.png
Views:	1
Size:	56.7 KB
ID:	1670761


      In your case, the earlier code would need to be adapted for your own variables and the last line would need to be replaced with something more like

      Code:
      statsby mean=r(mean) ub=r(ub) lb=r(lb), by(group year) : ci mean income 
      
      twoway connected mean year || rcap ub lb year, by(group)
      but I can't do more than guess because you don't give a data example or tell us your variable names or how your intervals are calculated.


      Comment


      • #4
        Thanks for your help!

        I manage to do as intended by creating two 'sub-figures':

        Code:
        graph combine Overall Females Males, ///
        rows(1) ///
        graphregion(fcolor(white)) ///
        l1title("Cases/1000 PY", size(small)) ///
        b1title("Period (Calendar Years)", size(small)) ///
        ycommon ///
        name(row1, replace)
        
        
        graph combine Whites Blacks OtherRaces , ///
        rows(1) ///
        graphregion(fcolor(white)) ///
        l1title("Cases/1000 PY", size(small)) ///
        b1title("Period (Calendar Years)", size(small)) ///
        ycommon ///
        name(row2, replace)
        
        graph combine row1 row2 , ///
        xsize(8.5) ///
        ysize(11) ///
        rows(5) ///
        graphregion(fcolor(white))
        Now, I challenged by the graph for 'Black' not aligning horizontally with the other graphs in the same combine graph. I have tried everything: made sure to use identical syntax, same y-scale, used ycommon :-(
        Any ideas what's up?
        This is the full code:
        Code:
         
         /* WHITES */
         
        svyset ER31997 [pweight = SW], singleunit(scaled) strata(ER31996)
        svy, subpop(subpop_white): poisson d_1000 i.year i.sex age_split, exp(FUT_split)
        margins, over(year) expression(predict(n)/FUT_split)
        marginsplot,     legend(rows(1)) ///
                        xlabel(2005 "2001-2005" 2009 "2005-2009" 2013 "2009-2013" 2017 "2013-2017", labsize(vsmall)) ///
                        plotopts(lcolor(black) mcolor(black)) ///
                        ciopts(color(black)) ///
                        xtitle("") xscale(range(2019)) ///
                        ytitle("") ///
                        xscale(titlegap(*10)) ///
                        ylabel(25(10)65, angle(0) labsize(vsmall)) ///
                        title("Whites", size(medium) color(black)) ///
                        graphregion( color(white) ) ///
                        name(Whites, replace)
         
            /* AA */
        svyset ER31997 [pweight = SW], singleunit(scaled) strata(ER31996)
        svy, subpop(subpop_AA): poisson d_1000 i.year i.sex age_split, exp(FUT_split)
        margins, over(year) expression(predict(n)/FUT_split)
        marginsplot,     legend(rows(1)) ///
                        xlabel(2005 "2001-2005" 2009 "2005-2009" 2013 "2009-2013" 2017 "2013-2017", labsize(vsmall)) ///
                        plotopts(lcolor(black) mcolor(black)) ///
                        ciopts(color(black)) ///
                        xtitle("") xscale(range(2019)) ///
                        ytitle("") ///
                        xscale(titlegap(*10)) ///
                        ylabel(25(10)65, angle(0) labsize(vsmall)) ///
                        title("Blacks", size(medium) color(black)) ///
                        graphregion( color(white) ) ///
                        name(Black, replace)
         
            /* OTHER RACES */
        
        svyset ER31997 [pweight = SW], singleunit(scaled) strata(ER31996)
        svy, subpop(subpop_other): poisson d_1000 i.year i.sex age_split, exp(FUT_split)
        margins, over(year) expression(predict(n)/FUT_split)
        marginsplot,     legend(rows(1)) ///
                        xlabel(2005 "2001-2005" 2009 "2005-2009" 2013 "2009-2013" 2017 "2013-2017", labsize(vsmall)) ///
                        plotopts(lcolor(black) mcolor(black)) ///
                        ciopts(color(black)) ///
                        xtitle("") xscale(range(2019)) ///
                        ytitle("") ///
                        xscale(titlegap(*10)) ///
                        ylabel(25(10)65, angle(0) labsize(vsmall)) ///
                        title("Other Races", size(medium) color(black)) ///
                        graphregion( color(white) ) ///
                        name(OtherRaces, replace)
        
        graph combine Whites Blacks OtherRaces , ///
        rows(1) ///
        graphregion(fcolor(white)) ///
        l1title("Cases/1000 PY", size(small)) ///
        b1title("Period (Calendar Years)", size(small)) ///
        ycommon ///
        name(row2, replace)
        Click image for larger version

Name:	row2.png
Views:	1
Size:	124.5 KB
ID:	1671094

        Kind regards,
        Janniw

        Comment


        • #5
          Does adding -span- change anything?

          Code:
          b1title("Period (Calendar Years)", size(small) span)

          Comment


          • #6
            Unfortunately not. I get the error: option(s) b1title_span(yes) not allowed

            Comment


            • #7
              It's not the title as the following shows, so just ignore #5.

              Code:
              sysuse auto, clear
              local i 1
              foreach var in price mpg weight{
                  gr bar `var', over(rep78) ytitle("") saving(gr`i', replace)
                  local ++i
              }
              
              graph combine gr1.gph gr2.gph gr3.gph, ///
              rows(1) ///
              graphregion(fcolor(white)) ///
              l1title("Cases/1000 PY", size(small)) ///
              b1title("Period (Calendar Years)", size(small)) ///
              ycommon xcommon ///
              name(row2, replace)
              I would experiment with specifying the same range in all graphs by adding

              Code:
              yscale(range(15 80))
              It may be that the upper limit is a little bit higher for graph in the center. Otherwise, there is not much to go by without a reproducible example.

              Comment

              Working...
              X