Data visualization with SAS programming

Graphing a year-long calendar in ODS Graphics

Reply
Occasional Contributor
Posts: 13

Graphing a year-long calendar in ODS Graphics

I am trying to basically duplicate Rob Allison's 10 year smog calendar, http://robslink.com/SAS/democd1/calgrid.htm, in ODS Graphics, or one year of it.  I am running Base SAS 9.4.2 and have code to do both a heatmap in GTL and a scatter plot using PROC SGPLOT.  Where I am having trouble is getting the month boundaries to show well.  Using a STEP plot I can put them into the graph but they do not line up well.  I do not see anything in SG Annotation that can help so I am reaching out here to see if anyone has any better ideas than I do.  I am attaching pictures of what Rob's code produces and what both versions of mine produce. 

 

Any help is apprecaited,

Jim Horne

 

My code to do this is:

PROC TEMPLATE;

DEFINE STATGRAPH scrtmap ;

BEGINGRAPH;

LAYOUT OVERLAY / YAXISOPTS=(REVERSE=TRUE) ;

HEATMAPPARM X=week Y=yval COLORRESPONSE=ozone /DISPLAY=ALL

OUTLINEATTRS=(PATTERN=1 COLOR=CX888888 THICKNESS=1)

ROLENAME=(date=date downame=downame) TIP=(date downame) ;

STEPPLOT X=week Y=yval / GROUP=monthbnd INCLUDEMISSINGGROUP=FALSE

LINEATTRS=(PATTERN=1 THICKNESS=2 COLOR=CX000000) ;

ENDLAYOUT;

ENDGRAPH;

END;

RUN;

PROC FORMAT; *day-ofweek number --> name for y-axis;

VALUE DOW

1 = 'Sun'

2 = 'Mon'

3 = 'Tue'

4 = 'Wed'

5 = 'Thu'

6 = 'Fri'

7 = 'Sat' ;

RUN;

 

DATA calendar ; * create dataset for 1 year with ozone values;

FORMAT date DATE9.;

FORMAT week DATE7.;

FORMAT yval DOW.;

DO date = '01JAN2015'D TO '31DEC2015'D ;

weekday = PUT(date,WEEKDAY.) ;

downame = TRIM(LEFT(PUT(date,DOWNAME.))) ;

week = INTNX('WEEK',date,0,'E') ;

yearmon = PUT(date,YYMMD.) ;

yval = INPUT(weekday,1.) ;

ozone = RANUNI(1)*.5;

OUTPUT;

END;

RUN;

*add month boundarues;

PROC SORT DATA=calendar ;

BY weekday date ;

RUN;

 

DATA calendar ;

SET calendar ;

BY weekday date ;

IF yearmon NE LAG1(yearmon) THEN monthbnd = yearmon ;

IF LAST.weekday THEN DO; * add border for last month in graph;

nextmon = PUT(INTNX('MONTH',date,1),YYMMD.) ;

monthbnd = nextmon ;

DROP nextmon ;

END;

RUN;

ODS LISTING CLOSE;

FILENAME odsout "/var/server_root/pub/techserv/testrpts/" ;

ODS HTML BODY='calendar.html' (TITLE="SAS calendar")

PATH=odsout (URL=NONE)

STYLE=analysis NEWFILE=NONE ;

ODS GRAPHICS ON / IMAGENAME='monyear' NOANTIALIAS NOBORDER IMAGEMAP

HEIGHT = 200px

TIPMAX = 40000

WIDTH = 900px ;

PROC SGRENDER DATA=calendar TEMPLATE=scrtmap ; RUN;

PROC SGPLOT DATA=calendar NOAUTOLEGEND ;

SCATTER X=week Y=yval / COLORRESPONSE=ozone

MARKERATTRS=(SIZE=15px SYMBOL=squarefilled)

NAME='mapmyr'

TIP=(date downame) ;

STEP X=week Y=yval / GROUP=monthbnd NOMISSINGGROUP

LINEATTRS=(PATTERN=1 THICKNESS=2 COLOR=CX000000) ;

XAXIS DISPLAY=(NOLABEL) INTERVAL=MONTH ;

YAXIS DISPLAY=(NOLABEL) DISCRETEORDER=FORMATTED REVERSE ;

RUN;

ODS HTML CLOSE;

Grand Advisor
Posts: 9,748

Re: Graphing a year-long calendar in ODS Graphics

[ Edited ]

The link you posted doesn't work for me just clicking. I think the comma is being treated as part of the link.

 

You might need to define what you intend by "boundaries to show well". Are you attempting to show borders of month dates as Rob did originally?

 

 

Occasional Contributor
Posts: 13

Re: Graphing a year-long calendar in ODS Graphics

I appreciate lerning about the link.  I think you are right about the comma so I know how not to put a link in now.

 

On the borders, I am trying to do like ROb did and put them at the edges of the cells.  While my actual application is intended for a small technical audience and we can live with the month borders being with the data it would be easily to read and faster to undertand if they were along the edges rather than down the middle of the cells.  While this was a straightforward thing when I used the technique in SAS/Graph, I haven't figured out how to do it in the ODS Graphics procedures or GTL.  My actual application will be writtten in GTL but I'm still torn between using a heat map or a scatter plot and scatter plots are easy to code in SGPLOT. 

 

Again, any help is apprecaited,

Jim Horne

SAS Super FREQ
Posts: 1,044

Re: Graphing a year-long calendar in ODS Graphics

Just FYI...SAS9.4M3 SGPLOT includes support of HEATMAP.

 

Clearly, the calendar display is very custom, and likely done using Annotate.  You canuse annotate with SGPLOT.  

 

Or if you want a functionally equivalent display, not exactly same, you could use month as a panel variable in SGPANEL.

SAS Super FREQ
Posts: 1,044

Re: Graphing a year-long calendar in ODS Graphics

PLease see:  sas.com/content/graphicallyspeaking/2011/12/08/calendar-heatmaps-in-gtl/

Occasional Contributor
Posts: 13

Re: Graphing a year-long calendar in ODS Graphics

Sanjay,

 

I am surprised to ;earn SGPLOT cannot use annotate.  That contradicts the ODS HGraphics Procedures manual but you use this more than I do so I believe you.  I already have a heatmap coded in GTL and I am trying to learn if annotate can do what I want.  The GTL User Guide talks about anchors such as upper left and lower right in a cell but the GTL reference annotate commands do not seem to have such a thing.

 

I appreciate your input.  I will just keep plugging away at this.

Jim

SAS Super FREQ
Posts: 1,044

Re: Graphing a year-long calendar in ODS Graphics

SAS 9.3 or higher SGPLOT supports annotate.  Please see earlier reply.  However, in addition to annotate, other techniques are available.  You can select the option that fits your use case.

Occasional Contributor
Posts: 13

Re: Graphing a year-long calendar in ODS Graphics

Sanjay,

 

I apologize.  I misread "canuse" as "cannot" on the annotation.  I have tried annotation and it is doing the same thing as STEPPLOT in GTL or a STEP plot in SGPLOT is doing.  It is putting the line right down the center of the value.

 

I am stumpled on how to move the lines over to the left as in Rob Allison's orginal 10 year smog plot.  STEP/STEPPLOT looks like my best answer so far because it is the easiest to code and - in my testing - produces identical results as far more complex code.  But it still seems as if this should be able to be done!

 

Thanks,

Jim

SAS Super FREQ
Posts: 1,044

Re: Graphing a year-long calendar in ODS Graphics

Sorry for the typo.

 

I suggest SGPANEL, because that can separate each month into a separate cell.

 

Using SGANNO, I believe you can use DISCERTEOFFSET to draw the lines half way between the values.  I am on vacation, and away from my computer, so I cannot send you sample code.  Back in a week.

Post a Question
Discussion Stats
  • 8 replies
  • 592 views
  • 0 likes
  • 3 in conversation