Announcement

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

  • Legend is distorted when stacking graphs using -graph combine-

    I'm trying to replicate a set of plots from an internal publication (which unfortunately I can't share here) that's currently implemented in Excel. I create three graphs, each of which look like this:



    This graph is almost what I want (minus the faint blue outline around the whole chart, but that's another topic), until I combine it with two other graphs using -graph combine-:



    To illustrate, I just used the same plot three times. When I use -graph combine-, the legend is distorted to fill the entire width of the plot. I'm guessing that this has something to do with the -scale- option, but I can't figure out how to tweak it to fix this. What am I doing wrong?

    Here is the code I used to input the data and generate the plots:

    Code:
    graph drop _all
    clear
    input int date double(rate2014 rate2015 rate2016 rate2017 rate2018)
    19016 .16  .04    .    .    .
    19034 .21  .81    .    .    .
    19076 .24  .65    .    .    .
    19085 .05  .19    .    .    .
    19141 .21  .39    .    .    .
    19159  .1  .93    .    .    .
    19198  .1  .31    .    .    .
    19231 .11  .43    .    .    .
    19240  .1  .86    .    .    .
    19269 .11   .1    .    .    .
    19323 .14  .23    .    .    .
    19341  .1  .17    .    .    .
    19376 .07  .12  .63    .    .
    19399 .07  .74 1.48    .    .
    19445 .04  .72 1.24    .    .
    19476 .14  .15 1.07    .    .
    19483 .07  .54  .72    .    .
    19512 .07  .55  .61    .    .
    19555 .17  .65 1.25    .    .
    19596 .14  .45 1.14    .    .
    19614 .22  .64 1.38    .    .
    19656 .18  .77 1.13    .    .
    19678   0  .22 1.02    .    .
    19715 .16  .99 1.67    .    .
    19739 .07  .54 1.27 1.16    .
    19771 .08  .56 1.86 1.02    .
    19792   0  .93 1.75 1.74    .
    19815 .11  .91  1.4 1.59    .
    19848 .21  .52 1.24 1.96    .
    19902  .1  .95 1.29 1.42    .
    19931 .01   .5 1.38 1.41    .
    19953 .15  .93 1.29 1.15    .
    19993 .05  .55 1.35 1.61    .
    20016  .2  .86 1.42 2.22    .
    20054 .03  .35  1.5 2.05    .
    20076 .05  .77 1.98 1.74    .
    20109 .02 1.35 1.76 2.79 2.56
    20145 .17 1.62 2.29 2.45  2.6
    20163 .09 1.74 2.43 2.99 2.46
    20204 .21 1.06  1.6 2.99 2.55
    20232 .01  1.2 2.26 2.02 2.04
    20248  .1 1.38 1.61 2.07 2.43
    20279 .09 1.35 2.08 2.08 2.43
    20309 .01 1.75 1.68 2.34 1.86
    20332 .03  1.9 1.69 2.73 2.86
    20373 .13 1.38 2.02  2.9 3.16
    20414 .18 1.53 1.73 2.46 2.49
    20450 .14 1.35 1.76 2.01 2.44
    end
    
    * make sure data start on Jan 1, 2012
    set obs `=_N+1'
    replace date = td(1jan2012) if missing(date)
    tsset date, daily
    tsfill
    levelsof date if !missing(rate2015), local(key_dates)
    
    local xlabels
    summ date
    forv i = `r(min)'/`r(max)' {
        if day(`i') == 1 {
            if month(`i') == 1 local lbl: disp %tdMon_!'YY `i'
            else               local lbl: disp %tdMon `i'
            local xlabels `xlabels' `i' "`lbl'"
        }
    }
    
    # delimit ;
    twoway
           (connected rate2014 date, msize(medium) msymbol(square) lwidth(medium))
           (connected rate2015 date, msize(medium) msymbol(square) lwidth(medium))
           (connected rate2016 date, msize(medium) msymbol(square) lwidth(medium))
           (connected rate2017 date, msize(medium) msymbol(square) lwidth(medium))
           (connected rate2018 date, msize(medium) msymbol(square) lwidth(medium))
           ,
           ytitle("")
           yscale(noline)
           ylabel(0(1)4, angle(horizontal) format(%2.1f) grid glwidth(thin) glcolor(black) gmin gmax)
           xtitle("")
           xscale(noline)
           xline(`key_dates', lcolor(gs12) noextend)
           xlabel(`xlabels', angle(vertical))
           title("Assumptions")
           legend(on order(1 "2014:Q4" 2 "2015:Q4" 3 "2016:Q4" 4 "2017:Q4" 5 "2018:Q4")
               rows(1) colgap(small) keygap(small) size(3) region(lcolor(none))
               bmargin(top) position(11) ring(0))
           xsize(8.5) ysize(2.5)
           graphregion(margin(0 0 15 0) fcolor(white) lcolor(none))
           name(rates, replace)
    ;
    graph combine rates rates rates,
        rows(3)
        scale(0.75)
        xsize(8.5)
        ysize(11)
        title("Charts")
        graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white))
    ;
    # delimit cr
    graph export "sample.pdf", replace
    Also, I fully understand that this plot is ugly and not an ideal way to represent the data. However, that not withstanding, I still need to replicate the original.
    Attached Files
    sample chart
    Last edited by Michael Anbar; 11 Dec 2015, 13:31.

  • #2
    Is anyone able to replicate this problem? I should add that I'm using Stata 14.1 MP on a Windows 7 64-bit system.

    Comment


    • #3
      I get the same graph when I run your code. Have you considered using grc1leg?
      Code:
      findit grc1leg
      For the thin blue lines around the charts see these Statalist posts:

      Comment


      • #4
        I'll look into that, but unfortunately, each graph has to have its own legend, and the legend is different for each chart. (See my initial post; in my example, I just used the same graph for all three rows, but obviously in reality the graphs are different).

        Comment


        • #5
          You can experiment with options like symxsize(), keygap() and colgap() to reduce the amount of space occupied by the legend.
          Code:
          legend(on order(1 "2014:Q4" 2 "2015:Q4" 3 "2016:Q4" 4 "2017:Q4" 5 "2018:Q4")
            rows(1) size(3) region(lcolor(none))
            bmargin(top) position(11) ring(0)
            symxsize(*.3) keygap(*.2) colgap(*.5))

          Comment


          • #6
            Thank you. I'll look into the relative sizing options.
            Last edited by Michael Anbar; 14 Dec 2015, 15:18.

            Comment


            • #7
              Please use the full legend() code from post #5. In your code you have keygap() and colgap() twice within legend(), which causes the error message.

              Comment

              Working...
              X