BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mcs
Obsidian | Level 7 mcs
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
Jay54
Meteorite | Level 14

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

5 REPLIES 5
ballardw
Super User

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

 

keylegend 'act'  'pred';

mcs
Obsidian | Level 7 mcs
Obsidian | Level 7
> 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.
Jay54
Meteorite | Level 14

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.

 

 

mcs
Obsidian | Level 7 mcs
Obsidian | Level 7

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.

 

DanH_sas
SAS Super FREQ

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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