Data visualization with SAS programming

PROC SGPLOT legend labels with group option

Accepted Solution Solved
Reply
Contributor mcs
Contributor
Posts: 21
Accepted Solution

PROC SGPLOT legend labels with group option

I'm trying to make a graph that compares actual to predicted lapse rates.

 

proc sgplot;
xaxis label='Age' values=(20 to 64 by 2);
yaxis label='Lapse Rate' tickvalueformat=percent6. max=1;
keylegend 'act';
vline age / response=lapse_rate group=dur_grp name='act' legendlabel='Actual Lapse Rate';
vline age / response=pred_rate group=dur_grp lineattrs=(pattern=dash) name='pred' legendlabel='Predicted Lapse Rate';
run;

Since I'm using the group= option on the vline statements, the legendlabel= option has no effect.

 

 

I'd like viewers to know that the solid lines are actual and the dashed lines are predicted.  How can I get that information into a legend or a label somehow?

 

Martin


Accepted Solutions
Solution
‎09-21-2016 11:55 AM
SAS Super FREQ
Posts: 1,081

Re: PROC SGPLOT legend labels with group option

It would be easier to help if you attach your full program WITH the data so we can see what is going on.  Also, provide the SAS release version you are using.

 

That said, When you use a GROUP role, the unique group values are shown in the legend along with their visual attributes.  LEGENDLABEL is ignored.  In this case you are using the same GROUP variable, so adding both VLINES to legend will only duplicate the information.  If you nave many values for the"dur_grp", using the group variable in two statements will cause use of CYCLEATTRS, and the same group value may get different colors in each statement.  Setting NOCYCLEATTRS may retain the same color for each group, but I suggest you use PATTERN=SOLID for the first VLINE.

 

Now you will have multicolored lines with solid and dash patterns.  You need two legends, one for the pattern (for response), and one for the color (for group).  You can add these by using some tricks, but there is a better way with SAS 9.4. 

 

If you have SAS 9.4, you can use the SERIES plot with the GROUP, GROUPLC and GROUPLP options.  You will need to change your data, with one response var "Rate" with multiple groups for ID (for each curve), Color (for each group) and Pattern (for each response type).  Then, color can represent the "dur_grp" and pattern can represent Lapse or Pred.  See blog article on Spaghetti Plot:  http://blogs.sas.com/content/graphicallyspeaking/2014/08/16/more-on-spaghetti-plots/.  

 

If you do have multiple obs per age + group, you will need to summarize the data prior to using SGPLOT.

 

 

View solution in original post


All Replies
Grand Advisor
Posts: 10,235

Re: PROC SGPLOT legend labels with group option

Did you try with the names of both graphs on the Keylegend statement?

 

keylegend 'act'  'pred';

Contributor mcs
Contributor
Posts: 21

Re: PROC SGPLOT legend labels with group option

> Did you try with the names of both graphs on the Keylegend statement?
Yes. That just doubles the group names, i.e., the legend shows solid lines labeled by the values of the group=variable, and then dashed lines labeled by the same thing.
Solution
‎09-21-2016 11:55 AM
SAS Super FREQ
Posts: 1,081

Re: PROC SGPLOT legend labels with group option

It would be easier to help if you attach your full program WITH the data so we can see what is going on.  Also, provide the SAS release version you are using.

 

That said, When you use a GROUP role, the unique group values are shown in the legend along with their visual attributes.  LEGENDLABEL is ignored.  In this case you are using the same GROUP variable, so adding both VLINES to legend will only duplicate the information.  If you nave many values for the"dur_grp", using the group variable in two statements will cause use of CYCLEATTRS, and the same group value may get different colors in each statement.  Setting NOCYCLEATTRS may retain the same color for each group, but I suggest you use PATTERN=SOLID for the first VLINE.

 

Now you will have multicolored lines with solid and dash patterns.  You need two legends, one for the pattern (for response), and one for the color (for group).  You can add these by using some tricks, but there is a better way with SAS 9.4. 

 

If you have SAS 9.4, you can use the SERIES plot with the GROUP, GROUPLC and GROUPLP options.  You will need to change your data, with one response var "Rate" with multiple groups for ID (for each curve), Color (for each group) and Pattern (for each response type).  Then, color can represent the "dur_grp" and pattern can represent Lapse or Pred.  See blog article on Spaghetti Plot:  http://blogs.sas.com/content/graphicallyspeaking/2014/08/16/more-on-spaghetti-plots/.  

 

If you do have multiple obs per age + group, you will need to summarize the data prior to using SGPLOT.

 

 

Contributor mcs
Contributor
Posts: 21

Re: PROC SGPLOT legend labels with group option

Thanks.  I have 9.4 and was able to get the GROUPLP and GROUPLC options to work for me.

 

I have a couple of questions about fine-tuning the graph.  How can I control the colors of the lines?  I have three colors in my graph, and the default seems to be blue, red, green.  I would prefer red, blue, green in that order.  Also, how can I control the entries in the legend?  It's currently using the values of the GROUPLP and GROUPLC variables.  I would like to specify more easily-readable strings.

 

SAS Super FREQ
Posts: 890

Re: PROC SGPLOT legend labels with group option

For the color ordering, the most straightforward way to do that is using the STYLEATTRS statement in SGPLOT:

 

STYLEATTRS datacontrastcolors=(red blue green);

 

As for the group values, the best way to handle that item is to create user-define formats for those variables using PROC FORMAT and assign the formats to the variables using the FORMAT statement within PROC SGPLOT.

 

Hope this helps!

Dan

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 3395 views
  • 0 likes
  • 4 in conversation