Mizzle
Mizzle

Reputation: 757

SAS proc sgplot two separate legends

I run into legend issue with SAS proc sgplot. My code is below. In my first plot, I have legend for height_group. In my second plot, I have legend for ellipse line. I want to draw a plot with both legends. That is a legend for height_group, another legend for ellipse line in one plot. How can I do it? I only got the third plot. But the two legends are too far from each other, I want them to be both at bottom, I also want the color legend the same as the first plot which indicates that this legend is for height_group.

data plot;
set sashelp.class;
if height > 65 then height_group = "high";
else height_group = "low";
run;

data attrmap;
retain id "myid";
infile datalines delimiter=','; 
length value $45 markercolor $10 markersymbol $10;
input value $ markercolor $ markersymbol $ ;
cards;
high, red, Circle
low, blue, X
;
run;

* This is my first plot;
proc sgplot data=plot dattrmap=attrmap;
  scatter x=height y=weight/ group=height_group attrid=myid;
  ellipse x=height y=weight / alpha=.2 name="eighty" legendlabel="80% Prediction";
  ellipse x=height y=weight / alpha=.05  name="ninetyfive" legendlabel="95% Prediction";
  *keylegend "eighty" "ninetyfive";
run;

* This is my second plot;
proc sgplot data=plot dattrmap=attrmap;
  scatter x=height y=weight/ group=height_group attrid=myid;
  ellipse x=height y=weight / alpha=.2 name="eighty" legendlabel="80% Prediction";
  ellipse x=height y=weight / alpha=.05  name="ninetyfive" legendlabel="95% Prediction";
  keylegend "eighty" "ninetyfive";
run;

* This is my third plot;
proc sgplot data=plot dattrmap=attrmap;
  scatter x=height y=weight/ group=height_group attrid=myid name ="color";
  ellipse x=height y=weight / alpha=.2 name="eighty" legendlabel="80% Prediction";
  ellipse x=height y=weight / alpha=.05  name="ninetyfive" legendlabel="95% Prediction";
  keylegend "eighty" "ninetyfive";
  keylegend "color";
run;

Upvotes: 0

Views: 1767

Answers (1)

whymath
whymath

Reputation: 1394

You can use lineattrs= option to control the color of ecllipses. And position= option to control the position of legend.

proc sgplot data=plot dattrmap=attrmap;
  scatter x=height y=weight/ group=height_group attrid=myid name ="color";
  ellipse x=height y=weight / alpha=.2 name="eighty" legendlabel="80% Prediction" lineattrs=(color=red);
  ellipse x=height y=weight / alpha=.05  name="ninetyfive" legendlabel="95% Prediction" lineattrs=(color=blue);
  keylegend "color" "eighty" "ninetyfive"/position=bottom;
run;

By the way, If you split it to two keylegend statment, legends will be located at different position.

Upvotes: 0

Related Questions