Graphically Speaking

Getting Started with SGPLOT – Part 4 – Series Plot

Series plots are frequently used to visualize a numeric response on the y-axis by another numeric variable on the x-axis. Often the variable on the x-axis is a time variable, in which case these are known as TimeSeries plots. In such cases, the slope of the line provides significant information on the rate of change of the response. A basic Series plot of a response over time along with the data set is shown on the right.

SAS Certifications Tutorials and Materials, SAS Certifications Guide, SAS Certifications, SAS Certifications Syllabus, SAS Series Plot

This graph shows the plot of “Drug_A” variable by Date. The data set has multiple columns, one each for the response for each drug. We are plotting only the response for Drug_A. Since the x-axis uses a numeric variable with a SAS date format, the x-axis only displays the “year” when necessary. The SGPLOT code for this graph is shown below.

SAS Certifications Tutorials and Materials, SAS Certifications Guide, SAS Certifications, SAS Certifications Syllabus, SAS Series Plottitle ‘Response by Date’;
proc sgplot data=seriesMultiVar subpixel;
series x=date y=Drug_A;
xaxis display=(nolabel);
run;

I have set an x-axis option to suppress the display of the x-axis label as it is redundant in this graph. You can click on the graph to get a higher resolution image. There you will see that the curve is not smooth as there is a 15 day interval between the observations. Each (x, y) point is connected with a straight line in data order, creating a faceted appearance.

SAS Certifications Tutorials and Materials, SAS Certifications Guide, SAS Certifications, SAS Certifications Syllabus, SAS Series Plot

The curve will be smoother if more data points are provided. Or, in this case we can add the SMOOTHCONNECT option to get a smoother display of the data. The curve still passes through each data point that is provided. The graph on the right also increases the thickness of the connect line using the LINEATTRS=(THICKNES=3) option.

title ‘Response by Date’;
proc sgplot data=seriesMultiVar subpixel;
series x=date y=Drug_A / smoothconnect
lineattrs=(thickness=3);
xaxis display=(nolabel);
run;

SAS Certifications Tutorials and Materials, SAS Certifications Guide, SAS Certifications, SAS Certifications Syllabus, SAS Series Plot

As you can see in the view of the data set above, we have three different columns for drug response, “Drug_A”, “Drug_B” and “Drug_C”. To get a plot of all three columns, we simply add as many SERIES plot statements as we need. For each plot we have the option to set the plot attributes as we want such as the line color or pattern, or the thickness or other attributes. Note the NOBORDER option to drop the inner frame around the data.

title ‘Response by Date’;
proc sgplot data=seriesMultiVar subpixel noborder;
series x=date y=Drug_A / smoothconnect lineattrs=(thickness=3);
series x=date y=Drug_B / smoothconnect lineattrs=(thickness=3);
series x=date y=Drug_C / smoothconnect lineattrs=(thickness=3);
xaxis display=(nolabel);
yaxis label=’Response’;
run;

Note, in the code above, we have not specified the line color or pattern. However, the SGPLOT procedure has detected the overlay of three series, and has automatically assigned different attributes for each series from the GraphData1-12 style elements of the active style, in this case LISTING. The CYCLEATTRS option does this assignment. You can turn off CYCLEATTRS if you do not want such automatic attribute assignment. Note a legend is automatically generated to display the response labels for each curve.

Often users have questions on what is the best way to arrange their data to create SERIES plots. in the case above, separate SERIES statements are used, one for each variable. However, sometimes the number of curves in a data set may vary from day to day. In such cases it is better to use the GROUP feature of the SERIES plot.

SAS Certifications Tutorials and Materials, SAS Certifications Guide, SAS Certifications, SAS Certifications Syllabus, SAS Series Plot

In the data shown on the center, we have transposed the multi-variable “wide” data into a “long” grouped format. Here, the variable to be plotted is provided as a group, with the value provided as a single Response column. Now, we can use the GROUP option of the series plot to display this data using one SERIES statement. The curves are plotted using the group id to connect the points for the curve. All the curves now get same attributes except the group color and pattern, which are changed automatically based on group.

ods graphics / reset attrpriority=color;
title ‘Response by Date’;
proc sgplot data=seriesGroup noborder;
series x=date y=response / group=drug
lineattrs=(thickness=3) curvelabel
arrowheadpos=end arrowheadshape=barbed;
xaxis display=(nolabel);
yaxis display=(noline noticks) grid;
run;

In the graph above, I have made the following changes:

  1. I added curve labels, which often make it easier to decode the information in the graph as the variable name is placed close to the curve, and one does not have to refer to the legend to decode the data.
  2. I added arrowheads. Note, for arrowheads to work well with thick lines, one has to provide enough distance between the last two observations on the curve. If you look at the code linked below, you will see I have done just that.
  3. I used the YAXIS statement to turn off the y-axis line and ticks and added grid lines. This provides an alternate clean appearance for the graph.
  4. I used ATTRPRIORITY=COLOR in the ODS GRAPHICS statement to avoid usage of line patterns in this graph.

Finally, the SERIES plot statement has support for multiple grouping for color and patterns. This is very useful to create Spaghetti Plots, where the curves are grouped by multiple classifiers, such as Year and Region. This can be done using the grouplc and grouplp options.

SGPLOT code:

%let gpath=’.’;
ods html close;
%let w=4in;
%let dpi=200;
ods listing gpath=&gpath image_dpi=&dpi;

/*–Grouped Series–*/
data seriesMultiVar;
label Drug_A=’Drug A’ Drug_B=’Drug B’ Drug_C=’Drug C’;
drop i;
format Date Date9.;
do i=0 to 355 by 15, 365 to 365 by 1;
date=’01jan2009’d+i;

Drug_A = 16+ 3*sin(i/90+0.5) + 1*sin(3*i/90+0.7);
Drug_B = 10+ 3*sin(i/90+0.5) + 1*cos(3*i/90+0.7);
Drug_C = 10+ 3*cos(i/90+0.5) + 1*sin(3*i/90+0.7);;
output;
end;
run;

/*ods html;*/
/*proc print data=seriesMultiVar(obs=6);*/
/* var date drug_a drug_b drug_c;*/
/*run;*/
/*ods html close;*/

/*–Grouped Series–*/
data seriesGroup;
drop Drug_A Drug_B Drug_C;
set seriesMultiVar;
Drug=’Drug A’; Response=Drug_A; output;
Drug=’Drug B’; Response=Drug_B; output;
Drug=’Drug C’; Response=Drug_C; output;
run;

/*ods html;*/
/*proc print data=seriesGroup(obs=6);*/
/* var date drug response;*/
/*run;*/
/*ods html close;*/

/*–Series Plot–*/
ods graphics / reset width=5in height=3in imagename=’Series’;
title ‘Response by Date’;
proc sgplot data=seriesMultiVar;
series x=date y=Drug_A;
xaxis display=(nolabel);
run;

/*–Series Plot Sparse Smooth Thick–*/
ods graphics / reset width=5in height=3in imagename=’SeriesSmoothThk’;
title ‘Response by Date’;
proc sgplot data=seriesMultiVar subpixel;
series x=date y=Drug_A / smoothconnect
lineattrs=(thickness=3);
xaxis display=(nolabel);
run;

/*–Multi Var Series Plot Sparse Smooth Thick–*/
ods graphics / reset width=5in height=3in imagename=’SeriesMulti’;
title ‘Response by Date’;
proc sgplot data=seriesMultiVar subpixel noborder;
series x=date y=Drug_A / smoothconnect lineattrs=(thickness=3);
series x=date y=Drug_B / smoothconnect lineattrs=(thickness=3);
series x=date y=Drug_C / smoothconnect lineattrs=(thickness=3);
xaxis display=(nolabel);
yaxis label=’Response’;
run;

/*–Grouped Series Plot Sparse Smooth Thick Label–*/
ods graphics / reset width=5in height=3in attrpriority=color imagename=’SeriesGroupLabelArrow’;
title ‘Response by Date’;
proc sgplot data=seriesGroup subpixel noborder;
series x=date y=response / group=drug lineattrs=(thickness=3)
arrowheadpos=end arrowheadshape=barbed curvelabel;
xaxis display=(nolabel);
yaxis display=(noline noticks) grid;
run;

/*–Icon–*/
ods listing image_dpi=100;
ods graphics / reset width=2.7in height=1.8in attrpriority=color imagename=’SeriesIcon’;
title ‘Response by Date’;
proc sgplot data=seriesGroup subpixel noborder;
series x=date y=response / group=drug lineattrs=(thickness=3)
arrowheadpos=end arrowheadshape=barbed curvelabel;
xaxis display=(nolabel);
yaxis display=(noline noticks) grid;
run;

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s