1 graphics hrp223 – 2012 november 28, 2012 copyright © 1999-2012 leland stanford junior...

172
1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is protected by copyright law and international treaties. Unauthorized reproduction of this presentation, or any portion of it, may result in severe civil and criminal penalties and will be prosecuted to maximum extent possible under the law.

Upload: caren-rice

Post on 31-Dec-2015

219 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

1

Graphics

HRP223 – 2012November 28, 2012

Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved.Warning: This presentation is protected by copyright law and international treaties. Unauthorized reproduction of this presentation, or any portion of it, may result in severe civil and criminal penalties and will be prosecuted to maximum extent possible under the law.

Page 2: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

2

Robbins

• Creating More Effective Graphics by Naomi Robbins is a wonderful book showing the right and wrong ways to visualize scientific data. Read it when you have an afternoon off. It is an ideal read on a transcontinental flight.

Page 3: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

3

How I do graphics

• Exploratory stuff– Use the quick and dirty graphics built into EG

• Production quality graphics– Write SAS or R code to make better looking

graphics– Edit in Adobe Illustrator

Page 4: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

4

Visualization Tools

• This is a excellent book that covers how to visualize stuff using many tools (including R). It has a great introduction to Adobe Illustrator.

Page 5: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

5

Why Do Data Visualization?

• Well designed pictures will show you the details and the whole pattern in your data.

• Numeric descriptions can easily hide important information.

• Some patterns are hard to detect in tables.– Whenever data is reported over time or locations,

you need art.

YOU CAN LEARN A LOT BY JUST LOOKING.-Yogi Berra

Page 6: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

6

Fisher’s Plot Data Reported in Cleveland

Based on code written by Robert Allison at SAS Institute

Year 1 Year 2

Page 7: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

7

0

5

10

15

0 5 10 15 200

5

10

15

0 5 10 15 20

0

5

10

15

0 5 10 15 200

5

10

15

0 5 10 15 20

Scatter Plot for Correlations

All have r2 = .67Anscombe 1973, Graphs in Statistical Analysis

Page 8: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

8

Bad Things

• First, I want to talk about bad graphics that I frequently see.– 3d– Extra Ink– Pie– Donuts– Stacked graphics

Page 9: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

9

General

• 3D graphics

– Don’t, Don’t, Don’tWhile the SAS implementation of 3D graphics is relatively good, don’t use 3D effects, unless you are measuring something in 3D. Even then, don’t.

Page 10: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

10

Tufte is a God to many.

• The empiricist in me is very nervous about the amount of pontificating in his books…– I want to have evidence-based advice.

• His best advice is to put no extra ink on the page.– Think about the ink-to-information ratio.– Remove all chart junk.

Note: the irony of the chart junk on this slide….

Page 11: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

11

Bar Chart ~ Default

Page 12: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

12

How did I know that?

• Search onLineDoc for sgplot… and/or get this:

Page 13: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

13

Custom AxisI prefer to have

ticks beyond the largest value

Page 14: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

14

Page 15: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

15

Ticks on Both Y AxesTicks on both axes

may help guestimate the counts

Page 16: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

16

Data in Jail

First draw bars then draw

reference lines

Page 17: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

17

OR

Things in < > are optional arguments

A variable name A number Optionally add other numbers

Optionally add a / and options for the reference

line(s)

Page 18: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

18

Page 19: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

19

Line Attributes

• I wanted to adjust the line attributes so I will use / lineattrs=()

OR Give a set of line options in ()

A style element with options

I wish I knew how that works…

Page 20: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

20

• Details in parentheses:

Page 21: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

21

Reference LinesFirst draw

reference linesthen draw bars

Page 22: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

22

Reducing Ink

• In Tufte’s world less ink is good. I could remove the bar color but that is removing necessary ink for this style of plot. I can remove the black edges to the bars.

Page 23: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

23

Page 24: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

24

Page 25: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

25

Put White Lines Over the Bars

Page 26: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

26

Ut oh…

Page 27: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

27

Page 28: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

28

Awful Design…. I specify a title and it does not show up

in the plot.

Page 29: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

29

Ink-to-Information Ratio

• How much ink for seven numbers?

Based on Soukup & Davidson, 2002 Visual Data Mining

Page 30: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

30

Cleveland

• If you want to know how to do scientific visualization, you must read William Cleveland’s work.– He attempted to quantify what makes a good graphic good.

• His early work on graphics is one of the reasons why R/S-plus is taking over the statistical world.

Page 31: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

31

Cleveland made Dot Plots Replace Bar Charts

Page 32: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

32

Adding an Offset to the y-axis

Page 33: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

33

Adding Value Labels

Page 34: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

34

Show Frequency Counts

Page 35: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

35

Label Attributes

Page 36: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

36

Page 37: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

37

Pie is bad.

• Work by Cleveland (and experimental psychologists) suggests that:– people are bad at judging the relative magnitude of angles – if you twist the rotation of the pie you can cause people to

systematically misjudge the size of the angles– a 3rd dimension makes judgment worse

• If you get a glossy handout with a 3D pie, assume someone is lying to you.

• Don’t use them.

Page 38: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

38

Don’t Explode!

• This exploded 3D pie (brought to you by Excel) is nearly useless for judging amounts.

Total

tweakedtwistedwrecked

Page 39: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

39

Forbidden Donut….

• Donut plots have the same problems as pies (if not worse) ….

Page 40: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

40

Stacking is Bad

• Cleveland also quantified the fact that people are bad at judging the relative height of stacked data.

Page 41: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

41

Wow, a cinnamon roll plot!

• Good luck making rapid judgments using this stacked 3D pie.

Page 42: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

42

What is a good graphic?

• Don’t make your audience think unnecessarily!– The point of the graphic should stand out instantly.– Plot the quantity (inference) that you want people to

notice.• Show the central tendency and the variability.• Minimize the amount of ink on the page.• Be sure colorblind people can understand it.

– Use a black and white photocopier and make sure you can distinguish all groups.

Page 43: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

43

What is wrong with this?

Female Male125

130

135

140

145

150

155

160

165

170

Weight in Framingham Dataset

What is the point that the reader should learn from this?

How is the variability represented?What are the error bars?Can you interpret a 1 SD error bars?

How many people are included?Ink to information … How many numbers are depicted?

Never contrast black on blue.

Page 44: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

44

Another Way

Page 45: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

45

How did I do that?

Page 46: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

46

The CodeDon’t put a legend on the

plot, add a pop-up description tooltip for pages

on the web and save the graphics template.

Page 47: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

47

The Graphic Template

• Premade complex plot designs are stored in a graphic template. You can also save the graphic template for any plot you make.

• Add tmplout= to the gplot statement.

Page 48: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

48

The templateI gave it a name

Use the new template on the

dataset.

Page 49: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

49

Running the template fixed the title bug!

Page 50: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

50

Another version of the same data

Page 51: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

51

Moderately Awful Code

• Bubble sizes are set by providing a radius… I force the area by setting the smallest and largest bubble size in centimeters.

I want to use this part of the plot for the legend so I

name it.

Page 52: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

52

How do I set the blue and pink?

• For grouped data you can specify the details for each plotted element in a style template:

Page 53: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

53

What is wrong with this?

What is the point of this graphic?

How are the two sexes represented?

What data is this?

Page 54: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

54

Page 55: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

55

Easy But Awful Boxplots

Page 56: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

56

What is wrong with this?

Lovely white space

What is the point?

How are the sexes represented?

How many people?

Where is the mean?

What data is this?

Page 57: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

57

What is the point of this graphic?

How are the two sexes represented?

A Good Boxplot

Page 58: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

58

Code for a Good Boxplot

Page 59: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

59

SAS's Framingham Heart Data

What is the point of this graphic?

Page 60: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

60

Page 61: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

61

Built in Graphics

• Many Enterprise Guide analyses have built in diagnostic graphics.

Page 62: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

62

When you test for the difference in the mean SAS gives you a great plot.

Page 63: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

63

Avoid Thinking

• Put labels on the graphic directly instead of using a key.

• If you want people to compare the difference between two lines, plot the difference, not the two lines.

Page 64: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

64

Bivariate Comparisons with Lines

• People are extremely bad at judging the distance between two curves. Never ask people to judge up and down (vertical) distances between curves.

Based on: Robbins Creating More Effective Graphs, 2005

The distance between the two curves is the same at all points.

Page 65: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

65

Plot Types

• Categorical variables– Descriptive

• Bar charts• Dot plots

– Inferential• Continuous variables

– Histogram– Box plot– Violin plots– Quantile and QQ plots

Page 66: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

66

Frequency Plots

• EG for frequency plots• Custom code

Page 67: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

67

I Typically Use HTML

This says the images should show tooltips with extra statistical details when you hover the mouse over parts of the graphic. (I can’t image these.)

This is the appearance template. For optimal results use:Analysis: colorDefault : overdistinguishes symbols for color or B&WJournal or journal2, etc: black and whiteStatistical or statistical2, etc: color

Include image_dpi = 300 to set the resolution to be higher than the default 100 dots per inch. Try 300 for final images pasting into MS Office.

Page 68: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

68

ods graphics on;

• This turns on the ODS statistical graphics.• Behind the scenes this combines your data

with a pre-specified description of what to plot and the aesthetics of the appearance.

Your data

Graphtemplate

Styletemplate

What Where?

ColorsFonts

Page 69: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

69

Useful ods graphics options

• ods graphics on /

• ods graphics / reset;

• ods graphics off;

Width = 8inHeight = 11inImagefmt = jpgimagename = thingyimagefmt = staticmap ;

Make a series of graphics

called thingy1,

thingy2, etc.

If you set only width or height, it will use a 4:3 aspect ratio.

Reset the graphic counter back to 1

Use pop-up tooltips with

details.

If you want to disable ods graphics

for a procedure

Page 70: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

70

Page 71: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

71

ODS Graphics Editor with EG

• If you want to do extensive tweaking to a graphic, you can use the WYSIWYG ODS Graphics editor. Unfortunately it only works with ODS graphics procedures and you need to rerun the code in SAS to invoke it.

Page 72: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

72

Move code from EG to SAS

1. Use the query builder to put your data in a permanent SAS library (not the work library).

2. Right click on the graphic node which is run on data in a permanent library and choose Open… Open Last Submitted Code.

3. Copy the code beginning with the SQL that makes the data.

4. Start SAS and paste the code into the program editor.

Page 73: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

73

Move all your code to SAS• Because the ODS graphics editor is not in EG

(yet), you can export the entire set of code for the project and then rerun it in SAS.

Page 74: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

74

ODS Graphics Editor with EG(2)

• After exporting all your EG project, open the code in SAS and add these lines at the top of the program:

ods rtf file = "c:\blah\somefile.rtf";ods listing sge = on;

• Then open the graphic of interest.

Page 75: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

75

Page 76: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

76

WYSIWYG Editing

• Right click and/or double click to set properties for objects in the plot.

The tool is optimized for some of the ODS style templates but you can use custom colors.

Page 77: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

77

• Right click on things to set properties. – Colors, text details, fonts– Point and click annotation– Symbols, arrows, text, circles

Page 78: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

78

WYSIWYG Editing

• While the Statistical graphics editor is a much needed improvement, it is incomplete. You can only use a few, style templates (for setting default colors and such) and you can not use custom style templates. This means that you can not do critical tasks like manually set the color for different values in scatter plots.

Page 79: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

79

Bar Charts

• The ink-to-information ratio is lousy.• A one dimensional quantity is being

“expanded” into two dimensions. – Doubling of the amount corresponds to how much

of an increase in area?

Page 80: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

80

SAS Bar Charts

• SAS makes the reader do extra work by rotating the axis labels in ActiveX images.

• They pointlessly include variable labels by default.

Page 81: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

81

How to do it?

Notice you can Edit the data and apply filters.

You can right click on variables and apply user-defined formats off the

Properties dialog.

Page 82: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

82

First create the format.

In the Data windowpane of the Bar Chart GUI, right click on the variable and change the format to the User Defined format you had created.

Page 83: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

83

The GUI is Solid

• My only complaints are that the rotate grouping values text does not work (position in this example) and the summary statistics do not show up when you request ActiveX images.

Page 84: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

84

.PNG format

ActiveX image format

Page 85: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

85

Saving the Graphic for Publication

• The easiest way to get publication quality graphics is to set the output type to be RTF.

Page 86: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

86

Default Output and Graphics

• The default graphic format in EG is ActiveX. These images can be edited (even on the web) but they only display with Internet Explorer. I have set my graphics to display as ActiveX images. Tweak this with Tools> Options… > Graph.

Page 87: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

87

Page 88: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

88

Types of Images

• The default formats of the images are determined by the ODS destinations you are using:– LISTING: pgn visible in the Windows Image Fax Viewer– HTML: png, gif, jpg contained in web pages and visible in

Internet Explorer, Firefox or Opera– LATEX: PostScrpt, epsi, gif, jpeg, pgn are visible in GhostView– PCL or PS: contained in Postscript file are visible in GhostView– PDF: contained in pdf, which is visible with Adobe Reader– RTF: visible in MS Word

• RTF graphics are done at 300 dpi by default

Page 89: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

89

You can browse the ODS appearance templates from the Style Manager on the Tools menu.

Page 90: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

90

ODS SGraphics

• Compared to the competition, for the last 10 years SAS graphics have been between poor and pathetic.– Graphics procedures rendered with okay quality,

at best .– No “what you see is what you get” editing.– Many plots were nearly impossible to render.– Custom graphics required extensive programming.

• SAS 9.x has attempted to solve this problem.

Page 91: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

91

Old vs. New Procedures

• The old (commonly used) graphics procedures were gchart, gplot.

• Now most analysis procedures have built in high quality graphics that can be invoked with an ODS graphics on statement.– Early on in the class I told you to tweak the EG

options to include “ODS graphics on” with every run.

• There are also new “easy to use” statistical graphics (sg) procedures.

Page 92: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

92

New Graphics Statistical Graphics Procs

• proc sgPlot– general plotting procedure that replaces gplot

• proc sgScatter– lots of tools for scatterplots and scatter matrices

• proc sgPanel– quick and easy trellis/lattice/matrix/panel of plots

• Proc sgRender– used with proc template to make totally custom plots– It replaces proc greplay

Page 93: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

93

Plot Types

• Categorical variables– Bar charts– Dot plots

• Continuous variables– Histogram– Box plot– Violin plots– Quantile and QQ plots

Page 94: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

94

You can get an okay looking graphic using sgpanel.

Categorical variables

Page 95: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

95

I was able to get exactly the graphic I wanted using R.

Categorical variables

Page 96: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

96

If you want to use R

• Download R for Mac or PC cran.cnr.berkeley.edu/bin/macosx/ cran.cnr.berkeley.edu/bin/windows/base

Page 97: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

97

How to learn R

• I usually teach R classes in the summer.– www.stanford.edu/~balise/ has links to my slide

decks for R classes.• In 2013 I may teach 223 one last time … in R.

Page 98: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

98

Plots for inference

• Categorical plots– Confidence limits on odds ratios– Four-fold plots– Expectancy plots– Mosaic plots

Page 99: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

99

Grouped Categorical Variables

• To graph categorical data in SAS you need to get Michael Friendly’s Visualizing Categorical Data. Unfortunately, his macros are copyrighted with the book… So I will show you the R versions.– Fourfold plots– Mosaic plots– Association plots

Grouped categorical variables

Page 100: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

100

Fourfold Plots

• They draw 4 slices of pie with the area corresponding to the number of people in each cell of a 2x2 table and they have confidence bands such that if the confidence bounds overlap on adjacent pie pieces, they are not statistically significantly different.

Grouped categorical variables

45% male vs. 30% female admission

Page 101: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

101

More males were admitted than females.

There is clear evidence of sexist policies in admissions!

Row: Male

Co

l: A

dm

itte

d

Row: Female

Co

l: R

eje

cte

d

1198

557

1493

1278

Grouped categorical variables

Page 102: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

102

Department A admitted more females than males and every other department had no bias!

The joy of Simpsons paradox.

Sex: Male

Ad

mit

?:

Ye

s

Sex: Female

Ad

mit

?:

No

Department: A

512

89

313

19

Sex: Male

Ad

mit

?:

Ye

s

Sex: Female

Ad

mit

?:

No

Department: B

353

17

207

8

Sex: Male

Ad

mit

?:

Ye

s

Sex: Female

Ad

mit

?:

No

Department: C

120

202

205

391

Sex: Male

Ad

mit

?:

Ye

s

Sex: Female

Ad

mit

?:

No

Department: D

138

131

279

244

Sex: Male

Ad

mit

?:

Ye

s

Sex: Female

Ad

mit

?:

No

Department: E

53

94

138

299

Sex: Male

Ad

mit

?:

Ye

s

Sex: Female

Ad

mit

?:

No

Department: F

22

24

351

317

Grouped categorical variables

Page 103: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

103

Mosaic Plots

• So you have an contingency table and you want to know if there is as an association. You do a chi-square test and it says there are associations between the rows and columns. What next?

Grouped categorical variables

Page 104: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

104

Some basic voodoo in R shows which combinations are over (in blue) or under represented (in red).

Grouped categorical variables

Page 105: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

105

I prefer the simpler association plots.

Green Hazel Blue Brown

Blo

nd

Re

dB

row

nB

lack

Relation between hair and eye color

Grouped categorical variables

Page 106: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

106

Continuous Outcomes

• The Distribution Analysis menu option can do basic plots.

Continuous variables

Page 107: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

107

The resolution of the histogram is okay but the others are unacceptable.

Page 108: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

108

Use sgplot for high resolution plots.

Continuous variables

Page 109: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

109

As you add more requests to the plot, it resizes and shifts things to make room. It draws them in the order you request them. It reads the requests from the first listed to the bottom. Change the order if you want to have an item appear layered on top of, or behind, another thing.

Some colors are not set yet in the enhanced editor. Use the menu Tools>Options>Enhanced Editor… then click User Defined Keywords to add the coloring.

Page 110: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

110

I want the title!

Page 111: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

111

How is that made?proc format library = work;

value $smoked"Non-smoker" = "None "missing = "Missing"other = "Not none"

;run;

data fram;set sashelp.heart;smokin = put(smoking_Status, $smoked.);

run;

Page 112: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

112

How is that made?

title "5209 Cholesterol Measures from Framingham Heart Study";proc sgplot data = fram tmplout="c:\blah\plate.sas";

histogram cholesterol;density cholesterol / type = kernel;density cholesterol / type = normal;keylegend /

location=inside position=topright across=1;run;

Make a new graphics template

Page 113: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

113

proc template;define statgraph sgplotFram;begingraph /;EntryTitle "5209 Cholesterol Measures from Framingham Heart Study" /;layout overlay; Histogram 'Cholesterol'n / primary=true binaxis=false LegendLabel="Cholesterol"; DensityPlot 'Cholesterol'n / Lineattrs=GraphFit kernel() LegendLabel="Kernel" NAME="DENSITY"; DensityPlot 'Cholesterol'n / Lineattrs=GraphFit2 normal() LegendLabel="Normal" NAME="DENSITY1"; DiscreteLegend "DENSITY" "DENSITY1" / Location=Inside across=1 halign=right valign=top;endlayout;endgraph;end;run;

proc sgrender data = work.fram template = template=sgplotFram;run;

This was saved in plate.sas.

Render a graphic with the template and dataset specified.

Note I changed the name of this template.

Page 114: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

114

How to set the color for a histogram

Page 115: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

115

proc sgplot data = fram;histogram weight / fillattrs = (color = coral);

run;

Page 116: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

116

You can also tweak the style template

Page 117: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

117

Tweaking the Style Template

proc template;define style myStyle;

parent = styles.Statistical;style GraphDataDefault / color=coral;

end;run;

ods html style = myStyle;proc sgplot data = fram;

histogram weight ;run;ods html close;

Page 118: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

118

vbar Version

proc sgplot data = fram;vbar weight / group = sex;

run;

Page 119: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

119

proc sgplot data = fram;vbar weight / group = sex;xaxis fitpolicy = thin ;

run;

Page 120: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

120

proc template;define style myStyle;parent = styles.Statistical;

style graphdata1 from graphdata1 / contrastColor=pink color = pink;

style graphdata2 from graphdata1 / contrastColor=blue color = blue;

end;run;

ods html style = myStyle;proc sgplot data = fram;

vbar weight / group = sex;xaxis fitpolicy = thin ;

run;ods html close;

Page 121: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

121

Customizing graphics

• You can tweak the graphics that ship with SAS by modifying their graph template or you can create truly custom graphics by making your own statistical graph template.

Your data

Graphtemplate

Styletemplate

Page 122: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

122

If you do not want to explain what Kernel density estimation is…

remove the lines.

Page 123: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

123

Finding the template

• Add before the procedure that draws the graphic add ods trace on; and include ods trace off; afterwards. This prints the names of all the templates used by the procedure in the log. product.procedure.Graphis.TemplateName

Page 124: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

124

Looking at a Template

• You can ask proc template to display the template with the source statement:

proc template;source stat.ttest.graphics.summary2;

run;

• Remember to type this before you start editing:

ods path(prepend) work.template (update);

Page 125: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

125

Don’t Panic

This is a complete template except for the

proc template statement here and a run statement at the

bottom.

Copy this into an editor window and add proc

template.

Page 126: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

126

After adding proc template and commenting out the Kernel statements rerun the code.

Page 127: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

127

Oops. Unknown key words…

• You can fix the color coding on the template code easily.

Page 128: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

128

Fixed (permanently)

All your subsequent plots will have no density line.

Page 129: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

129

Continuous variables

Page 130: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

130

Violin

• A violin plot mirrors the shape of the histogram (density). They can be done in R.

05

01

00

15

0

Continuous variables

Page 131: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

131

Grouped Continuous Variables

• You can use the Distribution Analysis to get basic grouped plots.

• For better looking plots you need to write sgplot and/or sgpanel code.

Grouped continuous variables

Page 132: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

132

Request distinct graphics by subgroups.

Grouped continuous variables

Page 133: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

133

Grouped continuous variables

Page 134: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

134

Actually this took a bit of voodoo.

Grouped continuous variables

Page 135: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

135

1st

2nd

Grouped continuous variables

Page 136: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

136

Double click here.

Put details on the histogram tweaks here.

I use/tweak nrow ncol and endpoints often.endpoints = 2 to 10 by 0.5midpoints = 5.6 5.8 6.0 6.2 6.4

Grouped continuous variables

Page 137: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

137

Grouped continuous variables

Page 138: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

138

Grouped continuous variables

Page 139: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

139

I want to add in a reference line showing what is normal and put the categories in order.

Page 140: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

140

Page 141: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

141

Side by Side Violin Plots

A B C

50

10

01

50

Grouped continuous variables

Page 142: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

142

Paired Continuous Variables

• People typically show paired data with scatterplots.

• EG generate them:

Grouped continuous variables

Page 143: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

143

Scatter PlotGrouped continuous variables

Page 144: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

144

Jittered Plot

Page 145: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

145

Jitter vs. Sunflowers

In R you can also do sunflower plots.

Grouped continuous variables

Page 146: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

146

Ordinary Least Squares Regression

• People typically plot a regression line to show a relationship between two continuous variables.

Grouped continuous variables

Page 147: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

147

Regression line

• You can easily add a regression line to the scatter plot.

Page 148: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

148

proc sgplot data = fram;scatter x = height y = weight;

run;

proc sgplot data = fram;reg x = height y =

weight;run;

Page 149: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

149

ods listing sge = on style = statistical;

proc sgplot data = fram;reg x = height y = weight /

markerattrs = (color = green) lineattrs = graphdata1 (color = lime);

run;

Page 150: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

150

ods listing style = statistical;

proc sgplot data = fram;reg x = height y = weight / group = sex ;

run;

Page 151: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

151

proc template;define style sexE;parent = styles.Statistical;style graphdata1 /

contrastColor=pink markersymbol = "star";

style graphdata2 / contrastColor=blue markersymbol = "plus";

end;run;

ods html body="C:\blah\stuff.html" gpath="c:\blah" style = sexE;

proc sgplot data = fram;scatter x = height y = weight / group = sex ;reg x = height y = weight / group = sex ;

run;ods html close;

Page 152: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

152

Page 153: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

153

Bisquare• Figure out what is an odd value and then put a weight on it to

devalue it. There are many robust regression algorithms around. R and S-Plus software have them well implemented.

0 1 2 3 4 5 6 7 8

V1

0

5

10

15

20

V3

0 1 2 3 4 5 6 7 8

V1

0

5

10

15

20

V3

Grouped continuous variables

Page 154: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

154

Loess and Splines

• Loess is a technique essentially creates a rolling window and gets a weighted average across the values visible inside the window.

• Splines are curved lines that allow different amounts of stiffness to the curves.

Grouped continuous variables

Page 155: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

155

Smooth = 25Smooth = 50

Smooth = 99

Page 156: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

156

Proc phreg has a lot of new features but nothing major in the graphics. With phreg, if you specify ods graphics on you do not automatically get any plots. Here I request survival and cumulative hazard plots including the global confidence limits option (cl).

Once again the option names are not consistent with the table names.

Page 157: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

157

Proc lifetest can show the number at risk but the implementation is weak. It labels the groups with numbers even if the strata are character strings. You have to manually edit them and this affords ample opportunity for mistakes.I don’t see a way to change the censoring symbol in the legend.

This shows the number of people at risk after 20, 40 etc days.

Page 158: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

158

Too Many Graphics

• If the ods graphics on statement gives you too many graphics, you can specify which graphics you want by including code designed for the procedure. Typically it looks like this: plot(only) = (table names). This design is poorly implemented because you need to know where to put the plot statement and what the table names are. Does it go on the proc line (like phreg), the tables line (like proc freq), or some other line? Also the table names specified with a plot statement do not always match the ODS table names.

Page 159: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

159

• Usually you can use an ODS exclude statement or an ODS select statement to pick the correct things to print. Using the plots(only) = syntax is more efficient.

Page 160: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

160

Splitting a Grid

• Some procedures produce a grid of plots. You can get access to the individual plots by specifying plots(unpack). Then you can use plots(only)=tableName to get just the right parts.

• ODS select or exclude statements will not work.

Page 161: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

161

plots(GlobalOptionsGoHere). The global options apply to all graphics in this procedure.

Page 162: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

162

Beyond the Basic Univariate plots

• There are 4 SG procedures that allow you to build up complex univariate plots and do multivariate (trellis/lattice) plots.

Page 163: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

163

Statistical Graphics Procs

• proc sgPlot– general plotting procedure that replaces gplot

• Proc sgRender– used with proc template to make totally custom plots– It replaces proc greplay

• proc sgScatter– lots of tools for scatterplots and scatter matrices

• proc sgPanel– quick and easy trellis/lattice/matrix/panel of plots

Page 164: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

164

Grids

• You can produce lattices full of graphics with proc gpanel.

Page 165: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

165

Page 166: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

166

Spaghetti Plots

Data from Singer and Willett: www.ats.ucla.edu/stat/examples/alda.htm

Page 167: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

167

SGPlot vs Template

• You can replicate everything done with proc sgplot using the template language but don’t reinvent the wheel if you don’t need to.

• You will want to use proc template to build custom graphics that use many panels.

• Proc sgplot uses statements that start like reg but template uses names like regressionplot. – Similar but not identical names… boo.

Page 168: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

168

Page 169: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

169

Page 170: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

170

layout gridded = ticks do not have to alignlayout lattice = ticks must align

Page 171: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

171

Page 172: 1 Graphics HRP223 – 2012 November 28, 2012 Copyright © 1999-2012 Leland Stanford Junior University. All rights reserved. Warning: This presentation is

172