Statistical Graphics Using ODS


Changing the Default Markers and Lines

The preceding section shows how to use the %MODSTYLE autocall macro to create an all-color style. You can also use the %MODSTYLE macro to change markers and line styles. This example creates a new style called MARKSTYLE that inherits 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 is illustrated in the section Creating an All-Color Style. This example uses TYPE=CLM to cycle through colors, line styles, and markers (holding none of them fixed). Other TYPE= values are described in the section Style Template Modification Macro. The values specified with the TYPE= option are case-sensitive ('by' is lower case and the 'L', 'C', and 'M' are upper case). The new marker list is specified with the MARKERS= option. The results are displayed in Figure 21.55. The marker list is reused in the tenth and subsequent groups since only nine markers are defined.

Figure 21.55: 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 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 with the LINESTYLES= option. The results are displayed in Figure 21.56. In this example, each of the first seven groups uses a dash pattern that is shorter than the previous group. The line list is reused in the eighth and subsequent groups since only seven line patterns are defined.

Figure 21.56: 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 created in the new style.

Modifying Graph Fonts in Styles

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

proc template;
   source Styles.HTMLBlue;
   source Styles.Statistical;
   source Styles.Default;
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 with ODS Graphics. The following fonts are the ones typically 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 marks) 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 with 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 of Chapter 86: The ROBUSTREG Procedure, creates the following data set to illustrate the use of the 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.57) and the NEWSTYLE style (see Figure 21.58):

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.57: Q-Q Plot That Uses the HTMLBLUE Style

Q-Q Plot That Uses the HTMLBLUE Style


Figure 21.58: 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 of your output (graphs and tables) in the destination for which you specify the style. See the section Changing the Default Style for information about specifying a default style for all your output.

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 complete listing of the source because GraphReference is inherited from a parent style. Most styles inherit many of their attributes from other styles. To find out more, you must list the parent style, as in the following example:

proc template;
   source styles.statistical;
run;

Most styles that you typically use with ODS Graphics inherit most of their attributes from only one style, the DEFAULT style. The HTMLBLUE style inherits from the STATISTICAL style, which inherits from the DEFAULT style. A few of the other styles inherit from several parents. You might have to repeat this process multiple times to find the first parent. The following step displays the HTMLBLUE style and its parent styles, STATISTICAL and DEFAULT:

proc template;
   source Styles.HTMLBlue;
   source Styles.Statistical;
   source Styles.Default;
run;

Styles are listed in the order: style of interest, then its parent, and then its grandparent. 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.59:

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.59: 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, and not just to Q-Q plots 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 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 since you did not modify it. Rather, you inherited from the HTMLBLUE style.