Announcement

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

  • Modify additional key to legend, not ex-post (graph editor)

    Dear Statalists,

    I plotted some impulse response functions and I had to combine two graphs and their respective legends (different). In order to solve the problem I just included an additional element (line of the second graph) in the legend of the first graph. However, I get a neutral symbol (i.e. without the line color, pattern and width of the original line). I highlight in red in the code (i) where I included the additional element in the legend of the first graph, without reference to any line, (ii) the line color, pattern and width of the line in question.

    The code is the following
    Code:
    Code:
     gen _h = h if h<=8;
    gen _zero = 0;  
    
    twoway  (line _zero h if h>=1 & h<=16, lcolor(black) lpattern(solid) ) ///        
                  (line multexp1  h if h>=1 & h<=16, connect(l) lcolor(navy) lpattern(solid) lwidth(thick)) ///        
                  (rarea up90fb lo90fb h if h>=1 & h<=16, lcolor(navy%20) fcolor(navy%20) lwidth(none)) ///        
                  (line up90hb h if h>=1 & h<=16, lcolor(cranberry) lpattern("_###_###") lwidth(medthick)) ///        
                  (line lo90hb h if h>=1 & h<=16, lcolor(cranberry) lpattern("_###_###") lwidth(medthick)) ///        
                  (scatter multrec1  h if h>=1 & h<=16, connect(l) lcolor(cranberry) lpattern(solid) lwidth(thick) msymbol(C) msize(medlarge) mcolor(cranberry)), ///        
                  xscale(range(1 16)) xlabel(2(2)16) xtitle("quarter") ylabel(, format(%03.1f) angle(0)) legend(order(2 "High private debt" 6 "Low private debt" 1 "Difference")  size(small)) graphregion(color(white)) bgcolor(white);        
    
    graph rename full_model, replace;  
    
    twoway  (line _zero h if h>=1 & h<=16, lcolor(black) lpattern(solid)) ///        
                  (rarea up90diff lo90diff h if h>=1 & h<=16, fcolor(eltgreen%50) lcolor(eltgreen%50) lwidth(none)) ///        
                  (line diff1  h if h>=1 & h<=16, lcolor(green) lpattern("longdash") lwidth(thick)) ///        
                  ,xscale(range(1 16)) xlabel(2(2)16) xtitle("quarter") ylabel(, format(%03.1f)  angle(0)) legend(order(3 "Difference")) graphregion(color(white)) bgcolor(white);        
    
    graph rename diff_model, replace;          
    
    grc1leg2 full_model diff_model, title("US `graphname' multiplier",size(medsmall) span) rows(1) c(1) xsize(2.5) legendfrom(full_model)  lcols(3) ltsize(small)  graphregion(color(white)) graphregion(margin(zero));
    I would like to automatize the code so I don't need to manually change the line color, pattern and width in the legend for the added element ("Difference").

    Let me attach also a figure in case I didn't make clear my point.
    graph_ct.pdf

    Thanks,
    Alessandro
    Last edited by Alessandro Franconi; 26 Mar 2021, 06:38.

  • #2
    grc1leg2 is from http://digital.cgdev.org/doc/stata/MO/Misc.

    Code:
    net install grc1leg2.pkg, from(http://digital.cgdev.org/doc/stata/MO/Misc/)
    From the description of grc1leg2, you have


    grc1leg2 -- Combine multiple graphs with a single common legend
    The command has to retrieve a legend from one of the graphs, so if you have different legends, it is not useful. A hack is to introduce an impossible condition(s) in the first graph so as to force the graph to include the legend(s) present in the second graph. The impossible condition is highlihted in red below.

    Code:
    webuse grunfeld
    keep if company<4
    tw (line invest year) (line invest year if year>5000, lcolor(green) lpattern("longdash") ///
    lwidth(thick) ) (line invest year if company==1, lcolor(cranberry) lpattern("_###_###") ///
    lwidth(medthick) legend(order(1 "invest all" 2 "kstock" 3 "invest co. 1")) ///
    scheme(s1color) saving(gr1, replace))
    
    tw (line kstock year, lcolor(green) lpattern("longdash") lwidth(thick) saving(gr2, replace) ///
    scheme(s1color) leg(on))
    
    grc1leg2 gr1.gph gr2.gph, title("Whatever",size(medsm) span) row(1) c(1) xsize(2.5) ///
    scheme(s1color) lcols(3)
    Res.:
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	39.2 KB
ID:	1600029

    Comment


    • #3
      Great idea, thanks!

      Comment

      Working...
      X