Beeswarm Plot - SAS With Overlays

SAS with Overlays

Show macro in new tab or Download

This example produces a horizontally-oriented log-scale beeswarm plot with a geometric mean overlay. See the SAS Macro example if you are new to beeswarm plots in SAS. See the User Guide if you want even more details about the SAS macro.

The target image is 8in wide and 4in tall. These dimensions work well for landscape pages. Producing a beeswarm plot with custom dimensions such as these requires a small amount of experimentation.

  • The default SGPLOT image size will fit 60 markers vertically and 80 markers horizontally. Experiment to determine how many markers will fit along the grouping axis (G) and the response axis (R) for your particular non-default dimensions (in this case, 8in by 4in).
  • Extreme precision is not required; approximately correct values will produce a respectable graph.
  • In your call to the beeswarm macro, specify GMARKERS=G and RMARKERS=R.

Through experimentation it was determined that 100 markers would fit along the 8in response axis and 45 markers would fit along the 4in grouping axis. This leads us to specify RMARKERS=100 and GMARKERS=45 in our call to the macro.

Source Code

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

data dummy;
   do trt = 1 to 4;
      do subjects = 1 to 50;
         if trt = 1 then response = 1 + 6*ranuni(17);
         if trt = 2 then response = 4 + rannor(17);
         if trt = 3 then response = 2 + 8*ranuni(17);
         if trt = 4 then response = 8 + 2*rannor(17);
         response = 10**(response/7);
         output;
      end;
   end;
run;


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

data log10dummy;
   set dummy;
   log10response = log10(response);1
run;

%include 'beeswarm.sas';2

%beeswarm(data=log10dummy
         ,respvar=log10response
         ,grpvar=trt
         ,rmarkers=100
         ,gmarkers=45
         );3


*------------------------------------------------------------;
*-------------- add geometric means to dataset --------------;
*------------------------------------------------------------;

proc means data=log10dummy noprint;
   var log10response;
   by trt;
   output out=log10means mean=log10mean;
run;

data plotdata;
   set beeswarm (in=beeswarm) log10means (in=means);
   if beeswarm then output;
   if means then do;
      mean = 10**log10mean;
      trt_bee = trt - 0.3;
      output;
      trt_bee = trt + 0.3;
      output;
   end;
run;


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

ods graphics / width=8in height=4in;4

proc sgplot data=plotdata noautolegend;
   *--- series for the mean bar ---;
   series y=trt_bee x=mean / group=trt 
      lineattrs=(pattern=solid color=gray thickness=5);
   *--- scatter for the beeswarm points ---;
   scatter y=trt_bee x=response / 
      markerattrs=(symbol=circlefilled color=black);
   *--- axis customizations ---;
   yaxis min=0.5 max=4.5 integer;5
   xaxis type=log logbase=10;
run;

1 Because the response values will be plotted on a log scale, we must pass a logged version to the macro.

2 See above for macro download links.

3 This macro call uses 2 optional parameters: RMARKERS and GMARKERS. These are needed because non-default values for WIDTH and HEIGHT are specified below. RMARKERS indicates how many markers will fit along the response axis. GMARKERS indicates how many markers will fit along the grouping axis.

4 Changing the default image size.

5 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