Beeswarm Plot - SAS With Panels

SAS with Panels

Show macro in new tab or Download

This example produces a paneled beeswarm plot. See the SAS Macro example if you are new to beeswarm plots. See the User Guide if you want even more details about the SAS macro.

Prior to calling the macro, first determine appropriate values for RMARKERS= (i.e., how many markers will fit along the response axis) and GMARKERS= (i.e., how many markers will fit along the grouping axis). By default the macro assumes values of 60 and 80. These values work fine for default-sized SGPLOT output. However, for SGPANEL, the appropriate values are quite different.

SAS
*------------------------------------------------------------;
*---------- determine best rmarkers= and gmarkers= ----------;
*------------------------------------------------------------;

data fillpanel;
   do panel = 1 to 3;
      do x = 0 to 75;
         do y = 0 to 35;1
            output;
         end;
      end;
   end;
run;

proc sgpanel data=fillpanel;
   panelby panel / columns=3;
   scatter x=x y=y;
   colaxis thresholdmax=0;
   rowaxis thresholdmax=0;
run;

1 The values 75 and 35 are arrived at through experimentation. Expect to have to test several values before getting a good fit.

The values 75 and 35 fit nicely here. Larger values would have resulted in dark strips (indicating overlaps). Smaller values would have resulted in light strips (indicating gaps).

The values for RMARKERS= and GMARKERS= now in hand, producing a paneled beeswarm plot is essentially a 3-step process.

  1. Split the original dataset into smaller panel-specific datasets.
  2. Pass the panel-specific datasets to the macro one at a time.
  3. Stack the panel-specific output datasets back together again and send to SGPANEL.

Source Code

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

data dummy;
   do panel = 1 to 3;1
      do trt = 1, 2, 3;
         do subjects = 1 to 30 - 4*trt*floor(sqrt(panel)) by 1;
            response = sqrt(trt)*sqrt(panel)*(rannor(1)+3);
            output;
         end;
      end;
   end;
run;


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

%include "beeswarm.sas";2

%macro panel;

   %do i = 1 %to 3;3

      data panel&i;
         set dummy;
         where panel eq &i;
      run;

      %beeswarm(data=panel&i
               ,respvar=response
               ,grpvar=trt
               ,rmarkers=75
               ,gmarkers=35
               ,rmin=0
               ,rmax=20
               ,out=beeswarm&i
               );4

   %end;

   data beeswarm;
      set %do i = 1 %to 3; beeswarm&i %end; ;
   run;

%mend panel;

%panel;


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

proc sgpanel data=beeswarm;
   panelby panel / columns=3;
   scatter x=trt_bee y=response / markerattrs=(symbol=circlefilled);
   colaxis min=0.5 max=3.5 integer;
run;

1 Generate 3 panels worth of dummy data.

2 See above for macro download links.

3 Break the original dataset up into smaller panel-specific datasets.

4 SGPANEL adds bounding tick marks to our plot. To compensate, we need to specify values for RMIN= and RMAX=. See the macro header or the User Guide for additional information about these optional parameters.

Data

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

top