Announcement

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

  • Coding so nelson-aalen curve legend display value label and not variable name and value label

    Hi! I am generating a quite large quantity of nelson-aalen curves and I want to find a way to write my code so that the output does not show for example: variable1= 1-4 in the legend but only "1-4". It is very time consuming to edit this manually for every graph

    I have tried so many ways that have been suggested here but neither works properly. Does anyone have a suggestion?

    This is the code used for the nelson-aalen curves, which does not include any coding for the way i want the legend to be:
    sts graph, by(variable1) cumhaz ci ylabel(0(0.1)0.3) xlabel(0(5)34) title("XXXX")

  • #2
    your question is not very clear to me but it sounds like you are having a problem with the legend; if correct, see
    Code:
    help legend_options

    Comment


    • #3
      Hi!
      sorry for the unclarity - yes exactly it is a problem with the legend and i have read the stata info about coding for the legend but it does not work. I have tried the following and it does not work:

      sts graph, by(variable1) cumhaz ci ylabel(0(0.1)0.3) xlabel(0(5)34) title("XXXX") legend(label (1 ”1-4”) label(2 ”5-9”) label(3 ”9-13”) label(4”13-17”))

      it is the values of variable1 that i want displayed in the legend instead of variable1=1-4 as is now presented in the legend

      Comment


      • #4
        what exactly do you mean by "does not work"? e.g., do you mean it gives you nothing or do you mean it gives you something you don't want or do you mean the labels overlap or ...? please read the FAQ which has lots of good advice on how to ask questions

        Comment


        • #5
          See https://www.statalist.org/forums/for...ing-legend-lab for a similar problem. If you want to assign your own labels, use the -order()- option within -legend()-. Note that in Nelson-Aalen cumulative hazard estimates graphs with CIs, the CIs take odd numbers in the legend, so you need to assign labels for even numbers.

          Code:
          webuse drug2, clear
          set scheme s1color
          sts graph, cumhaz by(drug) ci legend(order(1 2 "1-4" 3 4 "5-9"))
          Res.:
          Click image for larger version

Name:	Graph.png
Views:	1
Size:	23.9 KB
ID:	1672432





          If your issue is just to remove the default "outcome=" from the legend, I would recommend looping and saving the legend text, then removing the "outcome=" part and finally replacing the legend text. You can use the undocumented gr_edit command, where below you change the upper limit of the loop index to be equal or greater than the number of plots.

          Code:
          webuse drug2, clear
          set scheme s1color
          sts graph, cumhaz by(drug) ci saving(gr1, replace) title("")
          forval i=1/10{
              local text= ustrregexra("`.Graph.legend.plotregion1.label[`i'].text[1]'", "(\w+\s+)(\=\s+)(\w+)", "$3")
              cap gr_edit .legend.plotregion1.label[`i'].text = {}
              cap gr_edit .legend.plotregion1.label[`i'].text.Arrpush "`text'"
          }
          gr save gr2, replace
          gr combine gr1.gph gr2.gph
          Click image for larger version

Name:	Graph.png
Views:	1
Size:	23.2 KB
ID:	1672433

          Last edited by Andrew Musau; 06 Jul 2022, 12:53.

          Comment


          • #6
            Hi Andrew, thank you so much for the clear and informative reply! The first option using order worked to change the text in the legend Knowing what you said about the CI:s taking on odd values also made the label option work finally - THANK YOU!

            Comment


            • #7
              Hi again! i was wondering how to change the legend when using the order command so that the confidence intervals are one the top row vertically and underneath the labels (as in Andrew's second figure). Currently when I use the order command the legend looks like this:
              Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	31.4 KB
ID:	1672575


              but i would like it to look like this:
              Click image for larger version

Name:	Capture2.PNG
Views:	1
Size:	12.9 KB
ID:	1672576


              I have tried using rows and cols commands but that does not seem to fix the problem. Thank you!

              Comment


              • #8
                -order()- actually specifies order. You want

                Code:
                webuse drug2, clear
                set seed 07072022
                set scheme s1color
                g d4= runiformint(1,4)
                sts graph, cumhaz by(d4) ci legend(order(1 3 5 7 2 "1 - 4" 4 "5 - 9" 6 "9 -13" 8 "13-17"))
                Click image for larger version

Name:	Graph.png
Views:	1
Size:	25.1 KB
ID:	1672581

                Comment


                • #9
                  Hi again, wow thank you so much that worked perfectly!

                  Comment

                  Working...
                  X