Help using Base SAS procedures

Graph color order

Reply
Contributor
Posts: 36

Graph color order

Hi.

I have the following code:

proc format;
value $yesno 1 = 'Yes' 2 = 'No';
run;

%macro abc(kind);
proc sgplot data = def;
vbar var1 / group = var2_&kind;
format var2_&kind $yesno.;
label var2_v1 = 'label1';
label var2_v2 = 'label2';
label var2_v3 = 'label3';
%mend;

%abc(v1);
%abc(v2);
%abc(v3);

What happens is that for var2_v1 the graph shows the order 'Yes' 'No', but for var2_v2 the graph shows the order 'No' 'Yes', which will lead to that the graph colors get reversed.

Is there a way to force the order or handle this in any way?
Super Contributor
Super Contributor
Posts: 3,174

Re: Graph color order

Although not mentioned specifically in the current DOC (link below), investigate the order of your incoming data observations. Also, I see you are using a CHARACTER-type format rather than numeric, for additional consideration on collating sequence but not likely for the FORMAT you have shown in your post.

Scott Barry
SBBWorks, Inc.

SAS/Graph Statistical Graphics Procedures Guide, The SGPLOT Procedure
http://support.sas.com/documentation/cdl/en/grstatproc/61948/HTML/default/sgplot-chap.htm
Super Contributor
Posts: 291

Re: Graph color order

you might find this link helpful ...

http://support.sas.com/kb/24/874.html
SAS Super FREQ
Posts: 8,744

Re: Graph color order

Hi:
For the SG procedures, if you want to control the order of the items on the X or Y axis, you should investigate the DISCRETEORDER option:
http://support.sas.com/documentation/cdl/en/grstatproc/61948/HTML/default/xaxis-stmt.htm

For example, consider the following SGPLOT which compares the use of DISCRETEORDER=FORMATTED versus DISCRETEORDER=DATA for SASHELP.HEART.

cynthia
[pre]

** 1) DISCRETEORDER=formatted and XAXIS;
proc format;
value newchol low-199 = 'low'
200-high='high';
run;

ods listing;
proc sgplot data = heart;
title '1) FORMATTED order will show HIGH first';
vbar cholesterol ;
xaxis discreteorder=formatted;
format cholesterol newchol.;
run;

** 2) Sort by cholesterol & use format and discreteorder=data;
** so LOW will be first;
proc sort data=sashelp.heart out=heart;
by cholesterol;
run;

ods listing;
proc sgplot data = heart;
title '2) Should show LOW first';
vbar cholesterol ;
xaxis discreteorder=data;
format cholesterol newchol.;
run;
[/pre]
Super Contributor
Super Contributor
Posts: 3,174

Re: Graph color order

Very useful information Cynthia - I focused preparing a reply based on the OP's mention that this is a GROUP= variable order consideration. Also, I perused DOC about FILLATTRS= (and its default behavior discussion) -- and for the GROUP= parameter, it mentions "The plot elements for each group value are automatically distinguished by different visual attributes." O-K. Nothing really stood out as being well-documented in this particular regard.

Scott Barry
SBBWorks, Inc.
SAS Super FREQ
Posts: 8,744

Re: Graph color order

My thought was to get the AXIS under control first and then to work with getting the groups colored based on an understanding of how the AXIS worked. FILLATTRS would not be the way I would try to impact the colors for groups. I use FILLATTRS for ungrouped data.

Grouped fills are automatically colored in the SG procedures based on style attributes in the style template, as described here:
http://support.sas.com/documentation/cdl/en/grstatproc/61948/HTML/default/a003136924.htm (see the section where it describes GraphData1-GraphData12)
http://support.sas.com/documentation/cdl/en/grstatug/62464/HTML/default/p18c2hn0ybnntyn1t6aitke3f0l6...

If you look at the description of FILLATTRS for VBAR in SGPLOT:
http://support.sas.com/documentation/cdl/en/grstatproc/61948/HTML/default/vbar-stmt.htm where it says:

FILLATTRS= style-element | (COLOR= color)
specifies the appearance of the fill for the bars. You can specify the color of the fill by using a style element or by using the COLOR= suboption. For more information about specifying colors, see SAS/GRAPH Colors and Images in the SAS/GRAPH: Reference.

Note: This option has no effect if you specify the NOFILL option.

Default: For ungrouped data, the default color is specified by the Color attribute of the GraphDataDefault style element in the current style.
For grouped data, the default color is specified by the Color attribute of the GraphData1... GraphDatan style elements in the current style.



But, if you use FILLATTRS with GROUP=, then the FILLATTRS color will be used for ALL the groups, so FILLATTRS is mostly useful for ungrouped data.

I don't normally see any confusion with the SG procedures on the coloring of groups, so I wasn't sure what other code was in effect or what the data looked like to cause the original poster's issues with the groups. However, I figured that getting AXIS under control might cause his group issue to fix itself.

For example, if I want the first BP_STATUS to always be PINK, then I change the 'gdata1' attribute to be PINK. On the other hand, if I want the first BP_STATUS to always be PURPLE, then I change the GDATA1 attribute to PURPLE. In the code below, I'm changing GDATA1 to be PINK. No matter what set of options I run for the XAXIS, the first BP_STATUS is always PINK.

cynthia
[pre]
ods path work.temp(update)
sashelp.tmplmst(read);

proc template;
define style styles.grp;
parent=styles.analysis;
class GraphColors /
'gdata1' = pink
'gdata2' = purple
'gdata3' = lavender;
end;
run;

** 1) Sort by bp_status and cholesterol ;
proc sort data=sashelp.heart out=heart;
by bp_status cholesterol;
run;

ods listing style=styles.grp;
proc sgplot data = heart;
title '1) PINK for first bp_status';
vbar cholesterol /group=bp_status;
xaxis discreteorder=data;
format cholesterol newchol.;
run;


** 2) Sort by cholesterol & use format and discreteorder=data;
** so LOW will be first;
proc sort data=sashelp.heart out=heart;
by descending bp_status cholesterol;
run;

ods listing style=styles.grp;
proc sgplot data = heart;
title '2) Still pink for first bp_status';
vbar cholesterol /group=bp_status;
xaxis discreteorder=data;
format cholesterol newchol.;
run;

** 3) Sort by cholesterol & use format and discreteorder=formatted;
** so LOW will be first;
ods listing style=styles.grp;
proc sgplot data = heart;
title '3) All Groups still the same';
vbar cholesterol /group=bp_status;
xaxis discreteorder=formatted;
format cholesterol newchol.;
run;

** 4) Sort by cholesterol & use format and discreteorder=unformatted;
** so LOW will be first;
ods listing style=styles.grp;
proc sgplot data = heart;
title '4) All Groups still the same';
vbar cholesterol /group=bp_status;
xaxis discreteorder=unformatted;
format cholesterol newchol.;
run;
[/pre]
Contributor
Posts: 36

Re: Graph color order

Thanks for the comments.

I don't think I can solve this problem by using sorting like this. In below graphs I would like to have the '3' x axis scale item to not change colors in graph two and three. I would preferably do this without using proc template, as I have a range of variables and would not like to hard code a template for each variable.


proc format;
value $yesno '1' = 'Yes' '0' = 'No';
run;

data test;
informat a b c $1.;
input a b c d;
datalines;
0 1 0 2
1 0 1 3
1 1 0 3
1 0 1 4
;
run;

%macro abc(kind);

proc sort data = test;
by d &kind;
run;

title1 'test' font = verdana;
proc sgplot data = test;
xaxis label = "Time interval";* discreteorder = formatted;
yaxis label = "Count" min = 0 max = 50;* discreteorder = formatted; * alt. unformatted alt. data;
vbar d / group = &kind stat = sum transparency = 0.0 barwidth = 0.9;
format d 1.;
format &kind $yesno.;

label a = 'A';
label b = 'B';
label c = 'C';
run;

%mend abc;

%abc(a);
%abc(b);
%abc(c);
Ask a Question
Discussion stats
  • 6 replies
  • 861 views
  • 0 likes
  • 4 in conversation