As the first step in the decommissioning of sasCommunity.org the site has been converted to read-only mode.


Here are some tips for How to share your SAS knowledge with your professional network.


Sparklines

From sasCommunity
Jump to: navigation, search

Sparklines are small word-sized information graphics that can be embedded in text to present trends and variations associated with data in a simple and condensed way.

The concept was conceived by Dr. Edward Tufte, a data visualization guru.

Sparklines in SAS

Please see http://www.edwardtufte.com/tufte/ for more information and the details for how sparklines should be generated and used. These are some of my ideas and might not be 100% in line with Dr. Tufte's thinking. This technique is a work in progress for me and I frequently find better ways of doing this. --SamC 12:34, 7 September 2007 (EDT)


Gallery

JPEG rendered at 10000x833 resolution, shrunk down to
144x12pt: Croker airline.jpg
168x14ptCroker airline.jpg
192x16ptCroker airline.jpg
216x18ptCroker airline.jpg
Filesize is 233k, so a little big...

filename outgraph "c:/output/airline.jpg"; 
goptions  
     reset=all 
     device=jpeg
     xmax=144pt  horigin=0.000pt  hsize=144pt xpixels=10000
     ymax=12pt   vorigin=0.000pt  vsize=12pt  ypixels=833
     cback=white
     noborder
     gsfname=outgraph
     gsfmode=replace;
     symbol1 v=none i=j c=black width=50;
     axis1 label=none value=none major=none minor=none 
 offset=(0,0)
 style=0; 
     axis2 label=none value=none major=none minor=none
 style=0;
proc gplot data=sashelp.air;
     plot air*date /   haxis=axis1 vaxis=axis2;run;
quit;

PNG rendered at 10000x833 resolution, shrunk down to
144x12pt: Croker airline.png
168x14ptCroker airline.png
192x16ptCroker airline.png
216x18ptCroker airline.png
Filesize is 30k.

filename outgraph "c:/output/airline.png"; 
goptions  
     reset=all 
     device=png
     xmax=144pt  horigin=0.000pt  hsize=144pt xpixels=10000
     ymax=12pt   vorigin=0.000pt  vsize=12pt  ypixels=833
     cback=white
     noborder
     gsfname=outgraph
     gsfmode=replace;
     symbol1 v=none i=j c=black width=50;
     axis1 label=none value=none major=none minor=none 
 offset=(0,0)
 style=0; 
     axis2 label=none value=none major=none minor=none
 style=0;
proc gplot data=sashelp.air;
     plot air*date /   haxis=axis1 vaxis=axis2;run;
quit;

--SamC 13:45, 10 October 2007 (EDT)

High Resolution and Scalable Sparklines

For printed material PDF graphics typeset using LaTeX is has the highest quality that I have seen. PDF graphics generated using the SAS PDF or PDFC devices are vector and look good no matter how small they are made. There are no fuzzy edges to deal with.

Media:Sparkfcst.pdf


The key to this technique involves first generating a pdf graphics device that matches the dimensions that you want, and then producing the graph using SAS/GRAPH, specifically referring to this graphics device.

proc gdevice nofs catalog=gdevice0.devices;
     copy pdfC from=sashelp.devices newname=sp12pt;
     modify sp12pt
     description='PDF Sparkline 12pt*144pt'
          /* 1 in = 72 pt */
          /* the dimensions are a factor of 30 */
          xmax=2in       horigin=0.000  hsize=2in
          ymax=.083in    vorigin=0.000  vsize=.083in
          /* generate BIG so it looks good small. Especially important for fonts */
          xpixels=10000
          ypixels=333
          prows=0
          pcols=0        
          lrows=31
          lcols=60
          ;
quit;

Postscript fonts use 72 points per inch, so getting the right inches, or other unit of measurement involves doing a bit of math. The glue that can help you tie everything together in terms of these dimensions is the factor of height to width and I like 30.

Directly referencing this device involves using the GOPTIONS statement, something like:

filename outgraph "&goutdir\spark_air.pdf"; 
goptions            
       reset=all 
       device=sp12pt
       cback=white
       noborder
       gsfname=outgraph
       gsfmode=replace;

Take a look at this pdf file to see some examples of how these look in production. Media:Sparkexample_croker.pdf‎

You should be able to use the following code exactly, well after changing the output directory...

/*******************************************************************************
*       Program Name    :       $RCSfile: SparkProduction.sas,v $
*       REV/REV AUTH    :       $Revision: 1.1 $ $Author: scoyote $
*       REV DATE        :       $Date: 2007/09/07 15:23:08 $
********************************************************************************/
/* change this to somewhere on your file system */
%let goutdir=C:\Documents and Settings\scrok586\Desktop\Output;

/* set up a library for your modified device entry */
libname gdevice0 "&goutdir";
/* modify the color pdf device.  This is nice due to the 
vector graphics... it looks great small */
proc gdevice nofs catalog=gdevice0.devices;
     copy pdfC from=sashelp.devices newname=sp12pt;
     modify sp12pt
     description='PDF Sparkline 12pt*144pt'
          /* 1 in = 72 pt */
          /* the dimensions are a factor of 30 */
          xmax=2in       horigin=0.000  hsize=2in
          ymax=.083in    vorigin=0.000  vsize=.083in
          /* generate BIG so it looks good small. Especially important for fonts */
          xpixels=10000
          ypixels=333
          prows=0
          pcols=0        
          lrows=31
          lcols=60
          ;
quit;
/* prepare some data... */
proc sql;
     select mean(log(air)), min(date), max(date),max(log(air)), min(log(air)) 
          into :meanlogair,:mindate,:maxdate,:maxlogair,:minlogair from sashelp.air;
quit;
/* generate some high and low points for fun... */
data air;
     set sashelp.air;
     meanlogair=dif(log(air)-&meanlogair);
     if round(log(air),.0001)=round(&minlogair,.0001) then minlogair=meanlogair; 
     if round(log(air),.0001)=round(&maxlogair,.0001) then maxlogair=meanlogair; 
run;

   filename outgraph "&goutdir\spark_air.pdf"; 
          goptions            
               reset=all 
               device=sp12pt
               cback=white
               noborder
               gsfname=outgraph
               gsfmode=replace;
               symbol1 v=dot  i=none c=red height=40;
               symbol2 v=dot  i=none c=green   height=40; 
               symbol3 v=none i=join c=black width=1;
               axis1 
                    label=none 
                    value=none 
                    major=none 
                    minor=none 
                    offset=(0,0)
                    style=0; 
               axis2 
                    label=none 
                    value=none 
                    major=none 
                    minor=none
                    style=0;
 
proc gplot data=air;
     plot ( minlogair maxlogair meanlogair)*date 
          / overlay noframe haxis=axis1 vaxis=axis1;
run;quit;

--SamC 12:24, 7 September 2007 (EDT)

Related pages