dm "log; clear; odsresults; clear;"; options linesize=64 nonumber nodate; goptions reset gunit=in; *All sizes are in inches; *Default gunit value is "cells"; *Can set some defaults with goptions like htext=0.25 htitle=0.3 for height of text and titles; *To reset all options and remove the use of previous global statements, use: goptions reset; * Order does matter here so set the gunit AFTER reseting all options!; *********************************************************************; * AUTHOR: Chris Bilder *; * DATE: 6-3-16 *; * PURPOSE: Graphics with the cereal data set *; *********************************************************************; title1 "Chris Bilder, STAT 850"; *Read in the data set from a comma delimited file; proc import out=cereal datafile="C:\data\cereal.csv" DBMS=CSV replace; getnames=yes; datarow=2; run; *Adjust for serving size; data set1; set cereal; sugar = sugar_g/size_g; fat = fat_g/size_g; sodium = sodium_mg/size_g; *remove the old variables below from the data set; drop size_g sugar_g fat_g sodium_mg; run; title2 "Cereal data adjusted for serving size"; proc print data=set1(obs = 11); run; ****************************************************************************; * gplot *; ****************************************************************************; title2 "Cereal data - same symbols for each observation"; proc gplot data=set1; plot sugar*fat / vaxis=axis1 haxis=axis2 grid; axis1 label = (a=90 "Sugar"); axis2 label = ("Fat"); symbol1 v=circle cv=red h=0.25; run; title2 "Cereal data - symbol represents shelf"; proc gplot data=set1; plot sugar*fat=shelf / vaxis=axis1 haxis=axis2 grid legend=legend1; axis1 label = (a=90 "Sugar"); axis2 label = ("Fat"); symbol1 v=circle cv=red h=0.25; symbol2 v=diamond cv=blue h=0.25; symbol3 v=square cv=green h=0.25; symbol4 v=triangle cv=purple h=0.25; legend1 label = ("Shelf number") down = 1 across = 4 position = (bottom center outside) shape = symbol(0.00001,0.25); run; ************************************************; * Another way to get a legend; * This is done with the cereal data for illustrative purposes only; * There is no need to reformulate the data like this for this particular data set; data shelf1 shelf2 shelf3 shelf4; set set1; if shelf = 1 then output shelf1; if shelf = 2 then output shelf2; if shelf = 3 then output shelf3; if shelf = 4 then output shelf4; run; data set2; merge shelf1(keep=sugar fat rename=(sugar=sugar1 fat=fat1)) shelf2(keep=sugar fat rename=(sugar=sugar2 fat=fat2)) shelf3(keep=sugar fat rename=(sugar=sugar3 fat=fat3)) shelf4(keep=sugar fat rename=(sugar=sugar4 fat=fat4)); run; title2 "Reformulated data set"; proc print data=set2(obs = 3); run; *See how I used four y*x plots and put them on the same plot with overlay * Also, see the use of the value option in legend1; title2 "Sugar vs. fat with reformulated data set"; proc gplot data=set2; plot sugar1*fat1 sugar2*fat2 sugar3*fat3 sugar4*fat4 / overlay vaxis=axis1 haxis=axis2 grid legend=legend1; axis1 label = (a=90 "Sugar"); axis2 label = ("Fat"); symbol1 v=circle cv=red h=0.25; symbol2 v=diamond cv=blue h=0.25; symbol3 v=square cv=green h=0.25; symbol4 v=triangle cv=purple h=0.25; legend1 label = ("Shelf number") down = 1 across = 4 position = (bottom center outside) value = ("1" "2" "3" "4") shape = symbol(0.00001,0.25); run; *******************************************************; * sg procedures *; *******************************************************; ***************; *Scatter plots; title2 "sg procedure, cereal data"; proc sgplot data=set1; scatter x=fat y=sugar / group=shelf; run; *attrpriority=none allows one to control styles better; ods graphics / attrpriority=none; title2 "sg procedure, cereal data better plot"; proc sgplot data=set1; StyleAttrs datasymbols=(circle diamond square triangle) datacontrastcolors=(red blue green purple); scatter x=fat y=sugar / group=shelf MarkerAttrs=(size=10); yaxis grid GridAttrs=(pattern=dot color=gray); xaxis grid GridAttrs=(pattern=dot color=gray); run; ods graphics / attrpriority=color; *Default value; *Could also change the plot symbols and colors using proc template; ***************; *Box plots; title2 "sg procedure - box plots"; proc sgplot data=set1; vbox sugar / group=shelf; xaxis label="Shelf"; yaxis label="Sugar"; run; *Same as above but without the legend which is preferred in this situation; title2 "sg procedure - box plots"; proc sgplot data=set1; vbox sugar / category=shelf; xaxis label="Shelf"; yaxis label="Sugar"; run; *Same as above but now using panels (i.e., Trellis graphics); title2 "sg procedure - box plots"; proc sgpanel data=set1; panelby shelf / columns=4; vbox sugar; run; ***************; *Dot plots; *"Dot" plots do not work well in SAS - a statistic needs to be plotted; title2 "sg procedure - dot plots"; proc sgplot data=set1; dot sugar / group=shelf; run; *Another dot plot attempt does not work; proc sgplot data=set1; dot shelf / response=sugar; run; *Dot plot created via a scatter plot works well; proc sgplot data=set1; scatter x=shelf y=sugar / jitter jitterwidth=0.2; yaxis label="Sugar"; xaxis values=(1 to 4 by 1); *vbox sugar / group=shelf; *Does not work to overlay the two plots; run; *Minor tick marks (lines shown between the labeled tick marks) - use ; * something like "minor minorcount=9" which would produce tick marks; * at 1.1, 1.2, ..., 1.9, 2.1, ... in the above plot; *Box and dot plot overlay is not as easy as it should be; * See http://www.phusewiki.org/docs/2012/PAPERS/CS/CS03.pdf for an example of how to do it;