Hello,
I am plotting a series plot using sgplot. The group variable is subject. I am getting a nice plot with different subject plotted in different color. Now I need to color these subject (lines) by treatment. I still want to use subject as group variable but now I want to color the lines on graph as per the treatment. Is there any option to do that within sgplot?
On the KEYLEGEND statement, you can set the SORTORDER to be ASCENDING or DESCENDING. If you need precise control over the ordering, there is a way to use discrete attribute maps to do it. Let me know if you need an example of that.
Thanks!
Dan
You didn't mention your version of SAS, but try the tricks and links in this article: "Setting Group Colors"
I am using 9.4. Here is ths partial code
series x=visitn y=lbstresn / group = subjid markers lineattrs = (thickness = 1) markerattrs=(symbol=circlefilled size = 4) ;
By default , SAS picks different color for each subjects. I would like to color these subjects data by treatment.
Ahh, you're trying to display a spaghetti plot. Use
GROUPLC=treatment
For more on spaghetti plots, see "Create spaghetti plots in SAS."
For more on line colors and styles, see "Creating spaghetti plots just got easy."
So the grouplc works but the legend is still displayed based on subject as subject is by group.
How can I display correct legend based on treatment?
I'm not sure of your SAS 9.4 maintenance version, but we added support for extra levels of subgrouping for series plots (starting in SAS9.4m2). In your case, you would use the GROUPLC option:
series x=visitn y=lbstresn / group = subjid grouplc=treatment markers lineattrs = (thickness = 1) markerattrs=(symbol=circlefilled size = 4) ;
Hope that works for you!
Dan
You are only allowed one variable for sgplot grouping. So you need to create a new variable that represents the combination of subjid and treatment:
group = catx(" - ", subjId, treatment);
and then use an attribute map dataset with sgplot to associate groups with graphical attributes. I find it the most reliable method to do so.
The only trick I can think of to achieve that (without the new grouplc option) is to insert missing values between subjects and use the BREAK option so that each treatment is a single series broken into many subject lines.
Use the TYPE= option on the KEYLEGEND statement to choose the attributes to display. In the case of:
series x=visitn y=lbstresn / group = subjid grouplc=treatment markers lineattrs = (thickness = 1)
markerattrs=(symbol=circlefilled size = 4) name="series";
keylegend "series" / type=linecolor; /* because treatment is assigned to GROUPLC */
Thanks Dan. Is it possible the values in legend in a particular order ?
On the KEYLEGEND statement, you can set the SORTORDER to be ASCENDING or DESCENDING. If you need precise control over the ordering, there is a way to use discrete attribute maps to do it. Let me know if you need an example of that.
Thanks!
Dan
Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.
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.
Ready to level-up your skills? Choose your own adventure.