Graphically Speaking

Box plot legend

A grouped box plot created by SGPLOT VBOX / HBOX statement or GTL BOXPLOT statement will display groups within categories using group colors and puts the color swatches representing the group values in the legend. This works well for a color graph created as shown below.

title ‘Distributiion of Cholesterol by Death Cause and Sex’;
proc sgplot data=sashelp.heart nowall noborder;
vbox cholesterol / category=deathcause group=sex clusterwidth=0.5 );
xaxis display=(noline nolabel noticks);
yaxis display=(noline noticks) grid;
run;

SAS Certifications Tutorials and Materials, SAS Certifications Guide, SAS Certifications, SAS Certifications Syllabus

Often, users have expressed the need to render such graphs in black and white for submission to a journal. Many journals specify that graphs should be black and white. The good news is that you can do that by simply setting the STYLE=JOURNAL2 which produces a black and white graph with empty boxes as shown below.

SAS Certifications Tutorials and Materials, SAS Certifications Guide, SAS Certifications, SAS Certifications Syllabus

One problem with this graph (for me) is the usage of patterned lines for the boxes and the whiskers. Personally, I an not a fan of patterned lines for boxes and short whiskers. I would much rather prefer solid lines as shown below. This can be done by using the box plot options LINEATTRS and WHISKERATTRS to set the pattern to solid. The resulting graph is shown below.

SAS Certifications Tutorials and Materials, SAS Certifications Guide, SAS Certifications, SAS Certifications Syllabus

Now, the boxes and whiskers look nice and clean. But the legend does not provide good information on the classification. Now the legend contains two line segments both of which are solid black for Female and Male.

To fix this required some creative coding. However, with SAS 9.40M2 this has become easier. Now we have a new feature to display different legends based on TYPE, which filters what is shown in the legend. The syntax is as follows.

KEYLEGEND / TYPE=FILL | FILLCOLOR | LINE | LINECOLOR | LINEPATTERN | MARKER | MARKERCOLOR | MARKERSYMBOL;

Now, you can specify to see just markers, or markercolor or markersymbol in the legend. Markers means display the marker symbol with their color. MarkerColor will show only the color swatches for the markers, and MarkerSymbol will show only the symbol. The symbol displayed is the one used for displaying the mean value.

Using this option on the legend gets what we want as shown below. Note, now the legend displays the mean symbols for each group. If the graph was using a color style, the symbol color would also be displayed.

SAS Certifications Tutorials and Materials, SAS Certifications Guide, SAS Certifications, SAS Certifications Syllabus

title ‘Distributiion of Cholesterol by Death Cause and Sex’;
proc sgplot data=sashelp.heart nowall noborder;
vbox cholesterol / category=deathcause group=sex clusterwidth=0.5
meanattrs=(size=7) lineattrs=(pattern=solid) whiskerattrs=(pattern=solid);
xaxis display=(noline nolabel noticks);
yaxis display=(noline noticks) grid;
<strong>keylegend / type=marker;</strong>
run;

Full SAS 9.40M2 code:

%let gpath=.;
%let dpi=200;
ods html close;
ods listing gpath=”&gpath” image_dpi=&dpi;

/*–Box plot Color–*/
ods listing style=listing;
ods graphics / reset width=5in height=3in imagename=’Box_Color’;
title ‘Distributiion of Cholesterol by Death Cause and Sex’;
proc sgplot data=sashelp.heart nowall noborder;
vbox cholesterol / category=deathcause group=sex
clusterwidth=0.5 meanattrs=(size=7);
xaxis display=(noline nolabel noticks);
yaxis display=(noline noticks) grid;
run;

/*–Box plot BW–*/
ods listing style=journal2;
ods graphics / reset width=5in height=3in imagename=’Box_BW’;
title ‘Distributiion of Cholesterol by Death Cause and Sex’;
proc sgplot data=sashelp.heart nowall noborder;
vbox cholesterol / category=deathcause group=sex clusterwidth=0.5
meanattrs=(size=7);
xaxis display=(noline nolabel noticks);
yaxis display=(noline noticks) grid;
run;

/*–Box plot BW solid lines–*/
ods listing style=journal2;
ods graphics / reset width=5in height=3in imagename=’Box_Solid_Lines’;
title ‘Distributiion of Cholesterol by Death Cause and Sex’;
proc sgplot data=sashelp.heart nowall noborder;
vbox cholesterol / category=deathcause group=sex clusterwidth=0.5
meanattrs=(size=7) lineattrs=(pattern=solid) whiskerattrs=(pattern=solid);
xaxis display=(noline nolabel noticks);
yaxis display=(noline noticks) grid;
run;

/*–Box plot BW solid lines Legend markers–*/
ods listing style=journal2;
ods graphics / reset width=5in height=3in imagename=’Box_Legend’;
title ‘Distributiion of Cholesterol by Death Cause and Sex’;
proc sgplot data=sashelp.heart nowall noborder;
vbox cholesterol / category=deathcause group=sex clusterwidth=0.5
meanattrs=(size=7) lineattrs=(pattern=solid) whiskerattrs=(pattern=solid);
xaxis display=(noline nolabel noticks);
yaxis display=(noline noticks) grid;
keylegend / type=marker;
run;

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s