Data visualization with SAS programming

PROC SGPLOT bygroup color

Accepted Solution Solved
Reply
Contributor
Posts: 41
Accepted Solution

PROC SGPLOT bygroup color

 

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?

 

 


Accepted Solutions
Solution
‎07-20-2016 12:32 PM
SAS Super FREQ
Posts: 939

Re: PROC SGPLOT bygroup color

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


All Replies
SAS Super FREQ
Posts: 3,755

Re: PROC SGPLOT bygroup color

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

Contributor
Posts: 41

Re: PROC SGPLOT bygroup color

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. 

 

SAS Super FREQ
Posts: 3,755

Re: PROC SGPLOT bygroup color

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

Contributor
Posts: 41

Re: PROC SGPLOT bygroup color

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?

SAS Super FREQ
Posts: 939

Re: PROC SGPLOT bygroup color

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

Respected Advisor
Posts: 4,934

Re: PROC SGPLOT bygroup color

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
Contributor
Posts: 41

Re: PROC SGPLOT bygroup color

In this case: How you do you define legends at the bottom ? How do you make sure they are based only on treatment ?
Respected Advisor
Posts: 4,934

Re: PROC SGPLOT bygroup color

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
SAS Super FREQ
Posts: 939

Re: PROC SGPLOT bygroup color

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

Contributor
Posts: 41

Re: PROC SGPLOT bygroup color

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

Solution
‎07-20-2016 12:32 PM
SAS Super FREQ
Posts: 939

Re: PROC SGPLOT bygroup color

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

☑ This topic is solved.

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

Discussion stats
  • 11 replies
  • 1112 views
  • 3 likes
  • 4 in conversation