Data visualization with SAS programming

legend in sgplot

Reply
Contributor
Posts: 56

legend in sgplot

Dear all,

sgplot automatically shows up the legend in the output of the graph.

Say in my plot, there are 3 lines, total, female, male.

What if in the legend, I only want the legend for female and male, but suppress the legend for total?

Thanks and Regards

Fred
N/A
Posts: 0

Re: legend in sgplot

Try this:

proc sgplot data=sashelp.stocks
(where=(date >= "01jan2000"d and stock = "IBM"));
title "Stock Trend";

series x=date y=close / name="A";
series x=date y=low / name="B";
series x=date y=high / name="C";
keylegend "A" "B";
run;
quit;
Contributor
Posts: 56

Re: legend in sgplot

I know this way, but some plots are with many groups, i just dont wanna type them one by one.

I am thinking of stick on the vline statement

vline x / response = y group=grp;

Under this circumstance, anyway to do it?

Thanks
SAS Employee
Posts: 967

Re: legend in sgplot

And if sgplot doesn't work out for you, here's how to do it with traditional SAS/Graph proc gplot:


legend1 label=none order=('Low' 'High');

symbol1 value=dot height=.7pct interpol=none c=gray;
symbol2 value=none interpol=join c=green;
symbol3 value=none interpol=join c=brown;

proc gplot data=sashelp.stocks
(where=(date >= "01jan2000"d and stock = "IBM"));
title "Stock Trend";
plot close*date=1 low*date=2 high*date=3 / overlay
legend=legend1;
run;
Contributor
Posts: 56

Re: legend in sgplot

my first exposure to SAS graphics is with sg procedures, so i dont know much about traditional SAS/graph procedures. And the output using this is not nice as well.

All my previous works on plots were done in R or Matlab. Since SG procedures are really nice, i try to stick on it for later work.

Thanks.
SAS Super FREQ
Posts: 890

Re: legend in sgplot

Is your goal to eliminate a group value from your legend?
Contributor
Posts: 56

Re: legend in sgplot

Yes, under Vline statement, there will be several plots over time for the groups.

say, total, female, and male. In the legend, I only wanna keep female and male, but eliminate total.

Anyways???

Thanks
SAS Super FREQ
Posts: 1,081

Re: legend in sgplot

For SAS 9.2, there is no way to remove an item from the legend. You just have to avoid adding it as suggested by Karel. You will need to separate out the groups you don't want in the legend. So, if you want group A, B and C, but not D and E, then you need two VLINE statements, with separate columns. Then, add only the one that you want to the KEYLEGEND statement. By default, CYCLEATTRS will still increment the attributes for each curve.
Contributor
Posts: 56

Re: legend in sgplot

Thanks for the reply.

Because there are several variables with different groups in the dataset. I need the calculate the proportion change for each group in each variable. Therefore I developed a macro to calculate , and plot each variable with different group at once.

series statement definitely is not a nice way for my purpose, as I have to input the groups one by one for each variable.

As there is no way to eliminate it.. then I will leave it.

Thanks..
Contributor
Posts: 56

Re: legend in sgplot

Also by the way,

for controlling the font in the graphics, is there anyway to handle such change for xaxis, yaxis, legend separtely?

So far I only can find graphlabetext, graphvaluetext and etc., but they change all of them simultaneously.
SAS Super FREQ
Posts: 1,081

Re: legend in sgplot

SG procedures are designed to surface the most commonly used features of Graph Template Language (GTL) in a procedure syntax. They do not surface all features from GTL. To customize each axis fonts individually, you have to use GTL. As a start, use the procedure option TMPLOUT=filename to get the GTL syntax for your SGPLOT program.

Then, on LAYOUT OVERLAY statement, you can set XAXISOPTS and YAXISOPTS suboptions to set the LABELATTRS= and TICKVALUEATTRS=. Within these options you can set the various font attributes.

On the DISCRETELEGEND statement, you can set TITLEATTRS= and VALUEATTRS= to get the customizations you need.

Note: in GTL, your VLINE statement will be transcoded as SERIESPLOT statement. If you are not familiar with GTL, there are many papers on support web site to get you started. Also, see the GTL users guide under the SAS/GRAPH doc.
Contributor
Posts: 56

Re: legend in sgplot

thank u very much indeed
N/A
Posts: 0

Re: legend in sgplot

Maybe this way :-)

data xxx;
set sashelp.stocks
(where=(date >= "01jan2000"d and stock = "IBM"));
grp = "close"; y = close; output;
grp = "low" ; y = low; output;
grp = "close"; z = high; output; /* "high" group masked as "close"*/
run;

proc sort data=xxx;
by grp date;
run;

proc sgplot data=xxx;
title "Stock Trend";
vline date / response = y group=grp;
vline date / response = z group=grp lineattrs=(color=red);
run;
quit;
Ask a Question
Discussion stats
  • 12 replies
  • 3333 views
  • 0 likes
  • 5 in conversation