BookmarkSubscribeRSS Feed
P_S_
Obsidian | Level 7

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

8 REPLIES 8
DanH_sas
SAS Super FREQ

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

DanH_sas
SAS Super FREQ

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;

P_S_
Obsidian | Level 7

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?

DanH_sas
SAS Super FREQ

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.

P_S_
Obsidian | Level 7

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.

Jay54
Meteorite | Level 14

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

lisagros
Calcite | Level 5

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!

lisagros
Calcite | Level 5

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 13312 views
  • 0 likes
  • 4 in conversation