Announcement

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

  • How to change the legend in a graph by STATA?

    I am plotting a graph, the code is below

    Btw, x,w,z are continues variables

    Code:
    /* determined high and low values for x */
    sum x
    global Hx=r(mean)+r(sd)
    global Lx=r(mean)-r(sd)
    
    /* determined high and low values for w */
    sum w
    global Hw=r(mean)+r(sd)
    global Lw=r(mean)-r(sd)
    
    /* determine high and low values for z */
    sum z
    global Hz=r(mean)+r(sd)
    global Lz=r(mean)-r(sd)
    
    margins, at(x=($Hx $Lx) w=($Hw $Lw) z=($Hz $Lz)) vsquish
    marginsplot, recast(line) noci ///
                xtitle("A") ///
                ytitle("B") ///
                xlabel($Hx "High x" $Lx "Low x", nogrid labsize(2.5)) ///
                legend(position(6) col(2))
    I got the following plot

    Click image for larger version

Name:	image_9892.png
Views:	1
Size:	66.0 KB
ID:	1429418

    The keys in the legend are numbers, I was wondering whether I can use code to change them.

    For example, when w= -.9960929 (i.e. $Lw), the key in the legend automatically changes to "low w"

    I know the -order- command in -legend- can change the keys manually (as below), but it's not elegant. Is it possible to let Stata itself change the keys?

    Code:
    legend(position(6) col(2) ///
               order(1 "Low w, Low z" ///
                        2 "Low w, High z" ///
                        3 "High w, Low z" ///
                        4 "High w, High z"))
    Last edited by Victor Smith; 09 Feb 2018, 09:58.
Working...
X