23
1 Using Graph Designer for High-Quality Graphs Sanjay Matange, SAS Institute, Cary, NC ABSTRACT Creating high quality graphs became a whole lot easier with the release of ODS Graph Designer. Traditionally, SAS/GRAPH has provided powerful procedure syntax to create graphs. With SAS 9.2, GTL and SG Procedures were released to help create complex analytical graphs. Now, the ODS Graphics Designer (Designer) completes the circle by creating the same graphs using interactive “point and click” actions. Many of you prefer to create your graph using an interactive application, and now you can do that within SAS. In this hands-on-workshop, we will introduce you to the application interface and work through some real world examples. INTRODUCTION Graph Designer is a new interactive application released with SAS 9.2M3 as part of the SAS/GRAPH product. This application is designed for the user who prefers to create their graphs using an interactive application. It presents you with a graphical user interface that takes the pain out of creating graphs, and makes it easy. Designer is based on Graph Template Language (GTL), the syntax used to create analytical graphs now produced by the SAS analytical procedures. GTL is a very extensive syntax, designed to allow the procedure writer all the features and flexibility they need to create their complex graphs. Designer uses the “Pareto principle” also known as the 80-20 rule to drive a subset of the GTL features that are necessary to create many different graphs. DESIGNER APPLICATION INTERFACE Figure 1 shows the Designer application interface that is started by submitting the following macro code from your program editor window: %sgdesign; <submit> Figure 1 Graph Gallery Plot Layers

Using Graph Designer for High-Quality Graphs Sanjay ... · graph that has a histogram of the scores, along with a couple of distribution curves. We also want to see the various severity

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Using Graph Designer for High-Quality Graphs Sanjay ... · graph that has a histogram of the scores, along with a couple of distribution curves. We also want to see the various severity

1

Using Graph Designer for High-Quality Graphs

Sanjay Matange, SAS Institute, Cary, NC

ABSTRACT Creating high quality graphs became a whole lot easier with the release of ODS Graph Designer. Traditionally, SAS/GRAPH has provided powerful procedure syntax to create graphs. With SAS 9.2, GTL and SG Procedures

were released to help create complex analytical graphs. Now, the ODS Graphics Designer (Designer) completes the circle by creating the same graphs using interactive “point and click” actions. Many of you prefer to create your graph using an interactive application, and now you can do that within SAS. In this

hands-on-workshop, we will introduce you to the application interface and work through some real world examples.

INTRODUCTION

Graph Designer is a new interactive application released with SAS 9.2M3 as part of the SAS/GRAPH product. This

application is designed for the user who prefers to create their graphs using an interactive application. It presents you with a graphical user interface that takes the pain out of creating graphs, and makes it easy.

Designer is based on Graph Template Language (GTL), the syntax used to create analytical graphs now produced by

the SAS analytical procedures. GTL is a very extensive syntax, designed to allow the procedure writer all the features and flexibility they need to create their complex graphs. Designer uses the “Pareto principle” also known as

the 80-20 rule to drive a subset of the GTL features that are necessary to create many different graphs.

DESIGNER APPLICATION INTERFACE

Figure 1 shows the Designer application interface that is started by submitting the following macro code from your

program editor window:

%sgdesign; <submit>

Figure 1

Graph Gallery

Plot Layers

Page 2: Using Graph Designer for High-Quality Graphs Sanjay ... · graph that has a histogram of the scores, along with a couple of distribution curves. We also want to see the various severity

2

While this code is required now, with future releases of SAS, you will be able to launch the application directly from

the Tools menu.

For this workshop, open the file “StartDesigner.sas” from the default location. This contains a libname statement to

assign the library for the data sets we will use in this workshop. It also has the macro to start Designer. Submit the

program. If running Designer for the first time, you will get a message about creating a “preference” file. Click OK.

Exercise 1: Exploring the User Interface

When first launched, the Designer interface appears as shown in Figure 1. The key parts of the interface are as

follows:

1. The standard menus for „File‟ „Edit‟ „View‟ „Insert‟ „Format‟, „Tools‟ and „Help‟ are shown at the top

2. A tool bar for frequent actions is shown below that.

3. On the left side are two stacked panels, for „Plot Layers‟ and „Insets‟. Both are currently inactive.

4. On the right side is the workspace with the „Graph Gallery‟.

Graph Gallery:

When Designer first starts, it displays the Graph Gallery in the work space. The Graph Gallery contains a set of

commonly used graphs that can be used to start your graph design. This gallery is organized into six groups. Let us explore the different groups included in the gallery.

1. Basic - A set of common “singleton” graphs such as Scatter Plot, Histogram, etc.

2. Grouped - A set of common plots with a group role.

3. Analytical – A set of graphs commonly used for analysis of data.

4. Custom - A set of unique custom graphs.

5. Matrix - A set of matrix graphs used to visualize variable associations.

6. Panels - A set of classification panels for comparative graphs.

Figure 2 – Graph Gallery groups

Page 3: Using Graph Designer for High-Quality Graphs Sanjay ... · graph that has a histogram of the scores, along with a couple of distribution curves. We also want to see the various severity

3

This gallery is editable as we will see subsequent Exercises. The purpose of the gallery is to provide you a quick

view of the many different types of graphs you can create, and provide a starting point for creating you graph.

Plot Layers and Insets:

On the left side of the application window is the panel of Plot Layers and Insets. Initially these panels are inactive.

Once we begin the process of creating a graph, these will become active, and their usage will become clear. Feel free to explore the application interface now.

Exercise 2: Create a Distribution Graph (Single Cell)

A new graph can be created starting from a blank graph, or from a graph included in the Graph Gallery. As you can

see in Figure 2, the Graph Gallery contains many types of graphs:

Simple single cell graphs with only one plot layer.

Single cell graphs with multiple plot layers together.

Multi cell graphs with different graphs in each cell

Multi cell classification panels, where each cell has the same graph.

Let us create a graph starting from the gallery. To create a graph, you can just pick a graph that comes closest to the

type of graph you want. Often, the starting point is one of the Basic graphs.

In this exercise, we want to create a graph to plot the distribution of the Hamilton Scores for Depression for a group of

subjects in a study. The data set, DEPRESS2, is in the “Data” library we already assigned. We want to create a graph that has a histogram of the scores, along with a couple of distribution curves. We also want to see the various severity levels of the disease.

Step 1: Create the basic Histogram.

If the Graph Gallery is not visible, select View->Graph Gallery.

Select the Basic tab. Click the Histogram. Click OK.

A graph will appear showing a Histogram as shown in Figure 3A.

The Assign Data dialog is displayed with the data settings for the histogram (Figure 3A).

o Note: This histogram is merely a place holder, to indicate the type of plot being customized.

o This place holder uses the SASHELP.CLASS data set, with X=HEIGHT.

Figure 3A Figure 3B

Page 4: Using Graph Designer for High-Quality Graphs Sanjay ... · graph that has a histogram of the scores, along with a couple of distribution curves. We also want to see the various severity

4

For our graph, we need to provide the LIBRARY, DATASET and VARIABLE that we want to graph.

o Click on the Library drop down arrow, and select “Data”.

o Click on the Data Set drop down arrow, and select DEPRESS2.

At this stage, the setting for X=HEIGHT is no longer valid as this column does not exist in the

DEPRESS2 data set. The X setting is cleared, and the Plot list and the Histogram are shown in red, indicating that the current data assignments are not correct. This is shown in Figure 4A.

Let us now assign the right variable to the X role.

o Click on the drop down arrow of the X role, and select the variable BASVAL.

o Now, the required roles for the histogram are assigned, and the plot name is no longer red.

o Since there is only one plot in this graph, the Plot list name is also no longer red. (Figure 4B).

o Note the name of the plot is “histogram”. This can be changed, but in this case we will leave this

as is. We will use it later. Click OK.

Figure 4A Figure 4B

The Assign Data dialog for the Histogram is dismissed, and the graph is now showing the histogram we

want for the variable BASVAL, as shown in Figure 5.

Step 1 is almost complete. At this stage we have a

graph with a histogram showing the distribution of the Hamilton scores for all the patients in the study. Let us review the elements of the graph so far:

A place holder title at the top

A place holder footnote at the bottom

A histogram of BASVAL.

At this point, double click on the title and enter the title

you want, like “Distribution of Hamilton Scores for Study”.

We could do the same with footnote, but we‟ll just

remove it. Right click on the footnote, and select

“Remove Footnote”.

Notice the following:

The Graph Gallery is now hidden.

The Plot Layers and Inset panel at the left side are now active.

Figure 5

Page 5: Using Graph Designer for High-Quality Graphs Sanjay ... · graph that has a histogram of the scores, along with a couple of distribution curves. We also want to see the various severity

5

Step 2: Add the Normal Density Curve.

Once a graph is open in the workspace, the “Plot Layers” and “Insets” panels on the left side of the

workspace are active.

To add a plot to an existing graph, simply click on the plot icon, drag and drop it on the graph.

Figure 5A Figure 5B

To add a Normal Curve, click and hold the mouse button on the icon for “Normal”, and drag and drop it

on top of the histogram.

If the new plot is compatible with the graph contents, the drop will be accepted, and the “Assign Data” dialog for the

Normal Curve will be displayed as shown in Figure 5B.

Note in Figure 5B, the settings for Library and Data Set are greyed out, and cannot

be changed. This is because all plots in one cell of a graph must come from a single data set.

Note the “Fit to existing plot” check box in

Figure 5B. This is a convenience feature that allows you to select the data for this plot from a plot that is already in the graph. The names of the plots you can chose

from are listed in the drop down box. In this case there is only one plot – the histogram.

We will accept this setting since we want

to use the same X variable as the histogram. However, in certain special cases you could uncheck this box, and select another variable. Click OK.

A Normal Density Curve is added to the graph for the same X variable.

Figure 6

Page 6: Using Graph Designer for High-Quality Graphs Sanjay ... · graph that has a histogram of the scores, along with a couple of distribution curves. We also want to see the various severity

6

Step 3: Selection

At this point we have two plots overlaid in

the graph. The plots are drawn in the order they are placed in the graph. In this case, the histogram is drawn first, and the Normal

curve is drawn on top of it.

Clicking on a plot selects it for editing. The selected plot is drawn in full strength, while all other plots in the cell are “dimmed”.

Click on each plot to see the effect. Click in the background to see all plots at as they will be in the final output graph.

When a plot is selected, you can edit its

data or plot attributes. Right click the mouse button on the Normal Curve, and you will see the standard Graph popup menu as shown in Figure 7.

The Graph popup menu is shown in more detail in Figure 8. The entries can be used as follows:

o Assign Data – Displays the menu to reassign the variable and

other settings for this plot. o Plot Properties - Displays the menu to set visual properties for

the plot, such as color, line thickness and so on. o Axis Properties – Displays the menu to customize the axis. o Cell Contents – Displays all the plots and insets in this cell. You

can use this to change the drawing order of the plots, or remove a

plot from the cell. o Add an Element – Displays a panel of the plots and insets that

can be added to the cell. This is an alternative to dragging a plot from the Plot Layers panel.

o Remove Plot „normal‟ – remove the selected plot. o Add a Row – We will see how this works soon. o Add a Column – We will see how this works soon. o Graph Properties – Set the properties for the whole graph.

o Some entries are greyed out as they are not applicable to the current graph. We will discuss these later.

Assign Data. The Assign Data dialog for the Normal density plot is shown in Figure 9A. This dialog performs

the same function as the first Assign Data dialog that was displayed when the Normal curve was added to the graph. The main difference is that this dialog contains the “Plot” drop down list. All the plots in the cell are included in this list, and the selected plot (Normal) is preselected. However, you can customize any plot in this

cell by just selecting its name from the list. Also note, the Library and Data Set selectors are active. You can change the data set name, and as long as all the variable names (of the right type) are found in the new data set, all will be well. If some variable is not found,

or is of the wrong type, the plot will go “red” as discussed in Figure 4A. Plot Names. This is a good time to discuss plot names. Every plot added to a graph is assigned a default

name. In our example, we get “histogram” and “normal”. If you add two Series plots one for Drug A and one for

Drug B, the default assigned names are “Series1”, “Series2”. To better recall which is which, it is useful to change the default name to something more useful, such as “Drug A” and “Drug B”. These names will be listed in the drop down box. You can change the name any time.

Figure 7

Figure 8

Page 7: Using Graph Designer for High-Quality Graphs Sanjay ... · graph that has a histogram of the scores, along with a couple of distribution curves. We also want to see the various severity

7

Plot Properties. The Plot Properties dialog for the Normal density plot is shown in Figure 9B. This dialog can

be used to customize the visual properties for the selected plot. This dialog too has a “Plot” drop down box that

includes the names of all plots in the cell. Since Normal was selected, it is preselected in the list. However, you can customize the visual properties of any plot in the cell. Note, the name of this dialog is actually “Cell Properties”. This is because this same dialog is used to customize

other items in the cell, such as the axis, or the general features of the cell, based on the tab selected at the top. In Figure 9B, the “Plots” tab is selected.

Figure 9A Figure 9B

Axis properties. Figure 10A shows the Axis properties tab, with X axis already selected.

General. Figure 10B shows the General cell properties tab.

Figure 10A Figure 10B

Page 8: Using Graph Designer for High-Quality Graphs Sanjay ... · graph that has a histogram of the scores, along with a couple of distribution curves. We also want to see the various severity

8

Set the Y Axis Grid Iines. While we are in the Axis Properties dialog, let us change the axis to Y, and check the

“Grid” box to get grid lines on the Y axis. This dialog is also accessible by clicking the right mouse button on the axis itself.

Step 4: Add severity ranges.

Figure 6 is a good start for a distribution plot. However, for better interpretation of the distribution, it would

be nice to be able to see the severity ranges for the Hamilton scores. As per the information found on the web, the interpretation of the scores is as follows:

0 – 6 : Normal

7 – 17 : Mild

18 – 24 : Moderate

Over 25 : Severe

We have already add these values into the DEPRESS2 data set, the range values as the variable “Score” and the level as the variable “Level”. Now, let us use these columns to add the ranges and their labels to the graph.

We will do this by using a special plot called the BLOCKPLOT. This plot that creates horizontal boxes when the “block” variable value is the contiguous. A block is created and labeled with the value.

Drag and drop the “Block” plot from the Plot Layers panel on top of the histogram. The Assign Data dialog for the BlockPlot is shown. Set X=Score and Y=Level as shown in Figure 11A. Click OK.

The Block Plot is added to the graph. Note, the Block Plot fills the entire space in the data area, so

it is automatically placed behind all other plots, as shown in Figure 11B.

Now, we have the bands, but the values are not yet displayed. To do that we need to set some visual properties of the Block Plot. Click the right mouse button on the block plot, and select “Plot Properties”

Figure 11A Figure 11B

The Plot Properties dialog for the Block Plot is shown in Figure 12A. In the “Display” tab, enable the “Values” check box to display the block values. Note, in the graph, the values are now shown,

though in the center-left in each block. The “Value” tab is now active.

Click the “Value” tab in the dialog. In the “Alignment” block, select Horizontal=Center and Vertical=Top. Click OK.

Page 9: Using Graph Designer for High-Quality Graphs Sanjay ... · graph that has a histogram of the scores, along with a couple of distribution curves. We also want to see the various severity

9

Figure 12A Figure 12B

The final graph is shown below in Figure 13. Note, after the Block Plot is added, it becomes the

“Primary” plot since it is drawn first. Hence the X axis label becomes “Score” based on the X variable for the Block Plot. This can be corrected by setting the X axis label from the Axis Dialog to the desired string

Use File->Save As to save this graph as “Ex2.sgd” to the file system for future usage.

Note:

You can copy this graph as in image to the system clip board using Edit->Copy. Paste it into an email, Word

document or Power point directly.

Figure 13

Page 10: Using Graph Designer for High-Quality Graphs Sanjay ... · graph that has a histogram of the scores, along with a couple of distribution curves. We also want to see the various severity

10

Exercise 3: Designer is a good learning tool for GTL.

Designer is an good learning tool for GTL. Designer uses GTL behind the covers to create the graphs, so Designer is

essentially a value added interactive wrapper on GTL. As we create the graphs interactively, the GTL template is built for us step by step. You can view this template by selecting View->Code, as shown in Figure 13A.

Figure 13A

The GTL template and proc SGRender code are displayed for the active graph as shown in Figure 13B. You can

leave the code view open as you build the graph to see each step. Note the structure of the Graph.

The template uses a Layout Lattice as the outer most container. The need for this will become clear later.

A Layout Overlay is used to stack all the plots, one on top of the previous.

The statements for BlockPlot, Histogram and Density are included with the appropriate options.

Figure 13B

Page 11: Using Graph Designer for High-Quality Graphs Sanjay ... · graph that has a histogram of the scores, along with a couple of distribution curves. We also want to see the various severity

11

Exercise 4: Change Graph Properties and save to Graph Gallery.

Often, we have to embed the graph in a presentation or a document. A graph is often created at the default size

(640ps x 480px). Think of this as a graph that is 6.4 inches @ 100 dpi. When saved as a png image file, the graph is saved with a default DPI of 100. If this graph image is imported in to a document, but default, it will be displayed at its DPI. In this case, the graph would look right at a size of 6.4 inches width in the document. At this size, the fonts will

look as originally intended.

However, often we want to insert the graph in a smaller region, like we did in Figure 11B and Figure 13. In each

case, the graph was scaled down to fit the region, and the fonts became smaller. In Figure 11B, the fonts are not easily readable. To retain the font sizes, it is better to create the original graph as close as possible to the final size in the document.

Also, you may want to change the overall visual appearance of the graph. This can be done by selecting one of the

predefined styles shipped with SAS.

Step 1: Set Graph Size and Style

Open the graph “EX2.sgd” from the file system. This is also available under File->Open Recent

When using File->Open, the SGD files are shown with an icon of the graph.

Right click in the graph, and select Graph Properties. The dialog is shown if Figure 14A.

Set the Width =400. Since “Keep Aspect” is checked, the height will change to 300.

Click on the drop down arrow of the Style selector at the top, and select “Analysis”.

The graph is now shown in the Analysis style, in the size specified.

This graph is included in Figure 14B below. Note: Since the graph is given the same size as its original

dimension, the font sizes are retained, and quite readable.

Step 2: Reducing clutter in the graph

Since the title of the graph mentions what we are viewing, we can remove the X and Y axis labels

without loss of information. This reduces clutter and improves the effectiveness of the graph.

Right click on the X axis, and select Axis Properties. In the dialog, uncheck the “Label” box.

In the “Axis” drop down at the top, select the “Y” axis. Uncheck the “label” box. Click OK.

Figure 14A Figure 14B

Page 12: Using Graph Designer for High-Quality Graphs Sanjay ... · graph that has a histogram of the scores, along with a couple of distribution curves. We also want to see the various severity

12

Step 3: Save to Graph Gallery

As mentioned in Exercise 1, the Graph Gallery is editable and user customizable. The graphs in the six tabs

originally shipped with the application are immutable. However, you can customize the view using the “organize” button at the bottom of the gallery, by hiding graphs or groups.

You can also add custom tabs to the gallery, and add your own graphs to the gallery for future access. We will

save the graph shown in Figure 14B to the Graph Gallery.

Select the graph, and then select File->Save In Graph Gallery.

The “Save in Graph Gallery” dialog is displayed as shown if Figure 15A.

If there are no user groups in the gallery, create a new one using the “New” dialog at top right.

The group name is displayed.

Add the Graph name.

The default icon for the graph is created for you from the graph itself. You can use it, or pick your own icon image from the file system by using the “Browse” button.

Provide a „”Tool Tip” name for the graph (optional). Click OK.

The graph is added to the Graph Gallery under the new tab “My Graphs” as shown in Figure 15B.

Figure 15A Figure 15B

Exercise 5: Setting Plot Properties and Legend.

To illustrate this feature, let us first add another plot to the graph created in Exercise 3.

Step 1: Add a Kernel Density Plot.

Get the “Hamilton” graph from the Graph Gallery.

Change the Style back to Listing (using the Graph Properties dialog).

Save this graph as “Ex4.sgd” for future use.

In the Plot Layers panel, find the icon for the “Kernel” density function

Click this icon, and drag and drop it on the graph.

The Assign Data dialog will be displayed. Accept the default settings and click OK.

The Kernel Density Estimate curve is added to the graph as shown in Figure 16A.

Note, when the new plot is added, it uses the default style element for its visual settings. For density

curves, the default element is GraphFit. Now, both the Normal and Kernel density curves use the same style element, hence they both have the same visual attributes.

Page 13: Using Graph Designer for High-Quality Graphs Sanjay ... · graph that has a histogram of the scores, along with a couple of distribution curves. We also want to see the various severity

13

Figure 16A Figure 16B

Click the right mouse button on the Kernel density curve. It should be get selected, and the Graph

popup menu is shown. Select the “Plot Properties” menu item.

The Plot Properties dialog is displayed as shown in figure 17A.

Note, the assigned style element is GraphFit.

Change this to GraphFit2, which has distinct visual properties. Select OK.

Figure 17A Figure 17B

Page 14: Using Graph Designer for High-Quality Graphs Sanjay ... · graph that has a histogram of the scores, along with a couple of distribution curves. We also want to see the various severity

14

The resulting graph is shown in Figure 18.

To identify the density curves, a legend is

needed. We can add a legend at the bottom of the graph by selecting Insert->Global Legend from the menus.

The Global Legend dialog is displayed as

shown in Figure 19A. Select the plots to be included in the graph.

Edit the display names if needed. Click OK.

The resulting graph is shown in Figure 19B

Note: A legend could be added inside the data area too.

We could have done that if we did not have the BLOCK

PLOT. In this case, an inside legend would cause clutter.

Figure 19A Figure 19B

Exercise 6: Multi Cell Graph.

So far, we have been working with a single cell graph. A graph of this type has one graph area in the middle, with

titles, footnotes, and legends, like the graph in Figure 19B. The graph may have multiple plots overlaid in the

common data area. In the graph shown in Figure 19B, four different plots make up this graph. The data area, along with the axes is called a “Cell”

A multi cell graph has multiple cells in the graph, along with the optional titles, footnotes and legends. The cells are in

a regular grid of rows and columns. Let us add a horizontal box plot in a separate cell to get another view of the distribution of the score data.

Step 1: Add a Row

Open the graph in file “EX4.sgd”.

Press the right mouse button on the graph, and select “Add a Row” from the graph pop up menu as

shown in Figure 20A.

The graph region is split horizontally, and a new row is added to the graph. Now the graph has two

rows of cells, each row has only one column as shown in Figure 20B

The previous graph is retained in the upper cell, and is reformatted to fit that cell.

Figure 18

Page 15: Using Graph Designer for High-Quality Graphs Sanjay ... · graph that has a histogram of the scores, along with a couple of distribution curves. We also want to see the various severity

15

Figure 20A Figure 20B

Step 2: Populate the new cell with an HBox.

The new empty cell has a hint in it “drop a plot here”.

Now, we can populate this new empty cell in the same way as we have been adding plots to cells

previously. Just drag and drop a plot icon from the Plot Layers Panel.

Let us add a horizontal Box Plot to the new cell.

Click on the Box(H) icon, drag and drop it in the empty cell area. The familiar Assign Data dialog is

displayed as shown in Figure 21 A.

Set Y=BASVAL, the same variable as for the histogram. Click OK.

The Horizontal Box Plot is added to the lower cell, along with its own X axis as shown in Figure 21B

Figure 21A Figure 21B

Step 3: Set Common Uniform axes

If you look carefully, you will note the X axes for the two cells are not exactly aligned. This is because these are plots in two independent cells, and do not know about each other.

Page 16: Using Graph Designer for High-Quality Graphs Sanjay ... · graph that has a histogram of the scores, along with a couple of distribution curves. We also want to see the various severity

16

For our purposes, it is important that the two plots line up correctly. Also, we don‟t really want to invest

valuable graph space to duplicate axes. So, we want to have one common X axis for both cells.

Right click on any of the X axes and select “Common Column Axis” as shown in Figure 22A.

The graph will now only have one X axis at the bottom of the graph. Each cell still has equal heights.

Figure 22A Figure 22B

We can resize the heights of the cells to provide more space to the upper cell. When you hover the

cursor in the middle of the two cells, you will see a horizontal dotted line, with an indicator. Click and drag this line down to reduce the space for the lower graph. The result is as shown in Figure 22B.

Save the graph as “Ex6.sgd”.

Exercise 7: Multi Cell 2x2 Graph.

We saw how to build a graph starting with the Histogram from Graph Gallery. Then, we added more plots from the

Plot Layers panel. We also added a 2nd

row with a Box Plot. Now, let us build a 2x2 graph from a blank graph adding plots using the “Add an Element” feature. We will use data.heart data set.

Step 1: Open a blank graph.

Select File->New->Blank Graph. Or, click on the first icon in the tool bar (New Blank Graph).

A new blank graph is added in the workspace with a hint “(drop a plot here…)”.

Click the right mouse button in the graph, and select “Add a Row‟. See Figure 23A.

Click the right mouse button in any row and select “Add a Column”. See Figure 23B.

Figure 23A Figure 23B

Page 17: Using Graph Designer for High-Quality Graphs Sanjay ... · graph that has a histogram of the scores, along with a couple of distribution curves. We also want to see the various severity

17

Step 2: Add Scatter Plots – Using “Add An Element”

Click the right mouse in the top left cell, and select “Add an Element”, as shown in Figure 24A.

The “Add an Element” dialog is shown. This dialog contains all the elements that can be added to a

cell, similar to the Plot Layers and Insets panel as shown in Figure 24B

Figure 24A Figure 24B

Select the scatter plot, and the familiar “Assign Data” dialog is shown.

Set Libname=data, Data Set Name=heart X=weight, Y=systolic. Click OK.

The systolic by weight scatter plot is added to the top left cell.

Repeat the same process and add a scatter plot of diastolic by weight in the top right cell.

Repeat the same process and add an hBar of mean weight by deathcause in the bottom left cell.

Repeat the same process and add an hBar of mean cholesterol by deathcause in the bottom left cell.

In the Plot Properties for the two bar charts, go to “Bar” tab, and check the Bar Labels box.

In the Plot Properties for the two scatter plots, set marker symbol to filled circle and transparency=90%.

Add a title. You should have a graph as shown in Figure 25 below.

Figure 25

Page 18: Using Graph Designer for High-Quality Graphs Sanjay ... · graph that has a histogram of the scores, along with a couple of distribution curves. We also want to see the various severity

18

Step 3: Set Uniform Axes

Since the data ranges for Systolic and Diastolic blood pressures are different, the graph in Figure 25

does not allow for good comparison of this data. We need uniform Y axes.

Since the data for both rows is of same type, we can make the Row axes uniform.

Click on the one of the Y axes, and select “Common Row Axes”. Also, turn the gridlines on.

Click on the top row axis label, and type “Blood Pressure”.

Now, to identify Systolic or Diastolic, drag and drop a „Text Entry” to the top of each scatter plots, and

change the text to “Systolic” and “Diastolic” each.

The resulting graph is shown Figure 26.

Note: If the y axes of all graphs in a row are not the same, setting common external axes can have unpredictable

results. Common external axes work best for numeric data ranges. For categorical data, it works well if the categories are the same. We did not make column axes uniform.

Exercise 8: Classification Panel.

The example shown in Exercise 7 created a multi-cell graph, where each cell contains independent graphs, with or

without uniform axes. Another type of important graph is the Classification Panel, where the graph is split into multiple cells based on data in the data set itself.

The split is based on the list of class variables. If two class variables are provided, and each have 2 and 3 distinct

values each, then the graph is split into 6 cells. Each cell contains exactly the same type of graph, for the subset of

the data that is in the cell.

Designer supports two kinds of Classification Panels:

Data Lattice - This accepts up to two class variable, one for Row and one for Column

Data Panel – This accepts up to two class variables, and creates cells for the combinations.

Let us create a graph to view the mileage for various cars by country of Origin, classified by Type from the data.cars

data set. We will use the Data Lattice type graph. The Data Panel will be left to reader as an exercise.

Figure 26

Page 19: Using Graph Designer for High-Quality Graphs Sanjay ... · graph that has a histogram of the scores, along with a couple of distribution curves. We also want to see the various severity

19

Step 1: Start from a blank graph and create a Bar Chart.

Click on the first icon in the tool bar (New Blank Graph) to create a new blank graph.

Drag and drop a Bar element on the graph. The “Assign Data” dialog is shown.

Set Library = Data and Data Set = Cars.

Set Category = ORIGIN, Response = MPG_CITY, Statistic = MEAN and Name=City.

Click OK. A bar Chart of MPG (City) by Origin is added as shown in Figure 27A

Drag and drop another Bar element on the first one. The “Assign Data” dialog is shown.

Set Library = Data and Data Set = Cars.

Set Category = ORIGIN, Response = MPG_HIGHWAY, Statistic = MEAN and Name=highway.

Click OK. The second (narrower) bar chart is added.

Use the Plot Properties for each to set their elements to GraphData1 and Graph Data 2.

Set the Bar Skins to Modern and Transparency to 30% each. Click OK.

Use the Insert->Global Legend to add a legend, with all plots included. Result is Figure 27B.

Step 2: Turn this into a Classification Panel.

Right click on a bar chart and select “Assign Data”.

Figure 27A Figure 27B

Figure 28A Figure 28B

Page 20: Using Graph Designer for High-Quality Graphs Sanjay ... · graph that has a histogram of the scores, along with a couple of distribution curves. We also want to see the various severity

20

The “Assign Data” dialog is displayed as shown in Figure 28A.

Note that so far, we have always been using the “Plot Variables” tab of this dialog to set various plot

related roles. To the left of this tab is the “Panel Variables” tab. We will use this tab to set the class variables. Click on the “Panel Variables” tab.

The Panel Variables tab is shown in Figure 28B. Note that two types of layouts are available, shown as

“Data Lattice” and” Data Panel”. Data Lattice is selected by default, and we will keep it.

Set Column = Type. Click OK.

The graph is split into three cells, one for each type of car in the data set.

Change Y axis label to “MPG” and turn on the Y axis gridlines.

In the Graph Dialog, uncheck the “Keep Aspect” box and set graph height to 320 pixels.

The resulting graph is shown in Figure 29. Save this graph as EX8.sgd

Figure 29

Step 3: Start from a Gallery Panel Graph

You can also create a panel graph starting from the

“Panels” tab of the Graph Gallery. In that tab you will

find a few different panel layouts.

Choose a layout closest to what you want. Don‟t

worry about the contents of the cells at this time.

In this case, we actually have an entry very similar to

what we want (Paneled Vertical Bar). But to illustrate the point above, let use chose the “Column Lattice” in the first row. Click OK.

The expected graph is shown, along with the “Assign

Data” dialog, with the “Panel Variables” tab selected.

First, change the libname = data and DataSet = Cars.

Note, the panel variable settings are cleared.

Chose Column= Type.

Select the “Plot Variables” tab. Note the “Plots”

indicator is red, and also the Scatter plot in the list is red as shown in Figure 30. This indicates the plot does not have enough information to render itself.

Click OK.

A warning dialog is displayed as shown in Figure 31. Click Continue.

Figure 30

Page 21: Using Graph Designer for High-Quality Graphs Sanjay ... · graph that has a histogram of the scores, along with a couple of distribution curves. We also want to see the various severity

21

The cell contents are cleared and the graph shown contains three empty cells.

Use the Graph Panel to set the graph height to 240 px, keeping the width the same (uncheck the “Keep

Aspect Ratio”) box. Resulting graph is shown in Figure 32.

Figure 32

Now, you can drag and drop the Bar statements from the Plot Layers Panel, and customize them just

like we did in Step 1. You can drop them in any of the cells, and all the cells will get the same graph.

In Step 1, we first created the single cell, and then split the graph into a panel by setting the Column

role in the Panels tab. In Step 3, we first set up the 3 cell panel, and then populated it with the needed graphs.

Exercise 9: Running Designer graphs in batch.

Once you have created your graph interactively, often you may want to run the same graph in a batch process. You

can easily run Designer graphs in batch and send the output to the open ODS destination. For example:

You can run a saved SGD file in batch using the SGDESIGN Procedure.

You can run the same graph with different data sets that have the same columns.

You can run the graph with variable substitution, as long as you know the details.

Step 1: In Designer, open the graph STOCKPLOT.SGD

Click File->Open and select “StockPlot.sgd” from the default location.

The Stock Chart graph is opened in the workspace as shown in Figure 33.

Note, this graph is a two cell graph. o The upper cell has one band plot and three series plot showing various indicators by month. o The lower cell has a needle plot of stock trading volume by month.

Note the title has a special string “dyn(name)”. This whole string will be replaced by the dynamic

variable “name” when used the SGDESIGN procedure.

Figure 31

Page 22: Using Graph Designer for High-Quality Graphs Sanjay ... · graph that has a histogram of the scores, along with a couple of distribution curves. We also want to see the various severity

22

Figure 33

Step 2: In SAS, open the program STOCKPLOT.SAS.

Open the program file StockPlot.sas. The code is shown in Figure 34.

The program opens the html destination.

Run PROC SGDESIGN, with sgd= stockplot.sgd and

data=stockA

Note, dynamic name=‟A‟. This will be included in the

title.

Run PROC PRINT to display the most recent data.

The above steps are repeated for stock B and C, with

the same graph name but different data.

The results of the program are output to the html file,

that contains three graphs.

Additional topics

Managing the Graph Gallery.

Using the Style Editor.

Creating Shared Variable Graphs.

Creating Scatter Plot Matrices.

ods listing close;

ods html; %let name='A'; title;

ods graphics / imagename='StockA'; proc sgdesign sgd='C:\StockPlot.sgd' data=sasuser.stock_A; dynamic name=&name;

run; title 'Stock Data for company ' &name; proc print data=sasuser.stock_A

(keep=stock date close volume); where date > '01JAN05'd; run;

/*--Repeat for Stock C --*/ /*--Repeat for Stock C --*/

ods html close; ods listing;

Figure 34

Page 23: Using Graph Designer for High-Quality Graphs Sanjay ... · graph that has a histogram of the scores, along with a couple of distribution curves. We also want to see the various severity

23

CONCLUSION

Many SAS users prefer to use an interactive application to create graphs. These users have to export their data to

applications like Excel to create the graph. The new Graph Designer now provides you the option to create your graphs using an interactive process right within SAS. No programming is required. Designer can also be a great prototyping tool for the graph programmer.

The Designer application is based on the same underpinnings as the ODS Graphics system. That means you can

create an analytical graph suitable for your needs. This graph will look consistent with the automatic graphs you will get from the SAS analytical procedures or from SG procedures. Furthermore, you can create the graph once, and then run it multiple times with different data using the SGRENDER procedure. Such graphs can be integrated with other output sent to the open ODS destination in a batch process. Designer also shows you the GTL code generated

as you work, so it can be a great learning tool for GTL.

The addition of the interactive Graph Designer to the SAS/GRAPH product means you now have all the tools you

need to create modern effective analytical graphs within SAS.

Heath, Dan. 2007. “New SAS/GRAPH Procedures for Creating Statistical Graphics in Data Analysis.” Proceedings of

the SAS Global Forum. Cary, NC: SAS Institute Inc. Available at http://support.sas.com/events/sasglobalforum/previous/index.html.

Cartier, Jeff. 2006. “A Programmer‟s Introduction to the Graphics Template Language.” Proceedings of the Thirty-first

Annual SAS Users Group International Conference. Cary, NC: SAS Institute Inc. Available at http://support.sas.com/events/sasglobalforum/previous/index.html.

CONTACT INFORMATION

Your comments and questions are valued and encouraged. Contact the author at:

Sanjay Matange

SAS Institute

SAS Campus Dr

Cary, NC 27512

(919) 531-6753

Fax (919) 677-4444

[email protected]

SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS

Institute Inc. in the USA and other countries. ® indicates USA registration.

Other brand and product names are trademarks of their respective companies.