BookmarkSubscribeRSS Feed
jplarios
Quartz | Level 8

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

4 REPLIES 4
GraphGuy
Meteorite | Level 14

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

jplarios
Quartz | Level 8

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 ?

GraphGuy
Meteorite | Level 14

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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 5195 views
  • 2 likes
  • 3 in conversation