1_Introduction_to_ODS_Graphics

advertisement
Introduction to SAS ODS Graphics
September 16, 2015
Rocio Lopez
Overview
• Introduction
• Basic Set-Up
• SAS Automatic Graphs
• SG Procedures
– SGPlot
– SGPanel
– SGScatter
• Take away message
• Future Talks
INTRODUCTION
Introduction
• Until version 9 all graphics were done with
SAS/GRAPH procedures (gplot, gchart, etc.)
http://support.sas.com/resources/papers/proceedings15/2441-2015.pdf
Introduction
• ODS graphics
– Developed to make it easier to generate common graphics.
– Features:
– Graphics capabilities added to statistical procedures
– SG procedures
– Graphics Template Language (GTL)
– ODS Graphics Designer & Editor
• 9.3 and 9.4 (including maintenance releases) have
made lots of improvements and additions
Some Examples
SET-UP
Set-up
• Request graphs
ods graphics on;
proc statements;
ods graphics off;
In SAS 9.4 ODS graphics are generated by default.
• Where to view graph
– Results window
– Depending on ods destination
– stand alone figure
– embedded figure in .rtf or .pdf file
Modifying destination and resolution
Modify graph options
ods graphics </options>;
Modify graph options
Option
border|noborder
border=on|off
height=480px
imagename="filename"
reset
scale|noscale
scale=on|off
width=dimension
outputfmt=file-type
See User Guide for full list
Description
Control border around graph
Height of graph. Accepted units are
cm, in, mm, pct, pt, px.
Specify file name
Reset all options to default values.
To reset particular options use
reset=option
Specify whether the content of the
graph is scaled proprotionally
Width of graph
Specify output format. This varies
by ODS destination
Supported file types
Set-up – Recap
ods listing gpath=“&GraphDir.” image_dpi=300;
ods graphics / reset imagename=“Figure1”
scale=on width=3in height=3in;
proc statements;
ods graphics off;
Only need to
resubmit these
statements if you
wish to change
any of the options
for next graph
AUTOMATIC GRAPHS
Automatic Graphs From Procedures
• Starting with SAS 9.2 many analytical procedures
automatically generate certain graphs
ods rtf file=“ProcRegExp.rtf” style=journal;
ods graphics on;
proc reg data=sashelp.class;
model Weight = Height;
run;
ods graphics off;
ods rtf close;
SGPLOT PROCEDURE
SGPLOT Procedure
• Single-cell graphs
• Supports over 20 different plot statements
• You can combine most plot statements in the same
graph
– Overlaid in order specified
• Supporting statements allow more customizations
– Reference lines
– Insets
– Axes
– Axes tables
– Legends
SGPLOT Procedure - Syntax
proc sgplot data=<data-set> <options>;
plot-statement(s) required-parameters </options>;
<refline-statement(s) >;
<inset-statement(s)>;
<axis-statement(s)>;
<keylegend statement(s)>;
<(xy)axistable statement(s)>;
run;
SGPLOT - Histogram
proc sgplot data=sashelp.class;
histogram age;
run;
SGPLOT – Histogram & Density
proc sgplot data=sashelp.class;
histogram age;
density age;
run;
SGPLOT – Histogram (move legend)
proc sgplot data=sashelp.class;
histogram age;
density age;
keylegend / location=inside
position=topright noborder;
run;
SGPlot – Box Plot
proc sgplot data=sashelp.class;
hbox height/category=sex;
run;
proc sgplot data=class;
vbox height/category=sex
group=Over13;
run;
SGPlot – Bar chart
proc sgplot data=sashelp.cars;
vbar type/stat=percent;
run;
proc sgplot data=sashelp.cars;
hbar type/stat=percent;
run;
SGPlot – Bar chart (add data label and change axis)
proc sgplot data=sashelp.cars;
vbar type/stat=percent
datalabel;
yaxis values=(0 to 0.6 by 0.1)
max=0.65 valueshint;
run;
Specifies to ignore
the values listed
when determining
min and max range
SGPLOT – Stacked Bar chart
proc sgplot data=sashelp.cars;
vbar type/stat=percent
group=origin;
run;
SG Procedure – Grouped Bar chart
proc sgplot data=sashelp.cars;
vbar type/stat=percent
group=origin datalabel
groupdisplay=cluster;
run;
Note: The %’s calculated are out
of the total. If you want to show
distribution of origin within type,
for example, you would need to
calculate first.
SGPlot – Grouped Bar chart
proc freq data=sashelp.cars;
tables type*origin;
ods output CrossTabFreqs=Pct;
run;
proc sgplot data=Pct;
vbar type/ group=origin
groupdisplay=cluster
response=RowPercent;
yaxis label=“Percent”;
run;
SGPlot – Axis Table
proc sgplot data=Pct;
vbar type/ group=origin
groupdisplay=cluster
response=RowPercent;
xaxistable frequency/
title='Frequency';
keylegend /location=inside
position=topright noborder;
yaxis label=“Percent”;
run;
Some Notes on Axis Tables
• Can be used with any kind of plot.
• For categorized charts the category and group
variables from the chart are used automatically. For
other plots you would need to specify.
– xaxistable variable /x=xvariable class=groupvariable;
• You can specify more than one variable or include
multiple xaxistable commands
• yaxistable also available
SGPLOT – Scatter Plots
proc sgplot data=sashelp.class;
scatter x=height y=weight/
group=sex;
keylegend /location=inside
position=topleft down=2;
run;
SGPLOT – Easily Add Reference Lines
proc sgplot data=sashelp.class;
refline 100/axis=y;
scatter x=height y=weight/
group=sex;
keylegend /location=inside
position=topleft down=2;
run;
SGPLOT – Scatter Plots with Error Bars
proc sgplot data=class_means;
scatter x=sex y=wt_mean/
yerrorlower=wt_lclm
yerrorupper=wt_uclm;
yaxis label=“Mean Weight”;
xaxis offsetmin=0.25
offsetmax=0.25;
run;
SGPLOT – Series Plots
proc sgplot data=sashelp.stocks;
series x=date y=close/
group=stock;
run;
proc sgplot data=sashelp.stocks;
series x=date y=close/group=stock
curvelabel curvelabelpos=max;
run;
SGPlot
• Now let’s create this plot with some of what we’ve
learned so far.
SGPlot –Overlaying Plots
proc sgplot data=sashelp.classfit noautolegend;
band x=height upper=uppermean
lower=lowermean/name='cl'
legendlabel='95% Confidence Limits';
run;
SGPlot –Overlaying Plots
proc sgplot data=sashelp.classfit noautolegend;
band x=height upper=uppermean
lower=lowermean/name='cl'
legendlabel='95% Confidence Limits';
scatter x=height y=weight;
run;
SGPlot –Overlaying Plots
proc sgplot data=sashelp.classfit noautolegend;
band x=height upper=uppermean
lower=lowermean/name='cl'
legendlabel='95% Confidence Limits';
scatter x=height y=weight;
series x=height y=predict/ name='fit';
run;
SGPlot –Overlaying Plots
proc sgplot data=sashelp.classfit noautolegend;
band x=height upper=uppermean
lower=lowermean/name='cl'
legendlabel='95% Confidence Limits';
scatter x=height y=weight;
series x=height y=predict/name='fit';
series x=height y=lower/
lineattrs=(pattern=dash) name='pl' ;
series x=height y=upper/
lineattrs=(pattern=dash);
run;
SGPlot –Overlaying Plots
proc sgplot data=sashelp.classfit noautolegend;
band x=height upper=uppermean
lower=lowermean/name='cl'
legendlabel='95% Confidence Limits';
scatter x=height y=weight;
series x=height y=predict/name='fit';
series x=height y=lower/
lineattrs=(pattern=dash) name='pl' ;
series x=height y=upper/
lineattrs=(pattern=dash);
inset "R(*ESC*){sup '2'} = 0.77"
"Adjusted R(*ESC*){sup '2'} = 0.76";
run;
SGPlot –Overlaying Plots
proc sgplot data=sashelp.classfit noautolegend;
band x=height upper=uppermean
lower=lowermean/name='cl'
legendlabel='95% Confidence Limits';
scatter x=height y=weight;
series x=height y=predict/name='fit';
series x=height y=lower/
lineattrs=(pattern=dash) name='pl' ;
series x=height y=upper/
lineattrs=(pattern=dash);
inset "R(*ESC*){sup '2'} = 0.77"
"Adjusted R(*ESC*){sup '2'} = 0.76";
keylegend 'fit' 'cl' 'pl';
run;
SGPLOT – Or just use the reg plot
proc sgplot data=sashelp.class;
reg x=height y=weight/cli clm;
keylegend /across=3;
run;
http://support.sas.com/resources/papers/proceedings15/2441-2015.pdf
SGPANEL PROCEDURE
SGPANEL Procedure
• Classification panels
– Same plot repeated by classification variable
• SGPLOT syntax carries over with some minor changes
– xaxis is now colaxis
– yaxis is now rowaxis
SGPANEL Procedure
proc sgpanel data=cars_reduced;
panelby type/novarname;
scatter x=mpg_city y=horsepower;
run;
SGPanel - Layout
panelby type/ novarname columns=4;
Or
panelby type/ novarname layout=columnlattice;
panelby type/ novarname rows=4;
Or
panelby type novarname/ layout=rowlattice;
SGPANEL Procedure – 2 Class Variables
panelby type origin/ novarname;
panelby type origin/ novarname
layout=lattice;
SGSCATTER PROCEDURE
SGSCATTER Procedure
• Comparative scatter plots or scatter plot matrices
• Does not use layered architecture
• Supports 3 plot statements
SGScatter – Plot statement
proc sgscatter data=sashelp.cars;
plot (mpg_city mpg_highway)*
horsepower;
run;
SGScatter – Compare statement
proc sgscatter data=sashelp.cars;
compare y=(mpg_city mpg_highway)
x=horsepower;
run;
SGScatter – Matrix statement
proc sgscatter data=sashelp.cars;
matrix mpg_city mpg_highway horsepower
enginesize weight length;
run;
TAKE AWAY MESSAGE
Take away message
• SAS has made many advances in their graphics
procedures
• ODS graphics are easy to use and produce high
quality plots
• There are constant improvements and new features
being added
• For primarily SAS users there is no longer a need to
rely on R for generating plots
SAS Version
• All examples presented have been generated using
SAS 9.4.
• Some of the code might not run in 9.2-9.3.
Talk Materials
• All materials will soon be available on SharePoint
(waiting for the page to be set up)
• For now you can find them at my SAS community user
page http://www.sascommunity.org/wiki/User:Lopezr
Good Resources
• Statistical Graphics Procedures by Example: Effective
Graphs Using SAS – Sanjay Matange & Dan Heath
• Graphically Speaking Blog
(http://blogs.sas.com/content/graphicallyspeaking/)
• SAS Tips Sheets
– ODS Graphics
– SG Procedures
– SGPlot
– SGPanel
• SAS SG Procedures Documentation
FUTURE TALKS
Future talks
• Modifying style attributes
• Introduction to Graph Template Language (GTL)
• Automatic ODS Graphics from SAS Procedures and
How to Modify These
• Useful SAS Graphics Macros
• Step-by-step examples of some plots
QUESTIONS?
THANKS
Download