Announcement

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

  • Marginsplot / addplot syntax

    I am adding two limit lines with addplot to a plot from marginsplot using the following code:
    Code:
    marginsplot, title("Darkness, Black background") xlabel(100(100)1500) ytitle("{&Delta} E")    ///
        plot1opts(lcolor(dkorange) mcolor(dkorange)) ci1opts(lcolor(dkorange))            ///
        plot2opts(lcolor(maroon) mcolor(maroon)) ci2opts(lcolor(maroon))            ///
        plot3opts(lcolor(gold) mcolor(gold)) ci3opts(lcolor(gold))                ///
        plot4opts(lcolor(gs9) mcolor(gs9)) ci4opts(lcolor(gs9))                    ///
        plot5opts(lcolor(midgreen) mcolor(midgreen)) ci5opts(lcolor(midgreen))            ///
        plot6opts(lcolor(red) mcolor(red)) ci6opts(lcolor(red))                    ///
        plot7opts(lcolor(dkgreen) mcolor(dkgreen)) ci7opts(lcolor(dkgreen))            ///
        addplot(pci 1 100 1 1500, lpattern(shortdash) lcolor(black) legend(label(8 "Lower"))|| pci 2 100 2 1500, lpattern(longdash) lcolor(black) legend(label(9 "Upper")))
    I have tried to label the lines but they both appear labelled as y/yb in the key region rather than Lower and Upper. I can change these using the graphics editor but I would prefer to do it in the command line - if I could work out the syntax. The graphics editor shows the labels to be labels [8] and [9].

    I am using Stata 14.

    Martyn

  • #2
    You can apply any changes made with the Graph Editor in a do-file, using the gr_edit command. See the two threads below for examples.There is probably a more direct solution but your example isn't reproducible so I cannot experiment with the code.

    Comment


    • #3
      Sorry, I should have produced a reproducible example:
      Code:
      webuse nhanes2, clear
      regress bpsystol agegrp##sex
      margins agegrp#sex
      marginsplot, addplot(pci 130 1 130 6, lpattern(shortdash) lcolor(black) legend(label(8 "Lower")))

      Comment


      • #4
        From help marginsplot:

        The label() suboption of the legend() option has no effect on marginsplot. Use the order() suboption instead.
        Code:
        marginsplot, addplot(pci 130 1 130 6, lpattern(shortdash) lcolor(black) legend(order(3 "Male" 4 "Female" 5 "Lower")))

        Comment


        • #5
          I must learn to read!
          When I used legend(order...) I got the label but lost the line definition. However using gr_edit solved the problem.

          Thank you.
          Martyn

          Comment


          • #6
            Try different numbers in the order() option. Compare the pictures produced with the commands below. The first graph has the line for the "Lower" entry in the legend, the second doesn't.
            Code:
            webuse nhanes2, clear
            regress bpsystol agegrp##sex
            margins agegrp#sex
            marginsplot, addplot(pci 130 1 130 6, lpattern(shortdash) lcolor(black) legend(order(3 "Male" 4 "Female" 5 "Lower"))) name(g1)
            marginsplot, addplot(pci 130 1 130 6, lpattern(shortdash) lcolor(black) legend(order(3 "Male" 4 "Female" 6 "Lower"))) name(g2)

            Comment

            Working...
            X