Announcement

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

  • Graphs: Putting means in legend box

    I am using Stata 16.1/SE on Windows 10. I would like to put means in the legend box:

    Click image for larger version

Name:	edited_output.png
Views:	1
Size:	23.2 KB
ID:	1538387

    I am currently using the coefplot command from SSC:
    Code:
    ssc install coefplot, replace
    In the MWE below (output attached), I output a plot with two coefficients on price. I am fine using a command other than coefplot if needbe.
    Code:
    * start fresh
    clear all
    
    * auto dataset
    sysuse auto.dta
    
    * regression 1: effect of mpg on price
    summarize price
    local rmean = r(mean)
    regress price mpg
    estimates store reg1
    estadd local mean "`rmean'"
    
    * regression 2: conditional on a domestic car, effect of mpg on price
    summarize price if foreign == 0
    local rmean = r(mean)
    regress price mpg if foreign == 0
    estimates store reg2
    estadd local mean "`rmean'"
    
    * plot
    coefplot ///
        (reg1, label("Regression 1") keep(mpg)) ///
        (reg2, label("Regression 2") keep(mpg)) ///
        , ///
        vertical recast(bar) barwidth(0.2) fcolor(*.5)
    Click image for larger version

Name:	output.png
Views:	1
Size:	26.7 KB
ID:	1538388

  • #2
    I do not see the sense in that. Why not place it within the bar or somewhere in the plot region? Otherwise, I think that you will need to program your own legend and place it somewhere in the plot region, turning off the default. I am not volunteering.

    Comment


    • #3
      I agree it's not a standard choice, but if it is what you want, can't you just call it directly?:

      Code:
      * start fresh
      clear all
      
      * auto dataset
      sysuse auto.dta
      
      * regression 1: effect of mpg on price
      summarize price
      local rmean1 = r(mean)
      regress price mpg
      estimates store reg1
      estadd local mean "`rmean1'"
      
      * regression 2: conditional on a domestic car, effect of mpg on price
      summarize price if foreign == 0
      local rmean2 = r(mean)
      regress price mpg if foreign == 0
      estimates store reg2
      estadd local mean "`rmean2'"
      
      * plot
      coefplot ///
          (reg1, label("`rmean1' Regression 1") keep(mpg)) ///
          (reg2, label("`rmean2' Regression 2") keep(mpg)) ///
          , ///
          vertical recast(bar) barwidth(0.2) fcolor(*.5)

      Comment


      • #4
        Or you literally want to overlay the value over the colored bar in the legend? If so, I would do it once in graph edit (hit record before you start), plug the code generated in to the bottom of your do file, then edit so the value passed is coming from your local.

        Comment


        • #5
          Andy and Mike:

          My team was thinking of presenting the graph this way.

          Mike:

          We did in fact want the mean to actually go inside the colored boxes in the legend. Thank you for your idea! I tried plugging in the generated .grec file, but it doesn't seem to work with in the do-file. Below is the .grec file.

          Code:
          StataFileTM:00001:01100:GREC:                          :
          00007:00007:00001:
          *! classname: twowaygraph_g
          *! family: twoway
          *! date: 26 Feb 2020
          *! time: 10:52:48
          *! graph_scheme: s1color
          *! naturallywhite: 1
          *! end
          
          // File created by Graph Editor Recorder.
          // Edit only if you know what you are doing.
          
          .legend.plotregion1.AddTextBox added_text editor 1.975317410956458 .1151210947388776
          .legend.plotregion1.added_text_new = 1
          .legend.plotregion1.added_text_rec = 1
          .legend.plotregion1.added_text[1].style.editstyle  angle(default) size( sztype(relative) val(3.4722) allow_pct(1)) color(black) horizontal(left) vertical(middle) margin( gleft( sztype(relative) val(0) allow_pct(1)) gright( sztype(relative) val(0) allow_pct(1)) gtop( sztype(relative) val(0) allow_pct(1)) gbottom( sztype(relative) val(0) allow_pct(1))) linegap( sztype(relative) val(0) allow_pct(1)) drawbox(no) boxmargin( gleft( sztype(relative) val(0) allow_pct(1)) gright( sztype(relative) val(0) allow_pct(1)) gtop( sztype(relative) val(0) allow_pct(1)) gbottom( sztype(relative) val(0) allow_pct(1))) fillcolor(bluishgray) linestyle( width( sztype(relative) val(.2) allow_pct(1)) color(black) pattern(solid) align(inside)) box_alignment(east) editcopy
          .legend.plotregion1.added_text[1].text = {}
          .legend.plotregion1.added_text[1].text.Arrpush MEAN 1
          // editor text[1] edits
          
          .legend.plotregion1.AddTextBox added_text editor 1.74822526065211 41.55943852528203
          .legend.plotregion1.added_text_new = 2
          .legend.plotregion1.added_text_rec = 2
          .legend.plotregion1.added_text[2].style.editstyle  angle(default) size( sztype(relative) val(3.4722) allow_pct(1)) color(black) horizontal(left) vertical(middle) margin( gleft( sztype(relative) val(0) allow_pct(1)) gright( sztype(relative) val(0) allow_pct(1)) gtop( sztype(relative) val(0) allow_pct(1)) gbottom( sztype(relative) val(0) allow_pct(1))) linegap( sztype(relative) val(0) allow_pct(1)) drawbox(no) boxmargin( gleft( sztype(relative) val(0) allow_pct(1)) gright( sztype(relative) val(0) allow_pct(1)) gtop( sztype(relative) val(0) allow_pct(1)) gbottom( sztype(relative) val(0) allow_pct(1))) fillcolor(bluishgray) linestyle( width( sztype(relative) val(.2) allow_pct(1)) color(black) pattern(solid) align(inside)) box_alignment(east) editcopy
          .legend.plotregion1.added_text[2].text = {}
          .legend.plotregion1.added_text[2].text.Arrpush MEAN 2
          // editor text[2] edits
          
          
          // <end>
          And here is the output (the making of which was recorded from which the .grec was generated):

          Click image for larger version

Name:	test.png
Views:	1
Size:	44.7 KB
ID:	1538586

          Comment


          • #6
            I don't see an attempt to feed the values into the colored boxes in the legend. The second mean below is not centered, so you may need to investigate what controls alignment, but here is the general idea. I would rather spend my time talking out the other team members of this idea.

            Code:
            * start fresh
            clear all
            
            * auto dataset
            sysuse auto.dta
            
            * regression 1: effect of mpg on price
            summarize price
            local rmean1: di %9.2f `=r(mean)'
            regress price mpg
            estimates store reg1
            estadd local mean "`rmean1'"
            
            * regression 2: conditional on a domestic car, effect of mpg on price
            summarize price if foreign == 0
            local rmean2: di %9.2f `=r(mean)'
            regress price mpg if foreign == 0
            estimates store reg2
            local mean "`rmean2'"
            
            * plot
            coefplot ///
                (reg1, label("Regression 1") keep(mpg)) ///
                (reg2, label("Regression 2") keep(mpg)) ///
                , ///
                vertical recast(bar) barwidth(0.2) fcolor(*.5) scheme(s1color)
            gr_edit .legend.plotregion1.AddTextBox added_text editor 1.975317410956458 .1151210947388776
            gr_edit .legend.plotregion1.added_text_new = 1
            gr_edit .legend.plotregion1.added_text_rec = 1
            gr_edit .legend.plotregion1.added_text[1].style.editstyle  angle(default) size( sztype(relative) val(3.4722) allow_pct(1)) color(black) horizontal(left) vertical(middle) margin( gleft( sztype(relative) val(0) allow_pct(1)) gright( sztype(relative) val(0) allow_pct(1)) gtop( sztype(relative) val(0) allow_pct(1)) gbottom( sztype(relative) val(0) allow_pct(1))) linegap( sztype(relative) val(0) allow_pct(1)) drawbox(no) boxmargin( gleft( sztype(relative) val(0) allow_pct(1)) gright( sztype(relative) val(0) allow_pct(1)) gtop( sztype(relative) val(0) allow_pct(1)) gbottom( sztype(relative) val(0) allow_pct(1))) fillcolor(bluishgray) linestyle( width( sztype(relative) val(.2) allow_pct(1)) color(black) pattern(solid) align(inside)) box_alignment(east) editcopy
            gr_edit .legend.plotregion1.added_text[1].text = {}
            gr_edit .legend.plotregion1.added_text[1].text.Arrpush  `=`rmean1'' 
            // editor text[1] edits
            
            gr_edit .legend.plotregion1.AddTextBox added_text editor 1.74822526065211 41.55943852528203
            gr_edit .legend.plotregion1.added_text_new = 2
            gr_edit .legend.plotregion1.added_text_rec = 2
            gr_edit .legend.plotregion1.added_text[1].style.editstyle  angle(default) size( sztype(relative) val(3.4722) allow_pct(1)) color(black) horizontal(left) vertical(middle) margin( gleft( sztype(relative) val(0) allow_pct(1)) gright( sztype(relative) val(0) allow_pct(1)) gtop( sztype(relative) val(0) allow_pct(1)) gbottom( sztype(relative) val(0) allow_pct(1))) linegap( sztype(relative) val(0) allow_pct(1)) drawbox(no) boxmargin( gleft( sztype(relative) val(0) allow_pct(1)) gright( sztype(relative) val(0) allow_pct(1)) gtop( sztype(relative) val(0) allow_pct(1)) gbottom( sztype(relative) val(0) allow_pct(1))) fillcolor(bluishgray) linestyle( width( sztype(relative) val(.2) allow_pct(1)) color(black) pattern(solid) align(inside)) box_alignment(east) editcopy
            gr_edit .legend.plotregion1.added_text[2].text = {}
            gr_edit .legend.plotregion1.added_text[2].text.Arrpush  `=`rmean2''  
            // editor text[2] edits
            Click image for larger version

Name:	Graph.png
Views:	1
Size:	15.1 KB
ID:	1538608

            Comment


            • #7
              Andrew:

              Thank you so much, I tried it, this works! The off-center label might be a computer-specific thing; the numbers were a lot better on my end. I didn't know about the gr_edit command.

              Thank you!

              Comment

              Working...
              X