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
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.
Did you try with the names of both graphs on the Keylegend statement?
keylegend 'act' 'pred';
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.
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.
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
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.