Data visualization with SAS programming

How do I change the text in a graph's legend?

Reply
Occasional Contributor
Posts: 13

How do I change the text in a graph's legend?

Hi everyone,

 

I'm doing a Kaplan-Meier survival graph (proc lifetest) with two curves on it (comparing two cancer treatments). The two treatments are listed in the datalines as medication=0 and medication=1. So the graph legend shows the red line as medication=0 and the blue line as medication=1.

OK! Now, I'd like the legend to look a little nicer. Is there a line of code I can add that would basically tell SAS: "In the legend of the graph, replace the term medication=0 with the text 'Green Eggs', and replace the term medication=1 with the text 'Ham' "?

Bogus example, of course - just in case my professor is trolling these forums. But I'm asking how to change the text of the legend.

Any help will be greatly appreciated by this newbie!

Thanks,

Pep

Esteemed Advisor
Posts: 6,878

How do I change the text in a graph's legend?

SAS Super FREQ
Posts: 1,042

How do I change the text in a graph's legend?

I assume you are using a grouped step plot.  You can define a User Defined Format and apply it to the group variable.

proc format;

  value

    0='Green Eggs'

    1='Ham';

run;

proc sgplot data=greeneggsandham;

  format trt v.;

  step x=time y=value / group=trt;

run;

See my blog post on Survival Plots:

Grand Advisor
Posts: 16,304

How do I change the text in a graph's legend?

Apply the format as Sanjay has suggested.

But if you want to change other things on the graph, such as title and axis labels check out this page:

http://support.sas.com/documentation/cdl/en/statug/63962/HTML/default/viewer.htm#statug_templt_a0000...

Also, you didn't say it, but make sure you create your curve using the ODS graphics feature, it looks much better.

Occasional Contributor
Posts: 13

How do I change the text in a graph's legend?

You guys are great! Thank you!

I'm obviously still doing something wrong, though... So I'm pasting my code below - can you tell me what I'm doing wrong?

Thanks!

Pep

'assayresult = 1 = IH positive
'assayresult = 0 = IH negative
'survstatus = 1 = died
'survstatus = 0 = censored


'So use the code below for SAS:

DATA Sedmak;
INPUT assayresult time survstatus;
DATALINES;
0 19 1
0 25 1
0 30 1
0 34 1
0 37 1
0 46 1
0 47 1
0 51 1
0 56 1
0 57 1
0 61 1
0 66 1
0 67 1
0 74 1
0 78 1
0 86 1
0 122 0
0 123 0
0 130 0
0 130 0
0 133 0
0 134 0
0 136 0
0 141 0
0 143 0
0 148 0
0 151 0
0 152 0
0 153 0
0 154 0
0 156 0
0 162 0
0 164 0
0 165 0
0 182 0
0 189 0
1 22 1
1 23 1
1 38 1
1 42 1
1 73 1
1 77 1
1 89 1
1 115 1
1 144 0
;

ods rtf;

proc format;
  value assayresult 
    0='IH-Negative'
    1='IH-Positive';
run;

proc sgplot data=Sedmak;
  format assayresult;
  step x=time y=survstatus / group=assayresult;
run;


PROC LIFETEST DATA=Sedmak METHOD=KM PLOT=(S);
TIME time*survstatus(0);
STRATA assayresult;
TEST;
RUN;
ods rtf close;

SAS Super FREQ
Posts: 1,042

How do I change the text in a graph's legend?

While it is not clear what you are looking for, If you just want to change the values shown in the legend, you only need to apply the format to the class variable before (or in) the LIFETEST procedure.  You don't need proc SGPLOT at all.  The Survival plot drawn by the LIFETEST proc will show the formatted values in the legend.

proc format;
  value assayresult 
    0='IH-Negative'
    1='IH-Positive';
run;

ods graphics on;

PROC LIFETEST DATA=Sedmak METHOD=KM PLOT=(S);
format assayresult assayresult.;
TIME time*survstatus(0);
STRATA assayresult;
TEST;
RUN;

Occasional Contributor
Posts: 13

How do I change the text in a graph's legend?

Thank you so much!

Pep

Post a Question
Discussion Stats
  • 6 replies
  • 252 views
  • 0 likes
  • 4 in conversation