Changing Boxplot Display

The SAS System defines many graphical style elements. Some have a very narrow scope, such as those that control the display of box plots. Using these style elements as a starting point, you can change the style attribute values to achieve a very different appearance for your box plots. The same is true for histograms, contours, and some other plot types.
Using the DEFAULT style for an example, here is a portion of the style definition for elements that are related to box plots:
proc template;
  define style Styles.Default;
  ...
  style GraphBox /
     capstyle = "serif"
     connect = "mean"
     displayopts = "fill caps median mean outliers";
  style GraphBoxMean / ... ;
  style GraphBoxMedian / ... ;
  style GraphBoxOutlier / ... ;
  style GraphBoxWhisker / ... ;
  ...
  end;
run;
Style Element
Purpose
GraphBox
general box plot properties (see the next table)
GraphBoxMean
marker properties of mean marker
(MARKERSYMBOL= , MARKERSIZE=, CONTRASTCOLOR=)
GraphBoxMedian
line properties of the median line
(LINESTYLE=, LINETHICKNESS=, CONTRASTCOLOR=)
GraphBoxOutlier
marker properties of outliers
(MARKERSYMBOL=, MARKERSIZE=, CONTRASTCOLOR=)
GraphBoxWhisker
line properties of whiskers and caps
(LINESTYLE=, LINETHICKNESS=, CONTRASTCOLOR=)
Attributes and Values for the GraphBox Style Element
Attribute
Value(s)
Description
CONNECT=
"MEAN" | "MEDIAN" | "Q1" | "Q3" | "MIN" | "MAX"
statistic to connect with line
CAPSTYLE=
"SERIF" | "LINE" | "BRACKET"
shape at ends of whiskers
DISPLAYOPTS=
"<CAPS>
show caps at end of whiskers
<FILL>
show filled boxes
<MEAN>
show a marker for the mean
<MEDIAN>
show a line for the median
<OUTLIERS>
show markers for the outliers
<CONNECT>
show line connecting same statistic on multiple boxes
<NOTCHES>"
show notched boxes
The Displayopts attribute of GraphBox lists the general features that will be displayed. The following diagram shows the standard display for box plots, as defined by the DEFAULT style. The keywords that are related to the appearance features are annotated:
Standard Display for Box Plots
The two display options that are not the default are CONNECT (show connect lines) and NOTCHES.
Notched Box Plots with Connect Line
The STATISTICAL style is derived from the DEFAULT style and inherits the GraphBox element from the parent DEFAULT style. The following code generates a box plot for the STATISTICAL style:
title "Statistical Style";
ods listing style=statistical;

proc sgplot data=sashelp.heart;
  hbox diastolic /
       category=weight_status;
run;
Box Plot with Statistical Style
For this example, we want to change the following attributes on the default box plot:
  • By default, serif caps are displayed at the end of the fences. We want to remove those caps from the fence lines.
  • By default, the boxes are filled. We want to display empty, notched boxes.
  • By default, the mean values are represented by hollow diamonds. We want to display filled diamonds and slightly reduce their size.
  • By default, the marker symbols for the outliers are hollow black circles. We want to change the size and shape of the marker symbols, and again reduce their size.
To make these changes, we can derive a new style from the STATISTICAL style and set the attributes that we want to change. Any attribute settings that we do not change will be inherited from the parent STATISTICAL style. The following style definition will effect the desired changes:
proc template;
  define style Styles.Boxplot;
  parent = styles.statistical;
    style GraphBox from GraphBox /
      capstyle = " line "
      displayopts = " caps median mean outliers notches ";
    style GraphBoxMean from GraphBoxMean /
      markersymbol=" diamondfilled "
      contrastcolor=GraphColors("gcdata1")
      markersize = 5px;
    style GraphOutlier from GraphOutlier /
      markersize = 5px
      markersymbol = " x "
      contrastcolor = GraphColors(" gcdata2 ");
    end;
run;
  • The DEFINE STYLE statement assigns the name BOXPLOT to our new style, and sets the STATISTICAL style as the parent style.
  • On the GraphBox style element, the CAPSTYLE= attribute is set to LINE, which removes the serif caps from the end of the fences. The DISPLAYOPTS= attribute drops the FILL value from the display list and adds the NOTCHES value; these changes determine that the graph will display empty, notched boxes.
  • On the GraphBoxMean style element, the marker symbol is changed to a filled diamond and the marker size is reduced to 5 pixels (the default is 9 pixels). The CONTRASTCOLOR= attribute is set to GCDATA1 (the default is GCDATA).
  • On the GraphBoxOutlier style element, the marker symbol is changed to an X and the marker size is reduced to 5 pixels (the default is 7 pixels). The CONTRASTCOLOR= attribute is set to GCDATA2 (the default is GCOUTLIER).
The following code generates a box plot for the BOXPLOT style:
title "Boxplot Style";
ods listing style=styles.boxplot;

proc sgplot data=sashelp.heart;
  hbox diastolic /
       category=weight_status;
run;
Box Plot with BOXPLOT Style
When making such style changes remember that you are affecting all box plot displays for all procedures that produce box plots when this style is in effect. It is possible to change the box plot appearance for specific procedures, but to do this, a specific graph template must be modified, not a style template.
For a comprehensive description of the style elements affecting ODS graphics, see the section for the style elements affecting template-based graphics in the Appendix for ODS Style Elements of the SAS Output Delivery System: User's Guide.