Announcement

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

  • Legend is overwritten by -addplot or -marginsplot

    I have run on a peculiar problem with legends when adding a bar graph to a marginsplot, which does not seem identical to similar problems earlier reported with sts-graph and marginsplot. See
    https://www.stata.com/statalist/archive/2011-02/msg00343.html
    https://www.stata.com/statalist/arch.../msg00972.html

    I have finally succeeded to graph an ordinary bar chart on top of an marginsplot (the other way around sadly does not work). I have offset the x-variable - a categorical variable - in order to not having the bar chart covering the marginsplot.

    I added the legend option - and I can see it taking effect - but the resulting legend is immediately overwritten by a - quite insufficient - standard legend in the final result.

    I have tried numerous different order numbers up to 9 to no avail.

    Code:
    replace ckdgroupOffset = ckdgroup - 0.2 //The x-variable
    
    logistic diadys2 i.ckdgroup predictor variables...
    margins i.ckdgroup
    
    marginsplot, recast(scatter) recastci(rspike) title("Diastolic dysfunction - adjusted predictions and actual data") ///
    addplot(bar meandiadys ckdgroupOffset, yaxis(2) barw(0.2) xscale(range(-0.5 3.2)) xtitle("") fi(inten40) ///
    yscale(range(0 0.7) axis(2)) yla(0 0.1 "10" 0.2 "20" 0.3 "30" 0.4 "40" 0.5 "50" 0.6 "60" 0.7 "70", axis(2)) ytitle("%", axis(2)) ///
    yscale(range(0 0.7) alt) yla(0(0.1)0.7) ytitle(Probability)) legend(order(2 "DDF empirical fraction" 1 "DDF probability"))
    Anyone who has run in a similar problem? It frankly seems like a bug.

    Best regards Nino Landler, MD
    .

    Click image for larger version

Name:	BarPred.png
Views:	2
Size:	27.3 KB
ID:	1578160
    Attached Files

  • #2
    As recommended in the FAQs, you need to include a reproducible example to increase your chances of obtaining a helpful reply.

    Comment


    • #3
      Thank you for commenting. I presume I then would have to reproduce the error with a publicly available dataset and repost the code. I will have to look into that matter.
      Regards

      Comment


      • #4
        Yes, a publicly available dataset is fine. You can find examples at

        Code:
        help margins

        Comment


        • #5
          I have now found the reason for the malfunction, which is - as usual - a subtle error in code:
          My original code here with the famous automobile data:

          Code:
          sysuse auto2.dta
          gen rep78Offset = rep78 -0.2 //for offsetting bar graphs
          egen meanforeign = mean(foreign), by(rep78) //make percentage data for bar graph
          
          logistic foreign i.rep78
          
          marginsplot, recast(scatter) recastci(rspike) title("Title") ///
              addplot(bar meanforeign rep78Offset, barw(0.2) yaxis(2) ///
              yscale(range(0(0.2)1.1)) yscale(range(0(0.2)1.1) alt axis(2)) ///
              ytitle(Probability) ytitle(%, axis(2))) legend(order(3 "empirical fraction"  2 "probability" 1 "95% CI") rows(1)) 
          Note the parentheses: this places the legend option outside -addplot as an option to -marginsplot, which means it gets overwritten by -addplot. The simple solution is to place it "innermost"

          Code:
           marginsplot, recast(scatter) recastci(rspike) title("Title") ///
              addplot(bar meanforeign rep78Offset, legend(order(3 "empirical fraction"  2 "probability" 1 "95% CI") rows(1)) barw(0.2) yaxis(2) ///
              yscale(range(0(0.2)1.1)) yscale(range(0(0.2)1.1) alt axis(2)) ///
              ytitle(Probability) ytitle(%, axis(2)))
          Note further, there are 3 legend elements (Confidence intervals) contrary to what the standard legend shows. Ignoring this fact can also cause problems in legend labeling.

          Well, I learned something again - hope it is also helpful to others.

          Cheers

          Comment

          Working...
          X