Announcement

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

  • Removing the variable name and equals sign from legends without using legend(lab()) ?

    Hi, I am trying to create a series of Kaplan-Meier survival curves based on a series of categorical variables. Each variable's values have labels assigned to them. Accordingly, when I run the code below

    Code:
    local rvar olc omc ouc dlc dmc duc
    foreach i of local rvar {
        sts graph, by(`i')
    }
    I obtain a graph like this
    Click image for larger version

Name:	km-example.JPG
Views:	1
Size:	71.6 KB
ID:	1631642





    How could I go about removing the "olc = " in the legend? I know that I can use legend(lab(1 "Group A") lab(2 "Group B") lab(3 "Group C")) but each of the variables in my loop do not have the same value labels. Accordingly that solution would not work for me. Is there a better way to get what I want rather than run separate lines of code each of the graphs?
    Last edited by Al Adams; 13 Oct 2021, 15:59.

  • #2
    While you have put in some work to frame your question, it still does not satisfy the definition of a reproducible example (see FAQ Advice #12 for what the standard is). Here is a solution based on the graph editor, assuming a maximum of 10 items in the legend. This limit can be adjusted in the code provided.

    Code:
    webuse drug2, clear
    sts graph, by(drug) saving(gr1, replace)
    forval i=1/10{
        cap gr_edit .legend.plotregion1.label[`i'].text = {}
        cap gr_edit .legend.plotregion1.label[`i'].text.Arrpush `=`i'-1'
    }
    gr save gr2, replace
    gr combine gr1.gph gr2.gph
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	24.1 KB
ID:	1631727

    Last edited by Andrew Musau; 14 Oct 2021, 05:56.

    Comment


    • #3
      Great. Thanks for the help, Andrew!

      Comment


      • #4
        #2 assumes that the legend labels take on integer values starting from 0. If this is not the case, see #5 of https://www.statalist.org/forums/for...nd-value-label which shows how to substitute the legend item with the actual text that follows the equal sign.

        Comment

        Working...
        X