Data visualization with SAS programming

gchart change bar color

Reply
N/A
Posts: 0

gchart change bar color

How can I change the bar color based upon the value of rate1?
For example, if rate1=>2 I would like the bar to be red and if 0<2 then I would like the bar to be yellow.
proc gchart data=goals;
vbar bu
discrete sumvar=rate1;
maxis=axis1
raxis=axis2
gaxis=axis3
run;
Thank you.
SAS Super FREQ
Posts: 864

Re: gchart change bar color

I would do this by copying rate1 to a new column (we'll say rate2) and put a user-defined format on rate using your range specifications. Then, specify two PATTERN statements with the colors you want (yellow first, then red). Sort your data by rate2 from least to greatest. Set rate2 as the SUBGROUP variable on your VBAR statement. Hopefully, that will give you the result you want.

Thanks!
Dan
SAS Employee
Posts: 963

Re: gchart change bar color

Unfortunately, gchart does not let you directly specify specific colors for specific values or ranges of values (you could do that in SAS/AF charts with the "color range object", but not in SAS/Graph charts).

The easiest way I know of to do that, is to add an extra variable to your data set, and use if/else statements to assign a value to that variable using if/else statements based on the >= rules you mentioned.

Then in the gchart, use the subgroup= option, and specify that new variable. The values of this variable will get mapped to the colors in the pattern statement (you have to get a little 'tricky' if your data does not always contain obsns for all the desired colors, by the way).

Here is an example, showing the basics:

data goals;
input bu rate1;
datalines;
1 2.6
2 .5
;
run;

data goals; set goals;
if rate1>=2 then colorval=1;
else colorval=2;
run;

pattern1 v=solid c=red;
pattern2 v=solid c=yellow;

proc gchart data=goals;
vbar bu / discrete sumvar=rate1 subgroup=colorval
refline=2 clipref nolegend;
run;
Post a Question
Discussion Stats
  • 2 replies
  • 179 views
  • 0 likes
  • 3 in conversation