Sample 69820: Using the SGPLOT procedure to create a butterfly plot with text
This SAS Note provides a sample on how to use the SGPLOT procedure to create a butterfly plot with labels within the bars.
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
This sample code uses the SGPLOT procedure to create a butterfly plot with labels within the bars.
/* Create sample data for males and females in
different age groups */
data ButterFly;
length AgeGroup $12;
do AgeGroup='Pre Teen', 'Teen', 'Young Adult', 'Adult', 'Senior';
Male=round(500*(1+ranuni(2)));
/* Make the bars for the male group draw to the left by
making the value negative */
Male=-male;
Female=round(400*(1+ranuni(2))); zero=0;
output;
end;
run;
/* Create a format to display Male and Female count values
as positive numbers */
proc format;
picture positive low-<0='0000' 0<-high='0000';
run;
ods graphics / reset width=6in height=4in imagename="ButterflyChart";
title 'Population by Age and Sex';
proc sgplot data=ButterFly noautolegend;
format male female positive.;
hbarparm category=agegroup response=male / dataskin=sheen name='m'
fillattrs=graphdata1 datalabel=male datalabelattrs=(size=10) transparency=0.2;
hbarparm category=agegroup response=female / dataskin=sheen name='f'
fillattrs=graphdata2 datalabel=female datalabelattrs=(size=10) transparency=0.2;
text x=zero y=agegroup text=agegroup/ textattrs=(size=11 weight=bold);
keylegend 'm' 'f';
xaxis values=(-1000 to 1000 by 200) display=(nolabel) grid offsetmin=0.05 offsetmax=0.05;
yaxis display=(noticks novalues nolabel);
run;
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
Type: | Sample |
Topic: | SAS Reference ==> Procedures ==> SGPLOT
|
Date Modified: | 2023-01-31 13:28:52 |
Date Created: | 2023-01-20 13:24:40 |
Operating System and Release Information
SAS System | Base SAS | z/OS 64-bit | 9.4_M2 | | 9.4 TS1M2 | |
z/OS | 9.4_M2 | | 9.4 TS1M2 | |
Microsoft® Windows® for x64 | 9.4_M2 | | 9.4 TS1M2 | |
Microsoft Windows 8 Enterprise 32-bit | 9.4_M2 | | 9.4 TS1M2 | |
Microsoft Windows 8 Enterprise x64 | 9.4_M2 | | 9.4 TS1M2 | |
Microsoft Windows 8 Pro 32-bit | 9.4_M2 | | 9.4 TS1M2 | |
Microsoft Windows 8 Pro x64 | 9.4_M2 | | 9.4 TS1M2 | |
Microsoft Windows 8.1 Enterprise 32-bit | 9.4_M2 | | 9.4 TS1M2 | |
Microsoft Windows 8.1 Enterprise x64 | 9.4_M2 | | 9.4 TS1M2 | |
Microsoft Windows 8.1 Pro 32-bit | 9.4_M2 | | 9.4 TS1M2 | |
Microsoft Windows 8.1 Pro x64 | 9.4_M2 | | 9.4 TS1M2 | |
Microsoft Windows 10 | 9.4_M2 | | 9.4 TS1M2 | |
Microsoft Windows Server 2008 | 9.4_M2 | | 9.4 TS1M2 | |
Microsoft Windows Server 2008 R2 | 9.4_M2 | | 9.4 TS1M2 | |
Microsoft Windows Server 2008 for x64 | 9.4_M2 | | 9.4 TS1M2 | |
Microsoft Windows Server 2012 Datacenter | 9.4_M2 | | 9.4 TS1M2 | |
Microsoft Windows Server 2012 R2 Datacenter | 9.4_M2 | | 9.4 TS1M2 | |
Microsoft Windows Server 2012 R2 Std | 9.4_M2 | | 9.4 TS1M2 | |
Microsoft Windows Server 2012 Std | 9.4_M2 | | 9.4 TS1M2 | |
Windows 7 Enterprise 32 bit | 9.4_M2 | | 9.4 TS1M2 | |
Windows 7 Enterprise x64 | 9.4_M2 | | 9.4 TS1M2 | |
Windows 7 Home Premium 32 bit | 9.4_M2 | | 9.4 TS1M2 | |
Windows 7 Home Premium x64 | 9.4_M2 | | 9.4 TS1M2 | |
Windows 7 Professional 32 bit | 9.4_M2 | | 9.4 TS1M2 | |
Windows 7 Professional x64 | 9.4_M2 | | 9.4 TS1M2 | |
Windows 7 Ultimate 32 bit | 9.4_M2 | | 9.4 TS1M2 | |
Windows 7 Ultimate x64 | 9.4_M2 | | 9.4 TS1M2 | |
64-bit Enabled AIX | 9.4_M2 | | 9.4 TS1M2 | |
64-bit Enabled Solaris | 9.4_M2 | | 9.4 TS1M2 | |
HP-UX IPF | 9.4_M2 | | 9.4 TS1M2 | |
Linux for x64 | 9.4_M2 | | 9.4 TS1M2 | |
Solaris for x64 | 9.4_M2 | | 9.4 TS1M2 | |