Graphically Speaking

Animal life expectancy graph

I published an article on creating bar charts with visual category values. The idea was to use visual icons for the category values in a HBAR of age by animal. For the data, I referred to a visual from the 2010 Encyclopedia Britannica Inc. that I found on the web. I included that visual in my article, and many readers were intrigued by the visual itself, and how it cleverly placed many items on the compressing squiggly timeline.

A couple of readers proposed some ideas to create the visual using SGPLOT, and I decided to follow up over the weekend. The results are shown below. The first task is to generate the sinusoidal timeline with a reducing pitch so there is more space available in the early part where there are more animals, and less space later. This was easy to do by using the TYPE=log option on the y-axis. I computed a simple sine wave over 7.5 cycles, with y starting with 1 and ending with 751. Since I want to use the log option, I need to avoid zero or negative values on the y-axis.

The shape of the log transformed sine wave with ymin=1 was undesirable. The ymin value can be adjusted to obtain the shape you want. Here are two curves. I settled on the curve on the right with ymin=50.

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

Now, I created a separate “Animals” data set with animal name and age. I computed the equivalent x and y coordinates for each animal by using the reverse equation from the one used to generate the sine curve. For the curve, the value of age=20*angle/360. So, the (x, y) of animal by age can be computed as angle= age*360/20. Then, (x, y) can be computed as in the timeline data.

A simple join of the timeline and animals data set results in the combined data set that has all the information we need to plot the graph. Once again, we use the SYMBOLIMAGE statement to define image markers for each of the animal and use the STYLEATTRS statement to enter these into the group symbol list. Then, we can use the SCATTER statement to display the image of the animal at the appropriate (x, y) location. I shifted the icon up by 2 units. The name of the animal is displayed below the icon using a TEXT statement.

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

I only had the patience to place 9 icons on the timeline, but it would be straightforward for the motivated person to add all the animals needed. A label for each animal is placed below the icon using a TEXT plot statement.

SAS 9.40M3 SGPLOT program:

title ‘Life Expectancy for Different Animals’;
footnote j=l h=6pt ‘Maximum age, in years that certain animals may be expected to reach, based on reports of zoos and estimates of biologists.’;
footnote2 j=l h=6pt’Data from S. S. Flower, “The Duration of Life in Animals”, Proceedings of the London Zoological Society.’;
footnote3 j=l h=6pt ‘Based on 2010 Encyclopedia Britannica, Inc.’;

proc sgplot data=combined nowall noborder subpixel noautolegend;
symbolimage name=human image=”&human”;
symbolimage name=parrot image=”&parrot” / voffset=-0.2;
symbolimage name=elephant image=”&elephant”;
symbolimage name=turtle image=”&turtle”;
symbolimage name=bear image=”&bear”;
symbolimage name=queenant image=”&queenant”;
symbolimage name=squirrel image=”&squirrel”;
symbolimage name=mouse image=”&mouse”;
symbolimage name=dragonfly image=”&dragonfly”;

styleattrs datasymbols=(dragonfly mouse squirrel queenant bear turtle elephant parrot human);

series x=x y=y / lineattrs=(thickness=3) transparency=0.5;
text x=xage y=yage text=age / position=top textattrs=(size=6);
scatter x=xicon y=yicon / group=animal nomissinggroup markerattrs=(size=30);
text x=xicon y=ylbl text=animal / position=bottom textattrs=(size=6);
xaxis display=none offsetmin=0.05 offsetmax=0.05;
yaxis display=none reverse type=log logbase=2;
run;

Note the following features of the program:

  • A SERIES plot to draw the curve.
  • A TEXT plot to display the age values along the curve.
  • A SCATTER plot to display the image of the animals.
  • A TEXT plot to display the name of the animals below the icon.
  • Y-axis with TYPE=LOG and LOGBASE=2.

Full SAS 9.4M3 code:

%let gpath=C:\;
%let dpi=200;
ods html close;
ods listing gpath=”&gpath” image_dpi=&dpi;

/*–Compute coordinates for sinusoidal line–*/
data timeline;
pi=constant(“PI”);
a=200;
do angle=0 to 7.5*360 by 10;
y=(1+angle/36);
y50=y+50;
x=a*sin(angle*pi/180);
output;
end;
run;

/*–Timeline curve–*/
data LifeExpectancy;
pi=constant(“PI”);
a=200;
do angle=0 to 7.5*360 by 10;
y=(50+angle/36);
x=a*sin(angle*pi/180);
value=20*angle/360;
xage=.; yage=.; age=.;
if mod(value, 5)=0 then do;
xage=x; yage=y; age=value;
end;
output;
end;
run;

/*ods html;*/
/*proc print;run;*/
/*ods html close;*/

/*–Animal with ages–*/
/*–Use same equation as above to compute (x, y) for age–*/
data animals;
keep animal xicon yicon ylbl;
input animal $1-15 age;
pi=constant(“PI”);
a=200;
angle=age*360 / 20;
yicon=(50+angle/36)-2;
xicon=a*sin(angle*pi/180);
ylbl=yicon+2;
datalines;
DragonFly 1
Mouse 4
Squirrel 9
QueenAnt 16
Bear 37
Turtle 44
Elephant 49
Parrot 66
Human 100
;
run;

/*ods html;*/
/*proc print;run;*/
/*ods html close;*/

/*–Combine the timeline and animal ages–*/
data combined;
set lifeExpectancy animals;
run;

/*ods html;*/
/*proc print;run;*/
/*ods html close;*/

/*–Sine Wave–*/
ods graphics / reset width=3in height=4in imagename=’TimeLineLog’;
title ‘Time Line Y=1 to 750′;
proc sgplot data=timeline nowall noborder subpixel noautolegend;
series x=x y=y / lineattrs=(thickness=3) transparency=0.5;
xaxis display=none offsetmin=0.05 offsetmax=0.05;
yaxis display=none reverse type=log logbase=2;
run;

/*–Sine Wave–*/
ods graphics / reset width=3in height=4in imagename=’TimeLineLog_50’;
title ‘Time Line Y= 50 to 800′;
proc sgplot data=timeline nowall noborder subpixel noautolegend;
series x=x y=y50 / lineattrs=(thickness=3) transparency=0.5;
xaxis display=none offsetmin=0.05 offsetmax=0.05;
yaxis display=none reverse type=log logbase=2;
run;

/*–Icon File Names–*/
%let human=&gpath..\Icons\David_Trans.png;
%let parrot=&gpath..\Icons\Parrot_Trans.png;
%let elephant=&gpath..\Icons\Elephant_Trans.png;
%let turtle=&gpath..\Icons\Turtle_Trans.png;
%let bear=&gpath..\Icons\Bear_Trans.png;
%let queenant=&gpath..\Icons\QueenAnt_Trans.png;
%let squirrel=&gpath..\Icons\Squirrel_Trans.png;
%let mouse=&gpath..\Icons\Mouse_Trans.png;
%let dragonfly=&gpath..\Icons\DragonFly_Trans.png;

/*–Make Graph–*/
ods graphics / reset width=5in height=5in imagename=’LifeExpectancyLabel’;
title ‘Life Expectancy for Different Animals’;
footnote j=l h=6pt ‘Maximum age, in years that certain animals may be expected to reach, based on reports of zoos and estimates of biologists.’;
footnote2 j=l h=6pt’Data from S. S. Flower, “The Duration of Life in Animals”, Proceedings of the London Zoological Society.’;
footnote3 j=l h=6pt ‘Based on 2010 Encyclopedia Britannica, Inc.’;

proc sgplot data=combined nowall noborder subpixel noautolegend;
symbolimage name=human image=”&human”;
symbolimage name=parrot image=”&parrot” / voffset=-0.2;
symbolimage name=elephant image=”&elephant”;
symbolimage name=turtle image=”&turtle”;
symbolimage name=bear image=”&bear”;
symbolimage name=queenant image=”&queenant”;
symbolimage name=squirrel image=”&squirrel”;
symbolimage name=mouse image=”&mouse”;
symbolimage name=dragonfly image=”&dragonfly”;

styleattrs datasymbols=(dragonfly mouse squirrel queenant bear turtle elephant parrot human);

series x=x y=y / lineattrs=(thickness=3) transparency=0.5;
text x=xage y=yage text=age / position=top textattrs=(size=6);
scatter x=xicon y=yicon / group=animal nomissinggroup markerattrs=(size=30);
text x=xicon y=ylbl text=animal / position=bottom textattrs=(size=6);
xaxis display=none offsetmin=0.05 offsetmax=0.05;
yaxis display=none reverse type=log logbase=2;
run;

Zip file of icons (Unzip to C:\Icons folder): Icons

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