This script illustrates how to modify the appearance of legends in PROC SGPLOT using the KEYLEGEND statement. It covers several use cases: adjusting line length, modifying symbol scale, controlling the aspect ratio of fill areas, and using custom formats containing Unicode characters ('greater than or equal to' symbol).
Data Analysis
Type : SASHELP
The script exclusively uses the standard SASHELP.CARS and SASHELP.HEART example tables.
1 Code Block
PROC FORMAT
Explanation : Configuration of the graphical output environment (ODS) and creation of a custom 'agegroupUnicode' format that uses an escape sequence to display the 'greater than or equal to' mathematical symbol (Unicode 2265).
Explanation : Complex bar chart using the previously defined Unicode format. The legend is placed inside the graph ('location=inside'), rendered opaque, and its dimensions are customized to fit the 'dataskin=pressed' style.
Copied!
/*--Legend Swatch--*/
ods graphics / reset width=5in height=3in imagename='DeathsUnicode';
title 'Counts by Death Cause and Age Group';
proc sgplot data=sashelp.heart(where=(deathcause ne 'Unknown')) nocycleattrs noborder;
format ageatdeath agegroupUnicode.;
vbar ageatdeath / group=deathcause groupdisplay=cluster fillattrs=(color=white);
vbar ageatdeath / group=deathcause groupdisplay=cluster nooutline
baselineattrs=(thickness=0) dataskin=pressed filltype=gradient name='a';
keylegend 'a' / location=inside across=1 title='' fillheight=2.5pct fillaspect=2.5 opaque;
xaxis display=(nolabel noline);
yaxis label='Count' grid display=(noline noticks);
run;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.