ODS Graphics components like GTL and SG procedures are designed to work with Styles to create graphs that are effective in the delivery of information and aesthetically pleasing out of the box. You no longer have to tweak the colors to ensure a nice graph. The graph derives all the visual attributes from the active style of the open destination.
Each ODS destination has a default style, and SAS ships many styles that can be used for your graphs. For analytical graphs, the recommended styles are Listing, Analysis, Statistical, Journal, Journal2 and Default.
With SAS 9.3 additional styles are available such as HTMLBlue. These styles have been carefully designed by experts to ensure the result is aesthetically pleasing, the graph colors are distince, yet balanced. Here is a common graph created using the LISTING style.
SAS 9.2 Code:
ods listing style=listing; title "Mileage by Horsepower"; proc sgplot data=sashelp.cars(where=(type='Sports')); scatter x=horsepower y=mpg_city / group=origin; keylegend / location=inside position=topright across=1; xaxis grid; yaxis grid; run;
Normally, when a group variable is not used, the visual attributes of a plot are derived from the GraphDataDefault style element. Also, visual attributes can be easily changed by using the MarkerAttr option.
In this graph, we have assigned GROUP=origin. When a group variable is in effect, the visual attributes for each group are derived from the GraphData1 - GraphData12 elements. By default, 12 elements are defined in most styles, but you can define more or less elements. In this case, there is no option in SAS 9.2 SG procedures or GTL to change the list of colors or marker symbols that are used with each group value.
If you have the need to use specific marker shapes or colors for each group in your graph, you have to create a new style that specifies these values in the style. While you can certainly edit a style, one way to quickly and painlessly create a style with custom marker shapes, colors, etc. is to use the MODSTYLE macro.
In the following example, we want to use the LISTING style, but want to use the CircleFilled, TriangleFilled and DiamondFilled symbols for the group values.
SAS 9.2 Code:
%modstyle(name=markers, parent=listing, type=CLM, markers=circlefilled trianglefilled diamondfilled); ods listing style=markers; title "Mileage by Horsepower"; proc sgplot data=sashelp.cars(where=(type='Sports')); scatter x=horsepower y=mpg_city / group=origin; keylegend / location=inside position=topright across=1; xaxis grid; yaxis grid; run;
Here we use the MODSTYLE macro to derive a new style named 'Markers' from the LISTING style, with the three markers specified. Other details of the style are unchanged. Then, we use the new style "Markers" as the style for the LISTING destination.
In the following graph, we have modified both the marker symbols and the marker colors. Note, in the ODS Styles, the marker and lines use the "ContrastColor" attribute in the style element. The macro parameter "Color" sets this contrast color in the style. The macro parameter "FillColor" sets the "Color" attribute in the style element, used for coloring filled areas such as bars, histograms and bands. We have also increased the size of each marker using the MarkerAttrs option.
SAS 9.2 Code:
%modstyle(name=colors, parent=listing, type=CLM, markers=circlefilled trianglefilled diamondfilled, colors=darkgreen purple orange); ods listing style=colors gpath=&gpath image_dpi=&dpi; title "Mileage by Horsepower"; proc sgplot data=sashelp.cars(where=(type='Sports')); scatter x=horsepower y=mpg_city / group=origin markerattrs=(size=11); keylegend / location=inside position=topright across=1; xaxis grid; yaxis grid; run;
Using a custom style by itself does not ensure that specific group values you want will be mapped to specific markers in the graph. The assignment of group colors 1-N is done in the order the observations are encountered in the data. One way of ensuring the assignment order in SAS 9.2 is by using the GTL option INDEX. The index is a value 1-N that directly maps to one of the group style elements.
In this case, we have created a new column in the data called "Index". we have specifically assigned index the values 1, 2 and 3 for Origin values of "USA", "Europe" and "Asia". Note, "Asia" is still encountered first in the data and is shown first in the legend, but now has the orange diamond symbol. The green circle is assigned to "USA". The INDEX option is not available in SG procedures, so you have to use GTL.
SAS 9.2 GTL Code:
/*--Template for color by index--*/ proc template; define statgraph index; begingraph; entrytitle 'Mileage by Horsepower'; layout overlay; scatterplot x=horsepower y=mpg_city / group=origin index=index markerattrs=(size=11) name='a'; discretelegend 'a' / location=inside halign=right valign=top across=1; endlayout; endgraph; end; run; /*--Create graph with assigned group index--*/ ods listing style=colors gpath=&gpath image_dpi=&dpi; proc sgrender data=cars template=index; run;
Note: A new feature called "Attribute Map" has been added with SAS 9.3 to deterministically assign group attributes to data values.
Full SAS Code: SAS92_Code