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

 

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?

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
DanH_sas
SAS Super FREQ

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

View solution in original post

11 REPLIES 11
Rick_SAS
SAS Super FREQ

You didn't mention your version of SAS, but try the tricks and links in this article: "Setting Group Colors"

Leo9
Quartz | Level 8

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. 

 

Rick_SAS
SAS Super FREQ

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."

Leo9
Quartz | Level 8

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?

DanH_sas
SAS Super FREQ

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

PGStats
Opal | Level 21

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.

PG
Leo9
Quartz | Level 8
In this case: How you do you define legends at the bottom ? How do you make sure they are based only on treatment ?
PGStats
Opal | Level 21

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.

PG
DanH_sas
SAS Super FREQ

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 */

Leo9
Quartz | Level 8

Thanks Dan. Is it possible the values in legend in a particular order ? 

DanH_sas
SAS Super FREQ

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

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
  • 11 replies
  • 19072 views
  • 4 likes
  • 4 in conversation