Data visualization with SAS programming

Legend on Gplot are repeating on each page.

Reply
Occasional Contributor
Posts: 5

Legend on Gplot are repeating on each page.

Hello

Legend on Gplot are repeating on each page no matter the plot lines do not have all the strata on each page.

&g_strata=A, B, C

&g_pageby=1, 2 (1= A and B treatments; 2= C treatment)

legend1= Legends for A, B, C are present

PROC GPLOT DATA=km4 uniform;

by &g_pageby ;

   PLOT survival*&g_time=&g_strata/ annotate=annot HAXIS=axis1 VAXIS=axis2 HMINOR=4 legend=legend1; 

symbol1 i=stepj c=&color1 l=1 v=none;

symbol2 i=none  c=&color1 l=2 v=dot;

symbol3 i=stepj c=&color2 l=3 v=none;

symbol4 i=none  c=&color2 l=4 v=circle;

symbol5 i=stepj c=&color3 l=5 v=none;

symbol6 i=none  c=&color3 l=6 v=star;

RUN;   

When I ren the below code I see the plot with 2 pages with Page 1 having A and B plot lines and Page2 having C plot line with distinct colors and symbols.

However, the legend on page 1 has A, B and C and page 2 also has legends on A, B and C treatment groups.

I want only legend of A and B to appear on page 1 and legend of C only to appear on page 2. How do I get this ?

I am using SAS 9.2

Thanks, Ravi

SAS Super FREQ
Posts: 925

Legend on Gplot are repeating on each page.

PROC SGPLOT is a possible option for you. Run the little sample below and see if the legend appears as you would expect. Just looking at your symbol statements, it appears that you have intertwined your censored data with your strata data. Use a where clause to filter out those observation for the test below. Note that your SYMBOL, AXIS, and LEGEND statements do not apply to this procedure. Those types of options are specified directly in the procedure syntax (see the documentation for more details).

proc sgplot data=km4 uniform=all;

  step x=&g_time y=survival / group=&g_strata;

run;

If you like the result, you can put your censored data in separate columns and add them using using a SCATTER overlay:

proc sgplot data=km4 uniform=all;

  step x=&g_time y=survival / group=&g_strata;

  scatter x=&g_time2 y=censored / group=&g_strata2;

run;

Let me now if this works for you.

Thanks!
Dan

SAS Employee
Posts: 980

Legend on Gplot are repeating on each page.

Ravi,

This legend behavior is a feature of the 'uniform' option that you're using.  The uniform makes all the graphs have uniform axes & legends.  As the doc states:

"UNIFORM forces the assignment of SYMBOL statements for the category variable without regard to the BY-group variable. If a legend is generated, UNIFORM makes the legend the same across graphs."

If you just want uniform axes, and not uniform legends across all the plots, you'll have to remove the 'uniform' option, and then hard-code an axis statement (and use the axis order=) to keep all your axes the same in all the plots.

Here's some short code to demonstrate the original problem/feature, if anybody would like to experiment with it:

data km4;
length strata $1;
input byvar time survival strata;
datalines;
1 1 1.0 a
1 2 1.5 a
1 3 1.2 b
2 1 1.8 a
2 2 2.1 c
2 3 2.0 a
3 1 0.5 c
3 2 1.1 b
3 3 2.5 b
;
run;

symbol1 value=dot height=3 color=red interpol=none;
symbol2 value=triangle height=3 color=blue interpol=none;
symbol3 value=star height=3 color=pink interpol=none;

legend1 position=(top left inside);

proc gplot data=km4 uniform;
by byvar;
plot survival*time=strata /
legend=legend1;
run;

Occasional Contributor
Posts: 5

Legend on Gplot are repeating on each page.

Hi Robert

Taking out the UNIFORM option is giving the legend based on the plot lines in each page. However, the symbol and color on page 2 is the same symbol used in page1. I want the Legend as I get in the below plot and do have distinct color/symbol for each strata. UNIFORM option does give distinct symbol/colors but carries the legend too.   I tweaked the byvar column below. I have 2  categories of myvar and 3 strata. Please let me know.

data km4;
length strata $1;
input byvar time survival strata;
datalines;
1 1 1.0 a
1 2 1.5 a
1 3 1.2 b
1 1 1.8 a
2 2 2.1 c
1 3 2.0 a
2 1 0.5 c
1 2 1.1 b
1 3 2.5 b
;
run;

proc sort data=km4;
  by byvar;
run;

symbol1 value=dot height=3 color=red interpol=none;
symbol2 value=triangle height=3 color=blue interpol=none;
symbol3 value=star height=3 color=pink interpol=none;


legend1 position=(top left inside);


proc gplot data=km4  ;
by byvar;
plot survival*time=strata /legend=legend1;
run;

SAS Employee
Posts: 980

Legend on Gplot are repeating on each page.

Hypothetically, you could get the graphs you want, but it would require a lot of manual coding...

I think you would have to do a separate gplot for each graph (rather than using the 'by' statement).

You'd have to hard-code the desired legend, to keep all the graphs plotted to the same axis range.

And you'd have to manually re-do the symbol statements for each graph, so the desired symbols are used (there's no way in gplot to say "always use this symbol for this data value" - they're just assigned in sequential order to the values being plotted in the given graph).

This might not be too bad for a 1-time thing for a few plots ... but if you've got lots of plots, and/or if you want to re-use the code with other data, this manual process would be very time-consuming (and user-error-prone).

Ask a Question
Discussion stats
  • 4 replies
  • 274 views
  • 0 likes
  • 3 in conversation