Data visualization with SAS programming

GTL Customized Grouped Bar Colors

Reply
Contributor
Posts: 36

GTL Customized Grouped Bar Colors

I have been using SGDESIGN to build customized graphic templates with great success. My question is how do I change the default colors used for the bars to say GREEN, CYAN, RED? Specifying a bar color in SGDESIGN applies the color to all groups. Do I need to modify the styles.listing template? How do I assign a style in PROC SGRENDER?

Here is my template code.

proc template;
define statgraph sg_hbar_group;
dynamic _CLASS _RESPONSE _GROUP _TITLE _JOBID;
begingraph / backgroundcolor=&uss_background borderattrs=(color=&uss_background)
designheight=480 designwidth=700;
entrytitle _id='title' halign=center _TITLE / textattrs=(size=12 family='Albany AMT');
entryfootnote _id='footnote' halign=left _JOBID /;
layout lattice _id='lattice' / columndatarange=data rowdatarange=data;
layout overlay _id='overlay' / wallcolor=&uss_background
xaxisopts=(tickvalueattrs=(weight=BOLD style=NORMAL family='Albany AMT' size=10) labelattrs=(family='Albany AMT' weight=BOLD style=NORMAL))
yaxisopts=(tickvalueattrs=(weight=BOLD style=NORMAL size=10 family='Arial Narrow') display=(TICKVALUES LINE ));
barchart _id='bar(h)' x=_CLASS y=_RESPONSE / barlabel=true group=_GROUP name='bar(h)' orient=horizontal skin=modern;
discretelegend _id='legend' 'bar(h)' / border=true displayclipped=true down=1 halign=center location=inside opaque=false order=columnmajor valign=bottom;
endlayout;
endlayout;
endgraph;
end;
run;
SAS Super FREQ
Posts: 8,647

Re: GTL Customized Grouped Bar Colors

Hi:
The easiest way to change the bar colors is to use a modified style template. The style attribute values GDATA1-GDATA12 set the colors for grouped data. The program below does a very simple bar chart in the template and then shows the default colors from the LISTING style and then the new colors from the CARCOLORS style template.

The STYLE= option on the ODS invocation statement will be the appropriate place to change the style used for the SGRENDER...so for example:
[pre]
ods listing style=styles.carcolors;
** sgrender step;

OR

ods html file=... style=styles.carcolors;
** sgrender step;
ods html close;

OR

ods rtf file=... style=styles.carcolors;
** sgrender step;
ods rtf close;
[/pre]

Some good papers on style template usage in SAS 9.2 with a few graph examples are:
http://support.sas.com/resources/papers/proceedings10/033-2010.pdf
http://support.sas.com/resources/papers/proceedings09/227-2009.pdf

cynthia
[pre]
data carsum;
infile datalines;
input Type $ Count Sum_Weight;
return;
datalines;
Hybrid 3 7472
SUV 60 266666
Sedan 262 890555
Sports 49 161489
Truck 24 102018
Wagon 30 103164

;
run;

ods path work.tmp(update) sasuser.templat(update) sashelp.tmplmst(read);

proc template;
define statgraph carplot;
mvar _mytitle_;
begingraph;
EntryTitle _mytitle_ /;
layout overlay / xaxisopts=(type=Discrete );
BarChartParm X=Type Y=Sum_Weight /
primary=true
Group=Type
LegendLabel="Weight (LBS)" NAME="VBAR";
DiscreteLegend "VBAR"/ title="Type";
endlayout;
endgraph;
end;
run;

%let _mytitle_ = The Default Style for ODS LISTING;
proc sgrender data=carsum template=carplot;
run;

** now make a new style template;
proc template;
define style styles.carcolors;
parent=styles.listing;
class GraphColors /
'gdata1'=green
'gdata2'=red
'gdata3'=cyan
'gdata4'=pink
'gdata5'=yellow
'gdata6'=purple;
end;
run;

ods listing style=styles.carcolors;
%let _mytitle_ = The Changed Style Template;
proc sgrender data=carsum template=carplot;
run;

[/pre]
Contributor
Posts: 36

Re: GTL Customized Grouped Bar Colors

Thanks Cynthia. Problem solved.
SAS Super FREQ
Posts: 1,044

Re: GTL Customized Grouped Bar Colors

The solution Cynthia has provided is one of the right answers. Since one of the motivations for Designer is to avoid such coding, there is, "hidden" deep inside Designer, an early version of a GRAPH STYLE EDITOR.

In Designer, Click Tools->Style Editor. It will bring up the Graph Style Editor GUI. Starting with the current style (usually LISTING) as parent you can derive a new style. Once inside the Style Editor, you can change this initial parent style to another one.

Now, you can change the various color, marker, pattern, etc attributes for each of the style elements using the GUI. If you don't know the name of the element, click on the graphs on the right, and it will take you to the right one. Click the title, or background, or a bar, marker, etc. Make your changes, and save the style as a user custom style (give it a different name). You cannot overwrite a SAS supplied style.

Now, back in your graph, click Format->Style-> and select your new style. It may be in "More Styles" in the Custom Styles list.

It is called a "Graph Style Editor" because it only allows you to change the style elements that are applicable to the graph, not other things like tables, etc.

You can also export a generated style (File->Export Style) to a .sas file for use with your code. Note, for SAS 9.2M3, this contains the full definition, even if you have only made a few changes. This will be fixed in the next release.
SAS Super FREQ
Posts: 1,044

Re: GTL Customized Grouped Bar Colors

Documentation for Style Editor can be found in the SAS Help and Documentation:
- SAS Products
- SAS/GRAPH
- SAS/GRAPH 9.2 ODS Graphics Designer User's Guide:
- Changinfg Appearance of a Graph
- Customizing Graph Styles.
Post a Question
Discussion Stats
  • 4 replies
  • 276 views
  • 0 likes
  • 3 in conversation