Set attributes of markers in PROC SGPLOT by using ODS style elements

10

The SG procedures in SAS use aesthetically pleasing default colors, shapes, and styles, but sometimes it is necessary to override the default attributes. The MARKERATTRS= option enables you to override the default colors, symbols, and sizes of markers in scatter plots and other graphs. Similarly, the LINEATTRS= option enables you to override the default line colors, thickness, and pattern (solid, dashed, dotted, and so forth).

Many programmers specify hard-coded values such as MARKERATTRS=(COLOR=red SYMBOL=CircleFilled). However, sometimes it is important for colors and symbols to match the style elements in the current ODS style. For example, you might want to match the colors and symbols that appear in some other graph.

Use ODS style elements to specify marker attributes in #SAS graphs. #DataViz Click To Tweet

An example is shown below. The data set is the Sashelp.Cars data, which contains information on car and trucks. The first scatter plot displays a scatter plot where each marker is colored according to the value of the Origin variable, which has the values "Asia," "Europe," and "USA." The second scatter plot uses the WHERE statement to restrict the display to vehicles that were manufactured in the USA. For simplicity, only the most important statements are shown. You can download the complete SAS program that generates these graphs.

title "Vehicles: All Origins"; 
proc sgplot data=sashelp.cars;
  scatter x=wheelbase y=weight / markerattrs=(symbol=CircleFilled) group=origin;
run;
 
title "Vehicles: Origin=USA only";
proc sgplot data=sashelp.cars;
  where origin="USA";
  scatter x=wheelbase y=weight / markerattrs=(symbol=CircleFilled);
run;
odsstyleattrs1

The graphs look the way they do because the SG procedures follow certain rules for displaying grouped versus non-grouped data. The first graph has three groups. The GROUP= option causes the SGPLOT procedure to assign the ODS element GraphData1 to the first group of markers ("Asia"). Similarly, the ODS element GraphData2 is assigned to the second group ("Europe") and the GraphData3 element is assigned to the third group ("USA"). (These names are described in the documentation for ODS style elements.) In the HTMLBlue style, which is the style used for these graphs, each markers is colored a shade of blue, red, or green according to its group membership.

The second plot shows only the "USA" vehicles. Whereas these markers were colored green in the first plot, they are colored blue in the second plot. This shade of blue comes from the GraphDataDefault element. The marker color is GraphDataDefault:ContrastColor, the marker size is GraphDataDefault:MarkerSize, and the marker symbol is GraphDataDefault:MarkerSymbol.

If you are displaying both graphs, you would certainly want the markers in the second graph to use the same shade of green as the "USA" vehicles in the first graph. This shade of green does not have a standard name, so you cannot specify a hard-coded color such as MARKERATTRS=(color=DarkGreen). Instead, use the MARKERATTRS= option to specify the marker attributes in the GraphData3 element, as follows:

proc sgplot data=sashelp.cars;
  where origin="USA";
  scatter x=wheelbase y=weight / markerattrs=GraphData3;
run;
odsstyleattrs2

The MARKERATTRS= option tells the scatter plot to use markers that have the color, symbol, and size of the GraphData3 style element. For the default HTMLBlue style, that means dark green and open circles, as shown.

The second plot now looks similar to the "USA" vehicles in the first plot. Using the GraphData3 style changed the color, but the symbol is incorrect. To override an attribute of a style element, specify the attribute in parentheses AFTER the style element. For this example, specify the SYMBOL= option in parentheses, as follows:
proc sgplot data=sashelp.cars;
  where origin="USA";
  scatter x=wheelbase y=weight / markerattrs=GraphData3(symbol=CircleFilled);
run;
odsstyleattrs3

Voila! The task is complete. The second plot now has the same marker attributes as the first plot. ( View the plots side by side.) If you were to change the ODS style from HTMLBlue to some other style, the colors would still match.

Notice that PROC SGPLOT does not support specifying the color by using the name of the style element. For example, the following is invalid in PROC SGPLOT:

scatter x=x y=y / markerattrs=(GraphData3:ContrastColor); /* NOT supported */

The Graph Template Language (GTL) supports specifying style elements using the "colon notation," but that syntax is not supported in the SG procedures.

The SAS documentation contains two sections that explain ODS style elements and how to use them in the SG procedures:

As indicated above, the same ideas apply to line attributes, region attributes, and so forth. For example, in a SERIES statement you can use LINEATTRS=GraphFit2(thickness=3) to assign attributes of the GraphFit2 style and ovverride the THICKNESS attribute.

Thanks to Sanjay Matange and Dan Heath for several discussions that improved my understanding of ODS style elements and attributes.

Share

About Author

Rick Wicklin

Distinguished Researcher in Computational Statistics

Rick Wicklin, PhD, is a distinguished researcher in computational statistics at SAS and is a principal developer of PROC IML and SAS/IML Studio. His areas of expertise include computational statistics, simulation, statistical graphics, and modern methods in statistical data analysis. Rick is author of the books Statistical Programming with SAS/IML Software and Simulating Data with SAS.

10 Comments

  1. Peter Lancashire on

    Thanks for this tip. You deduced that USA used the third attribute by visual inspection of the grouped graph. Is there a more general (programmatic) way for an arbitrary value of a grouping variable? I assume it would fail if you had more groups than attributes. Is 12 the absolute maximum or could you extend it in a custom style?

  2. Pingback: What colors does PROC SGPLOT use for markers? - The DO Loop

  3. Viktor Polyakov on

    Could I plot something like this below (scatter plot with gradual color response by a third variable) and add a fourth group variable identified by marker shape?
    For example, there will be two groups, circles and triangles, each having graduated color scheme all on the same x-y plot.

    scatter x=variable1 y=variable2 / colorresponse=variable3
    markerattrs=(symbol=CircleFilled size=7)
    colormodel=(CX3288BD CX99D594 CXE6F598 CXFEE08B CXFC8D59 CXD53E4F);

  4. Tathabbai Pakalapati on

    Hi,

    Is it possible to adjust the size of the marker based on the value on y-axis i.e. if y=10 i use size=2 and if y=5, I use size=1?

    Thank you

Leave A Reply

Back to Top