Announcement

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

  • Addplot and Legend Positioning

    I want to add a text with an angle to my twoway graph, which I achieve by using addplot: scatteri (...). However, this overrides my legend. If I specify legend(off) in the addplot: (...), I have no legend at all. If I try to add a legend via addplot, it seems to ignore the positioning that worked in the first instance. What would be a way to combine both a custum legend placement and a text with an angle in one plot?

    Code:
    sysuse auto, clear
    regress headroom trunk
        local N_obs1 = e(N)
        local beta1 = string(round(_b[trunk],0.001),"%4.3f")
        local se1 = string(round(_se[trunk],0.001),"%4.3f")
        local constant1 = string(round(_b[_cons],0.001),"%4.3f")
    regress headroom length
        local N_obs2 = e(N)
        local beta2 = string(round(_b[length],0.001),"%4.3f")
        local se2 = string(round(_se[length],0.001),"%4.3f")
    
    * original idea of graph
    twoway function y = x*500, range(0 40) || function y = 250*x, range(0 40) || ///
    (function y = 750*x, range(0 40)), legend(order(1 "Slope 1: `beta1' (SE `se1')" ///
     2 "Slope 2:  `beta2' (SE `se2')") cols(1) ring(0) size(small) position(11) ///
     region(lwidth(none))) legend(size(medsmall))    
    
    * trying to add comment with angle
    twoway function y = x*500, range(0 40) || function y = 250*x, range(0 40) || ///
    (function y = 750*x, range(0 40)), legend(order(1 "Slope 1: `beta1' (SE `se1')" ///
     2 "Slope 2:  `beta2' (SE `se2')") cols(1) ring(0) size(small) position(11) ///
     region(lwidth(none))) legend(size(medsmall))    
        addplot: scatteri 5000 20 "Some Comment", mlabangle(35)
    
    * second attempt, positioning of legend does not work out
    twoway function y = x*500, range(0 40) || function y = 250*x, range(0 40) || ///
    function y = 750*x, range(0 40)
        addplot: scatteri 5000 20 "Some Comment", mlabangle(35)
        addplot: , legend(order(1 "Slope 1: `beta1' (SE `se1')" 2 "Slope 2:  `beta2' (SE `se2')") cols(1) ring(0) size(small) position(11) region(lwidth(none))) legend(size(medsmall))

  • #2
    The -addplot- is a community-contributed command written by Ben Jann. Its legend setting is a bit of complicated. Ben Jann explained in help file that
    The legend: addplot may cause a legend to be added to the graph. To suppress adding a legend, apply option legend(off) to the added twoway command. (So this is the case in your second graph.)......Furthermore, addplot always recreates an existing legend using default legend keys. To preserve or create a custom legend you need to specify an appropriate legend(order()) option with the added twoway command. (And this is what I try to do in the following code.)
    Thus I think the solution is repeating original legend setting in addplot. Hope this will be helpful.
    Code:
    twoway function y = x*500, range(0 40) || function y = 250*x, range(0 40) || ///
     function y = 750*x, range(0 40) ///
     legend(order(1 "Slope 1: `beta1' (SE `se1')" 2 "Slope 2:  `beta2' (SE `se2')") ///
     cols(1) ring(0) size(small) position(11) region(lwidth(none))) legend(size(medsmall))
     
    addplot: scatteri 5000 20 "Some Comment", mlabangle(35) ///
     legend(order(1 "Slope 1: `beta1' (SE `se1')" 2 "Slope 2:  `beta2' (SE `se2')") ///
     cols(1) ring(0) size(small) position(11) region(lwidth(none))) legend(size(medsmall))
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	63.8 KB
ID:	1697661

    Comment


    • #3
      Thank you so much! I had no idea that I had to repeat the legend, but as you have demonstrated, it works this way.

      Comment

      Working...
      X