Announcement

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

  • Markers in marginsplot legend

    Hi Statalist,

    I am using marginsplot to graph an interaction (Stata 15). I have gotten the plot close to what I want but am having trouble with the legend. Is there a way to get rid of the markers that are in the center of the lines in the legend? I only want the lines, one solid and the other dashed. Here is my syntax:

    Code:
        marginsplot, x(cl_kEzs2) ///
            xlab(-.25 " " 0 "Restricted" ///
            1 "Diverse" 1.25 " ", notick) ///
            xlab(,labsize(med)) ///
            plot1opts(msymbol(O) mcolor(red) lcolor(red) lwidth(medthick)) ///
            ci1opts(lcolor(red)) ///
            plot2opts(msymbol(O) mcolor(navy) lcolor(navy) lwidth(medthick) lpattern("--")) ///
            ci2opts(lcolor(navy)) ///
            legend(pos(6) size(medsmall) symxsize(15)) ///
            xtitle("SNT", size(medlarge)) ///
            ytitle(Pr(Major depression), size(medlarge)) ///
            ylab(, labsize(med)) ///
            title("") ///
            aspectratio(.8) ///
            plot( , label("No" "Yes"))
    I uploaded the graph as png. Hopefully it is legible.

    Thanks for your help,
    Robbie

    Click image for larger version

Name:	Graph.png
Views:	1
Size:	43.8 KB
ID:	1545573

  • #2
    Code:
    marginsplot, leg(order(1 2))

    Comment


    • #3
      Hi,

      Thanks for the response. When I tried that, the dashed line becomes solid and the labels (No and Yes) revert back to "predicted mean Depbi." Depbi is the outcome variable. Is there a way to keep the line dashed, keep the labels in the legend, and remove the marker in the middle of the lines?

      Thanks,
      Robbie

      Comment


      • #4
        You can explicitly assign the labels, e.g.,

        Code:
        marginsplot, leg(order(1 "Yes" 2 "No"))
        The issue of the dashed line is difficult to work out without a reproducible example. What I offer here is just general advice on restricting contents of the legend.

        Comment


        • #5
          I encountered a similar issue with legend labels in -marginsplot-. This post was helpful: https://www.statalist.org/forums/for...in-marginsplot. If it's not immediately clear in that post, the issue is that -marginsplot- begins counting plot elements with confidence intervals first, followed by the markers. Here is a simple illustration:

          Code:
          sysuse auto
          reg price c.weight#i.foreign
          margins, at(weight=(2000(1000)4000)) by(foreign) noatlegend
          marginsplot,  legend(order(1 "D" 2 "F")) // 1 and 2 refer to the CIs here.
          marginsplot,  legend(order(3 "D" 4 "F")) // 3 and 4 refer to the markers.
          This behavior is not intuitive and I have have not found it documented in a help file (though if you have, please share).

          To the original question, perhaps you could recast the graph to line type and use the numbering scheme described above for the legend:

          Code:
          marginsplot,  legend(order(3 "D" 4 "F")) recast(line)

          Comment


          • #6
            Thanks for the reproducible example. The option -recast(line)- changes the underlying graph, which is not what is intended in #1. It is possible to do what the OP wants using the -addplot- option where the added plot is non-existent. Specifying an impossible condition (e.g., negative values of mpg below) is one way to do this.

            Code:
            sysuse auto
            reg price c.weight#i.foreign
            margins, at(weight=(2000(1000)4000)) by(foreign)
            marginsplot, addplot((line mpg weight if mpg<0, lc(red)) ///
            (line mpg weight if mpg<0, lc(navy)), legend(order(5 "D" 6 "F")))
            Click image for larger version

Name:	Graph.png
Views:	1
Size:	68.0 KB
ID:	1622573

            Comment

            Working...
            X