Data visualization with SAS programming

gplot: specifying colors for specific categories of a variable

Reply
Regular Contributor
Posts: 199

gplot: specifying colors for specific categories of a variable

I'm using gplot to make some charts.
How do I specify that I always want a category in a categorical variable to be a specific color?
For example, if I have a variable x (with categories a,b,c) how do I specify that I always want c to be yellow?
If I use the symbol option then c may not be yellow if b is not plotted.
Thank you.
SAS Super FREQ
Posts: 8,815

Re: gplot: specifying colors for specific categories of a variable

Hi:
You may want to consider moving toward an approach like the one shown in this Tech Support note. http://support.sas.com/kb/24/874.html

It uses a SAS Macro program to guarantee that the ice cream flavors get the right color for the bar chart, even if there is no flavor in a particular group.

The key is specifying an explicit numbered PATTERN statement value in a macro program. Then the correct PATTERN statement is used for every group, so that strawberry is always pink, chocolate is always brown, etc, etc.

The other approach that folks use is to put a "dummy" obs into the data for every category or combination of category and by group. In this way, they ensure that there will be the right number of categories. See the program example below. SASHELP.CLASS only has 1 student -- a male in the age 16 group -- so by putting an extra observation for F in the dataset to graph, I can ensure that the bar for M uses the color in the second PATTERN statement.

cynthia
[pre]
ods listing;
proc freq data=sashelp.class;
title 'note that there are no F in Age 16';
where age in (14, 15, 16);
tables age*sex / list;
run;

** now, ADD an F observation with a very, very LOW value for the SUMVAR variable;
data class;
set sashelp.class end=eof;
where age in (14, 15, 16);
output;
if eof then do;
** make sure that age 16 has an obs for F;
name = 'extra';
sex = 'F';
height=.0001;
age = 16;
output;
end;
run;

proc sort data=class out=class;
by age;
run;

goptions reset=all;
pattern1 c=pink v=s;
pattern2 c=blue v=s;

proc gchart data=class;
title 'By Age #byval1';
by age;
vbar sex / sumvar=height patternid=midpoint;
run;
quit;

goptions reset=all;
[/pre]
Regular Contributor
Posts: 199

Re: gplot: specifying colors for specific categories of a variable

Thank you.
I think the SAS macro program is a good idea:
http://support.sas.com/kb/24/874.html
Although I should be able to implement this approach do you have any other examples that don't involve "across by groups" (I'm just interested in maintaining patterns)?
Thanks.
SAS Super FREQ
Posts: 8,815

Re: gplot: specifying colors for specific categories of a variable

Hi:
Well, reading the documentation is always a good idea:
http://support.sas.com/documentation/cdl/en/graphref/63022/HTML/default/viewer.htm#/documentation/cd...

"Seeding" the data with all the combinations of groups/subgroups that you need would be good. Really understanding the possible combinations is critical to getting the PATTERN right. Understanding how PATTERNID can impact bar fills (PATTERNID=MIDPOINT, PATTERNID=SUBGROUP, PATTERNID=GROUP, etc).

Using Macro can help, but understanding the data will be the biggest help of all. Running some tests and making up some fake data to see how well your program works under different circumstances.

cynthai
Valued Guide
Posts: 632

Re: gplot: specifying colors for specific categories of a variable

Although it specifically addresses LEGEND control, the paper
http://www2.sas.com/proceedings/sugi29/086-29.pdf also
discusses many of the same approaches.
Ask a Question
Discussion stats
  • 4 replies
  • 180 views
  • 0 likes
  • 3 in conversation