Statistical Graphics Using ODS


Changing the Default Markers and Lines

You can use the %MODSTYLE macro to change markers and line styles. This example creates a new style called MARKSTYLE that inherits attributes from the STATISTICAL style but uses a different set of markers. The following statements create artificial data, change the marker list, and display the results:

data x;
   do g = 1 to 12;
      do x = 1 to 10;
         y = 13 - g + sin(x * 0.1 * g);
         output;
      end;
   end;
run;

%modstyle(name=markstyle, parent=statistical, type=CLM,
          markers=star plus circle square diamond starfilled
                  circlefilled squarefilled diamondfilled)

ods listing style=markstyle;

proc sgplot;
   title 'Modified Marker List';
   loess y=y x=x / group=g;
run;

The NAME= option specifies the new style name, and the PARENT= option specifies the parent style. The TYPE= option controls the method of cycling through colors, lines, and markers. The default, TYPE=LMbyC, fixes (holds constant) the line styles and markers while cycling through the color list. This example uses TYPE=CLM to cycle through colors, line styles, and markers (holding none of them constant). Other TYPE= values are described in the section ODS Style Template Modification Macro. The values that are specified in the TYPE= option are case-sensitive ("by" is lowercase and the L, C, and M are uppercase). The new marker list is specified in the MARKERS= option. The results are displayed in Figure 21.70. The marker list is reused in the tenth and subsequent groups because only nine markers are defined.

Figure 21.70: A Modified Style with a New List of Markers

A Modified Style with a New List of Markers


The following statements create a new style called LINESTYLE that inherits attributes from the STATISTICAL style and changes the line list:

%modstyle(name=linestyle, parent=statistical, type=CLM,
          linestyles=Solid LongDash MediumDash Dash ShortDash Dot ThinDot)

ods listing style=linestyle;

proc sgplot;
   title 'Modified Line Style List';
   loess y=y x=x / group=g;
run;

The new line list is specified in the LINESTYLES= option. The results are displayed in Figure 21.71. In this example, each of the first seven groups uses a dash that is shorter than the dash in the previous group. The line list is reused in the eighth and subsequent groups because only seven line patterns are defined.

Figure 21.71: Modified Style with a New List of Line Styles

Modified Style with a New List of Line Styles


You can learn more about style modification by examining the new styles, as in the following example:

proc template;
   source styles.markstyle;
   source styles.linestyle;
run;

The results show the definitions of GraphData1 through GraphData32 that the macro created. An abridged listing of the results follows:

   define style Styles.Markstyle;
      parent = Styles.statistical;
      . . .
      style GraphData1 /
         markersymbol = "star"
         linestyle = 1
         contrastcolor = ColorStyles('c1')
         color = FillStyles('f1');
      . . .
      style GraphData32 /
         markersymbol = "diamond"
         linestyle = 42
         contrastcolor = ColorStyles('c8')
         color = FillStyles('f8');
   end;

   define style Styles.Linestyle;
      parent = Styles.statistical;
      . . .
      style GraphData1 /
         markersymbol = "circle"
         linestyle = 1
         contrastcolor = ColorStyles('c1')
         color = FillStyles('f1');
      . . .
      style GraphData32 /
         markersymbol = "triangle"
         linestyle = 20
         contrastcolor = ColorStyles('c8')
         color = FillStyles('f8');
   end;

You can use the NUMBEROFGROUPS= option in the %MODSTYLE macro to control the number of GraphDatan style elements that the new style creates.

Modifying Graph Fonts in Styles

You can modify an ODS style to customize the general appearance of plots that ODS Graphics produces, just as you can modify a style to customize the general appearance of ODS tables. This section shows you how to customize fonts that are used in graphs. The following step displays the HTMLBLUE style and its parent styles, STATISTICAL and DEFAULT:

proc template;
   source Styles.HTMLBlue / expand;
run;

If you search for "font", you find the style elements that control graph fonts:

   style GraphFonts /
      'GraphDataFont' = ("<sans-serif>, <MTsans-serif>",7pt)
      'GraphUnicodeFont' = ("<MTsans-serif-unicode>",9pt)
      'GraphValueFont' = ("<sans-serif>, <MTsans-serif>",9pt)
      'GraphLabel2Font' = ("<sans-serif>, <MTsans-serif>",10pt)
      'GraphLabelFont' = ("<sans-serif>, <MTsans-serif>",10pt)
      'GraphFootnoteFont' = ("<sans-serif>, <MTsans-serif>",10pt)
      'GraphTitleFont' = ("<sans-serif>, <MTsans-serif>",11pt,bold)
      'GraphTitle1Font' = ("<sans-serif>, <MTsans-serif>",14pt,bold)
      'GraphAnnoFont' = ("<sans-serif>, <MTsans-serif>",10pt);

The fonts GraphTitle1Font and GraphLabel2Font are not used by ODS Graphics. The following fonts are the ones that are usually used for the text in most graphs:

  • GraphDataFont is the smallest font. It is used for text that needs to be small (labels for points in scatter plots, labels for contours, and so on).

  • GraphValueFont is the next-largest font. It is used for axis value (tick mark) labels and legend entry labels.

  • GraphLabelFont is the next-largest font. It is used for axis labels and legend titles.

  • GraphFootnoteFont is the next-largest font. It is used for all footnotes.

  • GraphTitleFont is the largest font. It is used for all titles.

  • GraphUnicodeFont is used for special characters. (See the section Unicode and Special Characters in Chapter 22: ODS Graphics Template Modification.)

The following statements define a style named NEWSTYLE that replaces the graph fonts in the DEFAULT style with italic Times New Roman fonts, which are available in the Windows operating system:

proc template;
   define style Styles.NewStyle;
      parent=Styles.Statistical;
      replace GraphFonts  /
         'GraphDataFont'     = ("<MTserif>, Times New Roman",7pt)
         'GraphUnicodeFont'  = ("<MTserif>, Times New Roman",9pt)
         'GraphValueFont'    = ("<MTserif>, Times New Roman",9pt)
         'GraphLabel2Font'   = ("<MTserif>, Times New Roman",10pt)
         'GraphLabelFont'    = ("<MTserif>, Times New Roman",10pt)
         'GraphFootnoteFont' = ("<MTserif>, Times New Roman",10pt)
         'GraphTitleFont'    = ("<MTserif>, Times New Roman",11pt)
         'GraphTitle1Font'   = ("<MTserif>, Times New Roman",14pt)
         'GraphAnnoFont'     = ("<MTserif>, Times New Roman",10pt);
   end;
run;

For more information about the DEFINE, PARENT, and REPLACE statements, see the SAS Graph Template Language: Reference.

The "Getting Started" section in Chapter 98: The ROBUSTREG Procedure, creates the following data set to illustrate the use of PROC ROBUSTREG for robust regression:

data stack;
   input x1 x2 x3 y @@;
   datalines;
80 27 89 42   80 27 88 37   75 25 90 37   62 24 87 28   62 22 87 18
62 23 87 18   62 24 93 19   62 24 93 20   58 23 87 15   58 18 80 14
58 18 89 14   58 17 88 13   58 18 82 11   58 19 93 12   50 18 89  8
50 18 86  7   50 19 72  8   50 19 79  8   50 20 80  9   56 20 82 15
70 20 91 15
;

The following statements create a Q-Q plot that uses the HTMLBLUE style (see Figure 21.72) and the NEWSTYLE style (see Figure 21.73):

ods listing style=HTMLBlue;
ods graphics on;

proc robustreg data=stack plots=qqplot;
   ods select QQPlot;
   model y = x1 x2 x3;
run;
ods listing close;
ods listing style=NewStyle;

proc robustreg data=stack plots=qqplot;
   ods select QQPlot;
   model y = x1 x2 x3;
run;

Figure 21.72: Q-Q Plot That Uses the HTMLBLUE Style

Q-Q Plot That Uses the HTMLBLUE Style


Figure 21.73: Q-Q Plot That Uses the NEWSTYLE Style

Q-Q Plot That Uses the NEWSTYLE Style


Although this example illustrates the use of a style with graphical output from a particular procedure, a style is applied to all your output (graphs and tables) in the destination for which you specify the style. For information about specifying a default style for all your output, see the section Changing the Default Style.

Modifying Other Graph Elements in Styles

This section illustrates how to modify other style elements for graphics, specifically the style element GraphReference, which controls the attributes of reference lines. You can run the following statements to learn more about the GraphReference style element:

proc template;
   source styles.HTMLBlue;
run;

The following are the first two lines of the source listing:

define style Styles.HTMLBlue;
   parent = styles.statistical;

There is no mention of GraphReference in the template source listing because GraphReference is inherited from a parent style. The following step displays the HTMLBLUE style and its parent styles:

proc template;
   source Styles.HTMLBlue / expand;
run;

The EXPAND option lists the styles in the following order: style of interest, then its parent, and then its grandparent, and so on. The HTMLBLUE style inherits attributes from the STATISTICAL style, which inherits attributes from the DEFAULT style. If you search the results from the top, you will find the most recent specification of a style element first. The GraphReference style element is defined as follows:

   class GraphReference /
      linethickness = 1px
      linestyle = 1
      contrastcolor = GraphColors('greferencelines');

To specify a line thickness of 4 pixels for all reference lines, add the following statement to the definition of the NEWSTYLE style in the section Modifying Graph Fonts in Styles:

replace GraphReference / linethickness=4px;

The following statements modify the style and produce the Q-Q plot shown in Figure 21.74:

proc template;
   define style Styles.NewStyle;
      parent=Styles.Statistical;
      replace GraphFonts  /
         'GraphDataFont'     = ("<MTserif>, Times New Roman",7pt)
         'GraphUnicodeFont'  = ("<MTserif>, Times New Roman",9pt)
         'GraphValueFont'    = ("<MTserif>, Times New Roman",9pt)
         'GraphLabel2Font'   = ("<MTserif>, Times New Roman",10pt)
         'GraphLabelFont'    = ("<MTserif>, Times New Roman",10pt)
         'GraphFootnoteFont' = ("<MTserif>, Times New Roman",10pt)
         'GraphTitleFont'    = ("<MTserif>, Times New Roman",11pt)
         'GraphTitle1Font'   = ("<MTserif>, Times New Roman",14pt)
         'GraphAnnoFont'     = ("<MTserif>, Times New Roman",10pt);
      replace GraphReference / linethickness=4px;
   end;
run;
ods listing style=NewStyle;
ods graphics on;

proc robustreg data=stack plots=qqplot;
   ods select QQPlot;
   model y = x1 x2 x3;
run;

Figure 21.74: Q-Q Plot That Uses the NEWSTYLE Style with a Thicker Line

Q-Q Plot That Uses the NEWSTYLE Style with a Thicker Line


You can use this approach to modify other attributes of the line, such as LineStyle and ContrastColor. These style modifications apply to all graphs that display reference lines, not just Q-Q plots that are produced by PROC ROBUSTREG. You can control the attributes of specific graphs by modifying the graph template, as discussed in the section Graph Templates in Chapter 22: ODS Graphics Template Modification. Values that are specified directly in a graph template override style attributes.

When you are done with the NEWSTYLE style, you do not need to restore the HTMLBLUE style template, because you did not modify it. Rather, you inherited its attributes from the HTMLBLUE style.