Hello
I have been trying and searched all around the internet how to code this, I am pretty sure it is simple but i am going in circles and hope that someone here can help me
notably i tried to follow the advice here, unsuccessfully https://blogs.sas.com/content/graphicallyspeaking/2016/02/04/legend-order/
I am trying to invert the order of the legend of the attached plot so it matches that of the bars - as you can see for now you have in the plot the legend starts with Staphylococcus aureus on top, when in the plot it is at the bottom. It would be much more readable if the order of the legend matched that of the plot
thanks in advance
regards
Alex
ods html image_dpi=300;
title Frequency of pathogens by location of onset;
proc sgplot data=klfig.path;
vbar location / response=Percent group=Pathogen groupdisplay=stack SEGLABELFORMAT=percent10.1 seglabel grouporder=reversedata;
xaxis discreteorder=data;
yaxis grid values=(0 to 1 by 0.1) label="Percentage of each Pathogen" discreteorder=data;
keylegend / location=outside position=right across=1 title="order needs inverted";
run;
The REVERSEAUTO option on the KEYLEGEND should get you what you want. Here is a simple example
proc sgplot data=sashelp.cars;
vbar origin / response=msrp group=type name="type";
keylegend "type" / sortorder=reverseauto position=right;
run;
The REVERSEAUTO option on the KEYLEGEND should get you what you want. Here is a simple example
proc sgplot data=sashelp.cars;
vbar origin / response=msrp group=type name="type";
keylegend "type" / sortorder=reverseauto position=right;
run;
thank you very much 🙂
Glad you asked this question - sorting the legend like this should be a "best practice" imho.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Early bird rate extended! Save $200 when you sign up by March 31.
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.
Ready to level-up your skills? Choose your own adventure.