*******************************************************
Proc Gplot using Annotate
**Place legend depending on 'by' statement
**Resize so that 6 plots fit on one page in a word doc
*******************************************************
*****Sort concentration over time dataset that is composed of 3 analytes, 2 days, and 2 periods
proc sort data=exampledata;
by analyte day period;run;
**********************************
Create annotation dataset for legend
**********************************
**Create dataset with only one observation for each analyte and day (the periods will be on the same graph)
data first;
set exampledata;run;
by analyte day;
if first.day;
keep analyte day;
***Create the annotation dataset using xsys and ysys '1' (Referencing 0 to 100% of axis area), function, style, and text
data anno1;
*format variables - 'by' variables MUST be same format as in dataset. Annotation dataset variables MUST be in the proper format;run;
format analyte $4. day x y size best12. xsys ysys $1. function style text $8.;
set first;
by analyte day;
*retain the variables that will always remain the same;
retain xsys '1' ysys '1';
*Function 'move' will move the position to 67% of the x-axis and 20% of y-axis;
*Function 'draw' will draw a line of default type (black solid) until 70% of x-axis;
*Function 'label' will put in a marker associated with the text 'U' (a square) - font list here;
*Continue this process again to make a longer line and another marker, and then label with the text 'Period 1';
*Remember to output each line so that you will create one of each line for each observation in the 'first' dataset;
function='move'; x=67; y=20; size=.3; output;
function='draw'; x=70; output;
function='label'; x=72; style='markere'; text='U'; output;
function='draw'; x=75; style=''; text=''; output;
function='label'; x=78; style='markere'; text='U'; output;
function='draw'; x=81; style=''; text=''; output;
function='label'; x=88; style='hwcgm001';text='Period 1';size=.4; output;
function='move'; x=67; y=13; size=.3; style=''; text=''; output;
function='draw'; x=70; style=''; text=''; output;
function='label'; x=72; style='marker'; text='W'; output;
function='draw'; x=75; style=''; text=''; output;
function='label'; x=78; style='marker'; text='W'; output;
function='draw'; x=81; style=''; text=''; output;
function='label'; x=88; style='hwcgm001';text='Period 2';size=.4; output;
keep analyte day function x y xsys ysys size style text;
**Change the y-axis location for day=14 for both legend symbols;
data annolegend;
set anno1;run;
if day=14 then do;
if y=20 then y=90;
if y=13 then y=83;
end;
**Excerpt from what annolegend dataset looks like:
proc sort data=exampledata;
by analyte day;run;
proc sort data=annolegend;
by analyte day;run;
*******************************************
***Create the plots and output to CGM file;
*******************************************
filename outplot 'C:\Annotatedlegend\';
**Resize using hsize and vsize so that 6 plots will fit on one word doc page;
goptions
reset=ALL**Make sure to set up the symbol statements to correspond to the annotated legend you just made!;
gunit=pct
fby=hwcgm001
hby=3
ftext=hwcgm001
htext=3
dev=CGMOFML
gsfmode=replace
gsfname=outplot
hsize=3in vsize=2.32in;
symbol1 value=square i=j color=black h=3;
symbol2 value=dot i=j color=black h=3;
**Here the axis is being cut to 68% to aid with resizing. Also use log scale;
axis1 offset=(,3) value=(a=90) length=68 pct
logbase=10 logstyle=expand label=(a=90 color=black "Concentration (ng/mL)");
axis2 order=(0 to 12 by 2) label=(height=3 color=black "Time (hr)") minor=none;
proc gplot data=exampledata;
by analyte day;run;
plot concentration*time=period/nolegend
vaxis=axis1
haxis=axis2
anno=annolegend;
quit;
filename outplot clear;
See Also:
1. Good explanation of annotate facility