With Overlays - RhoInc/sas-beeswarm GitHub Wiki
In this example, we will produce a horizontally-oriented log-scale beeswarm plot with a geometric mean overlay.
The target image is 8in wide and 4in tall. These dimensions work well for landscape pages.
Through experimentation (similar to what was done for the Paneled Plots), 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.
*------------------------------------------------------------;
*------------------ 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 --------------------;
*------------------------------------------------------------;
*--- Because the response values will be plotted on a log scale, ;
*--- we must pass a logged version of the response variable to ;
*--- the macro. ;
data log10dummy;
set dummy;
log10response = log10(response);
run;
%beeswarm
(data=log10dummy
,respvar=log10response
,grpvar=trt
,rmarkers=100
,gmarkers=45
);
*------------------------------------------------------------;
*-------------- 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;
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;
xaxis type=log logbase=10;
run;