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


            • #7
              Dear Andrew Musau thank you for advancing the discussion.

              Is it possible to add marker in legend with CI caps as well?
              Best regards,
              Mukesh

              Comment


              • #8
                With the twoway plot types, AFAIK, you can get uncapped lines with markers (using tw connected) or capped lines (using tw rcap), but not capped lines with markers. An alternative would be to bring the capped-line legend within the plot region and add markers to it, or to switch off the legend entirely and generate your own.

                Code:
                sysuse auto, clear
                reg price c.weight#i.foreign
                margins, at(weight=(2000(1000)4000)) by(foreign)
                
                marginsplot, addplot((connected mpg weight foreign if mpg<0, lc(stc1) mc(stc1)) ///
                (connected mpg weight foreign if mpg<0, lc(stc2) mc(stc2) ///
                legend(order(5 "D" 7 "F")))) saving(gr1, replace)
                
                
                marginsplot, addplot((rcap mpg weight foreign if mpg<0, lc(stc1) mc(stc1)) ///
                (rcap mpg weight foreign if mpg<0, lc(stc2) mc(stc2) ///
                legend(order(5 "D" 6 "F")))) saving(gr2, replace)
                
                gr combine gr1.gph gr2.gph
                Click image for larger version

Name:	Graph.png
Views:	1
Size:	57.3 KB
ID:	1781074

                Last edited by Andrew Musau; 16 Aug 2025, 10:58.

                Comment


                • #9
                  Thank you for clarification "but not capped lines with markers".
                  The two graph shows are can be created easily with
                  Code:
                  marginsplot, leg( order (1 "D" 2 "F"))
                  for capes and
                  Code:
                  marginsplot, leg( order (3 "D" 4 "F"))
                  for without capes at least in Stata 18 SE

                  Thank you Mukesh
                  Best regards,
                  Mukesh

                  Comment


                  • #10
                    Correct.

                    Comment


                    • #11
                      A small follow up query.
                      Is it possible to change width of marker outline. suppose my marker type is "Oh" and I want outline width thin. I am asking because we can change the line width with lwidth.
                      Best regards,
                      Mukesh

                      Comment


                      • #12
                        Code:
                        help scatter##marker_options
                        mlwidth(linewidthstylelist) thickness of outline

                        Comment

                        Working...
                        X