Data visualization with SAS programming

Legend order in PROC GCHART

Reply
Regular Contributor
Posts: 163

Legend order in PROC GCHART

Hello,

I have a proc gchart and have a Legend. The proc outputs barcharts with subgroups. Like goup A with three bars,x,y,z Group B with xxy,z etc. In my case the order of the bars matter ( x needs to go first). How can I make the legend display the same order of the bars.RIght now, no matter what I do, it will display the legend with the color of y then z and then x. I tried many different things and nothing.Any help greatly appreciated. Thanks

PROC Star
Posts: 7,431

Legend order in PROC GCHART

SAS Employee
Posts: 977

Legend order in PROC GCHART

I assume you're wanting the legend colors to be 'stacked' in the same order as the bar segments are stacked(?)...

If you're re-using the same code with different data, sometimes it's not practical to hard-code the desired legend order (you would need to keep re-hard-coding the legend values as the data changes, or do something tricky to generate it dynamically in a macro variable).

Therefore, in 9.3 we've added the new "descending" option for the gchart legend -- it allows you to easily get the legend in descending order (whereas it's usually in ascending order).  Here's a little example & explanation:

http://robslink.com/SAS/democd50/new_93_sas.htm#desc

Regular Contributor
Posts: 163

Legend order in PROC GCHART

Robert, 

Yes, it is the colors that should be ordered the same way as the bars. I should have been more clear. For example, if red is first, the color in the legend should be red first. I did the order as mentioned by Art297 but noticed the colors did not want to get ordered. Any samples on ordering in 9.2 ?

SAS Employee
Posts: 977

Legend order in PROC GCHART

You would have to hard-code the legend order in 9.2, such as ...

data a;
input Salesman $ 1-6 Year Sales;
datalines;
Bob    2009  6
Anna   2009  8
Carl   2009  9
Bob    2010  7
Anna   2010  9
Carl   2010 10
;
run;

axis1 label=none order=(0 to 30 by 10) minor=none offset=(0,0);
axis2 label=none offset=(15,15);

legend1 position=(right middle) across=1 label=none
shape=bar(.13in,.13in) order=('Carl' 'Bob' 'Anna');

pattern1 v=s c=yellow;
pattern2 v=s c=orange;
pattern3 v=s c=red;

proc gchart data=a;
vbar year / discrete type=sum sumvar=sales
raxis=axis1 maxis=axis2 width=15 space=8
noframe autoref clipref cref=graycc
subgroup=Salesman legend=legend1;
run;

Ask a Question
Discussion stats
  • 4 replies
  • 2848 views
  • 2 likes
  • 3 in conversation