Announcement

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

  • Lines get lost in legend keys when adding confidence intervals to sts graph plots

    Hi,
    I'm trying to produce a simple KM graph using the following commands:
    Code:
    sts graph, by(PPI) tmax(9) ci plot1opts(lcolor(pink)) plot2opts(lcolor(navy)) ///
        yscale(noline) ylabel(.3(.1)1,angle(hor) noticks format(%6.2f)) xlabel(0(1)9, noticks) xtitle("Anni di follow-up", margin(top)) ///
        legend(order(1 "no PPI" 2 "PPI") cols(1) pos(5) ring(0)) title("Curve di sopravvivenza Kaplan-Meier" "outcome = decesso", margin(bottom)) ///
        plotregion(lcolor(none)) name(KMdeath, replace)
    And I get the following graph where, as you may see, the legend keys don't allow you to identify the curves. The keys changed in this way when I added the ci option; not using it, everything works fine. The 'no PPI' curve is the upper with the pink line.

    Click image for larger version

Name:	KMdeath.png
Views:	1
Size:	38.9 KB
ID:	1359914

    I'm using Stata v.13.1. Can anyone please help?

  • #2
    Try replacing
    Code:
    legend(order(1 "no PPI" 2 "PPI") cols(1) pos(5) ring(0))
    with
    Code:
    legend(order(2 "no PPI" 4 "PPI") cols(1) pos(5) ring(0))
    or, better, just turn off the legend and label the individual plots on the graph itself; see
    Code:
    help added_text_options
    for how.

    Comment


    • #3
      thanks a lot Joseph!
      I followed your suggestion to use added text. The trick on the legend order didn't work well, because I got two lines instead of a band in the legend key, but they were both grey, not pink and navy.
      Anyway, this looks like a small flaw of Stata.

      Comment


      • #4
        I realize this is an old post, but I did not find the answer very satisfying.

        This is not an error in Stata just in code.

        First of all - in the above example there are actually 4 legend elements: 2 for cofindence interval lines and 2 for lines. The code presented in the question addresses only 2 elements and omits elements 3 and 4, thus only 1 line is presented in the legend: the blue square is the legend for CI of the blue line, which is mislabeled by the code.

        Additionally, I experienced a related problem.
        I wanted to change text of the legend of a sts graph (cumulative hazard) with confidence intervals and found that I could only address the entries regarding the confidence intervals, not the ones regarding the lines.
        Unlike in the example presented by Mr Gibertoni, i used the label option, not the order option - and failed. I tested this systematically: the automatically assigned order to the elements in the legend somehow did not allow for labeling the desired entries. I could label all odd order numbers (legend elements for confidence intevals) but not the even numbers, which should have pointed to the line legend.

        However, i found that by using both the order option and the label option I could change the legend labels just fine. In the above example this would mean

        Code:
         sts graph, by(PPI) ...///     legend(order(1 2 3 4) label(2 "no PPI" 4 "PPI") ....
        This will produce a legend with confidence interval 1 legend 1 first then line 1 legend, then CI 2 etc. Only the labels of the lines changed, CI are labeled automatically by stst graph.


        Hope this was helpful.
        Last edited by Nino Landler; 27 Feb 2020, 02:40.

        Comment


        • #5
          Cannot seem to edit my previous post anymore, but felt the urge to clarify something.

          It is correct, that there was an error in the code of the questioner as i wrote earlier. But this error is actually addressed in the first answer by mr. Coveney. The code he proposes should have worked, but maybe the result did not look quite the way it was supposed to. I found that using only the order option i can label the legend correctly. So my conclusion is, that using the label option for legends does not work that well - at least not if the order option is not also specified. Labels can be incorporated in the order option, which also can change the order of appearance in the legend. Also, this option can be used to omit elements in the legend. One has just to figure out, how many elements there are in the first place.

          Cheers.

          Comment

          Working...
          X