Data visualization with SAS programming

PROC SGPLOT : How to use Custom Colors in Stacked Bar Chart

Reply
Contributor
Posts: 42

PROC SGPLOT : How to use Custom Colors in Stacked Bar Chart

Hi All,

I need some help using custom colors in Vertical Bar Charts.

I am using SGPLOT / VBAR statement and version is SAS 9.3.

Here is the simple example of what I have. But I would like to use user defined colors (let's say blue green yellow black brown etc etc).

Is there a quick and simple way to do that? Thank you for all your help.

P.S.

data shoes;
set sashelp.shoes;
where region in ('Africa', 'Middle East', 'United States', 'Canada');
run;

proc sgplot data=shoes;
vbar region / response=sales group=product groupdisplay=stack stat=sum;
run;

example.png

SAS Super FREQ
Posts: 925

Re: PROC SGPLOT : How to use Custom Colors in Stacked Bar Chart

At v9.3, you can use Attribute Maps to assign colors to specific group values. For the example below, I flipped the category and group variables so that I had fewer colors to define :-)

data shoes;

set sashelp.shoes;

where region in ('Africa', 'Middle East', 'United States', 'Canada');

run;

data attrmap;

retain id "myid";

input value $ 1-13 fillcolor $ 15-20;

cards;

Africa        blue

Middle East   green

United States yellow

Canada        black

;

run;

proc sgplot data=shoes dattrmap=attrmap;

vbar product / response=sales group=region groupdisplay=stack stat=sum attrid=myid;

run;

SGPlot.png

SAS Super FREQ
Posts: 925

Re: PROC SGPLOT : How to use Custom Colors in Stacked Bar Chart

Sorry, one more thing -- set the LINECOLOR to BLACK in the dataset.

data attrmap;

retain id "myid" linecolor "black";

input value $ 1-13 fillcolor $ 15-20;

cards;

Africa        blue

Middle East   green

United States yellow

Canada        black

;

run;

Contributor
Posts: 42

Re: PROC SGPLOT : How to use Custom Colors in Stacked Bar Chart

Hi DanH,

This is great example in this case. But the problem is I have many variables that I need to create stacked bar chart on and each of the variable has different values and are not consistent between variables.

This will create a problem for me as I will need to define colors for lots of values.

I have used GCHART before and have defined colors in pattern statement and the graph would automatically pick up the colors.

pattern1 value=s color=red ;

pattern2 value=s color=green ;

pattern3 value=l5 color=black;

and I was thinking something similar to that in SGPLOT. Is that an option?

SAS Super FREQ
Posts: 925

Re: PROC SGPLOT : How to use Custom Colors in Stacked Bar Chart

For SAS 9.4 this is trivial. We have a STYLEATTRS statement in the SG procedures where you can define a list of data colors for fills, lines, and markers. Since you said your version was SAS 9.3, I tried to give you a solution where your would not have to modify a style template. However, creating an ODS style with your custom colors is an option if this is something you need to consistently reproduce.

Contributor
Posts: 42

Re: PROC SGPLOT : How to use Custom Colors in Stacked Bar Chart

Hi DanH,

Yes I am still using 9.3. Can you please share an example of creating the ODS style with the custom color? That solution might work in my case. I want to give that a try.

And by the way I am outputting the plots to PDF destination.

Thanks for the help.

SAS Super FREQ
Posts: 1,141

Re: PROC SGPLOT : How to use Custom Colors in Stacked Bar Chart

%MODSTYLE is an easy way to create a style from a parent with specific group colors.

http://blogs.sas.com/content/graphicallyspeaking/2012/06/14/quick-and-easy-with-modstyle/

Occasional Contributor
Posts: 8

Re: PROC SGPLOT : How to use Custom Colors in Stacked Bar Chart

That is easier than Proc template with the added benefit that  if you have dynamic parameters you can fix the colors for a specific id - ie Canada is always red, and US always blue

Now to convert my all my templates to that method as we migrate to 9.4!

Occasional Contributor
Posts: 8

Re: PROC SGPLOT : How to use Custom Colors in Stacked Bar Chart

Hi

Controlling the colors of group bars can be done through Proc Template

proc template;

     define style styles.barcolor;

Style GraphData 1 fom GraphData1/

     Contrast Color = blue

     Color = blue;

end;

run;

Do for each color in the bar

Reference this style in the style=barcolor output for instance

ods html style=barcolor;

Ask a Question
Discussion stats
  • 8 replies
  • 4331 views
  • 0 likes
  • 4 in conversation