Beeswarm Plot - SAS Macro

SAS Macro

Show macro in new tab or Download

This example shows a basic beeswarm plot developed in SAS.
See the User Guide if you want more details about the SAS macro.

Producing a beeswarm plot is SAS is a two-step process.

  1. Use the %beeswarm macro to add a new variable to your dataset.
  2. Use this new variable as the X= option in a SCATTER statement.

The below example assumes that default settings will be in effect for ODS Grahpics and SGPLOT. If you will be modifying the default image size, aspect ratio, marker sizes, or continuous axis limits, see the User Guide page for links to additional documenation.

Source Code

SAS

*------------------------------------------------------------;
*---------- dummy data generation ----------;
*------------------------------------------------------------;

data dummy;
do trt = 1, 2;
  do subjects = 1 to 100;
     if trt = 1 then response = -3 + 6*ranuni(17);
     if trt = 2 then response = rannor(17);
     output;
  end;
end;
run;


*------------------------------------------------------------;
*---------- beeswarm macro call ----------;
*------------------------------------------------------------;

%include 'beeswarm.sas';1

%beeswarm(data=dummy
     ,grpvar=trt
     ,respvar=response
     );2


*------------------------------------------------------------;
*---------- plot it ----------;
*------------------------------------------------------------;

proc sgplot data=beeswarm;
scatter x=trt_bee y=response / markerattrs=(symbol=circlefilled);
xaxis min=0.5 max=2.5 integer;3
run;

1 See above for macro download links.

2 The macro creates dataset BEESWARM with variable TRT_BEE.

3 Customizing the categorical axis range to extend 0.5 outside of the actual data range.

Data

The above code generates raw data with random number functions. No data download is necessary.

top