Announcement

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

  • Coefplot: any way to position marker labels "away from" line of no effect ?

    Dear list member,

    re coefplot (SSC describe coefplot) by Ben Jann, I always wondered if there is a way to control the location of the marker labels -typically used to display coefficients- beyond a single value for the mlabp() option. The main problematic consequence of this restriction, in my experience, is the difficulty to visualize properly the labels when you have -to speak the epidemiology jargon- both risk and protective factors. The image should explain - a north-east position allows good visualization for the top values but fails for the bottom one, and vice versa.


    Click image for larger version

Name:	badlabelposition.JPG
Views:	1
Size:	9.0 KB
ID:	1780815



    Any workaround? Graph editor also allows to modify this only for all labels simultaneously.

    Best
    I'm using StataNow/MP 18.5

  • #2
    I have modified an example so that negative point estimates are shown to the left, positive ones to the right of the CIs:
    Code:
    sysuse auto, clear
    regress price mpg trunk length turn if foreign==0
    estimates store domestic
    regress price mpg trunk length turn if foreign==1
    estimates store foreign
    coefplot domestic foreign, drop(_cons) xline(0) ///
        addplot(scatter @at @ll if @b < 0, ms(i) mlabel(@b) mlabformat("%9.1f") mlabcolor(black) mlabpos(9) || ///
        scatter @at @ul if @b >= 0, ms(i) mlabel(@b) mlabformat("%9.1f") mlabcolor(black) mlabpos(3))
    https://repec.sowi.unibe.ch/stata/coefplot/markers.html
    Click image for larger version

Name:	test.png
Views:	2
Size:	76.8 KB
ID:	1780821
    Attached Files
    Best wishes

    Stata 18.0 MP | ORCID | Google Scholar

    Comment


    • #3
      Ah yes, that option, true. Wonderful. Thanks a lot.
      I'm using StataNow/MP 18.5

      Comment


      • #4
        Using addplot() in this way, however, doesn't seem to support conditions in the mlabel() option. I want to display only significant coefficients as labels. I use this

        Code:
        mlabel(cond(@pval<.05,string(@b,"%5.2f")+cond(@pval<.001, "***",cond(@pval<.01, "**",cond(@pval<.05, "*",""))),""))
        but if included in the solution presented above, i get "factor-variable and time-series operators not allowed".

        Any thoughts?
        I'm using StataNow/MP 18.5

        Comment


        • #5
          This works

          Code:
          sysuse auto, clear
          regress price mpg trunk length turn if foreign==0
          estimates store domestic
          regress price mpg trunk length turn if foreign==1
          estimates store foreign
          coefplot (domestic, drop(_cons) mlabel(cond(@pval<.99, string(@b,"%4.2f"), "")) mlabpos(10) if(@b < 0)) ///
          (domestic, drop(_cons) mlabel(cond(@pval<.99, string(@b,"%4.2f"), "")) mlabpos(2) if(@b >= 0)) ///
          (foreign, drop(_cons) mlabel(cond(@pval<.99, string(@b,"%4.2f"), "")) mlabpos(10) if(@b < 0)) ///
          (foreign, drop(_cons) mlabel(cond(@pval<.99, string(@b,"%4.2f"), "")) mlabpos(2) if(@b >= 0)) ///
          , xline(0)
          I'm using StataNow/MP 18.5

          Comment


          • #6
            The following code modifies #5 to improve spacing, coloring and code clarity, and incorporates the marker label code from #4:
            Code:
            clear all
            sysuse auto, clear
            regress price mpg trunk length turn if foreign==0
            estimates store domestic
            regress price mpg trunk length turn if foreign==1
            estimates store foreign
            
            local gen_opts drop(_cons) mc(black) mlabel(cond(@pval<.05,string(@b,"%5.2f")+cond(@pval<.001, "***",cond(@pval<.01, "**",cond(@pval<.05, "*",""))),""))
            local dom_opts ciopts(lcolor(stc1)) offset(+0.1)
            local for_opts ciopts(lcolor(stc2)) offset(-0.1)
            local neg_opts if(@b < 0) mlabpos(10) key(ci)
            local pos_opts if(@b >= 0) mlabpos(2) nokey
            
            coefplot ///
                (domestic, `gen_opts' `neg_opts' `dom_opts' ) ///
                (domestic, `gen_opts' `pos_opts' `dom_opts' ) ///
                (foreign, `gen_opts' `neg_opts' `for_opts' ) ///
                (foreign, `gen_opts' `pos_opts' `for_opts') ///
                , xline(0)
            This produces:
            Click image for larger version

Name:	Screenshot 2025-08-13 at 11.15.17 PM.png
Views:	1
Size:	107.4 KB
ID:	1780974

            Last edited by Hemanshu Kumar; 13 Aug 2025, 11:46.

            Comment

            Working...
            X