Showing posts with label annotate. Show all posts
Showing posts with label annotate. Show all posts

Tuesday, April 27, 2010

Customize Legend in Figure


*******************************************************
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;
by analyte day;
if first.day;
keep analyte day;
run;

***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;

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;
run;

**Change the y-axis location for day=14 for both legend symbols;
data annolegend;
set anno1;
if day=14 then do;
if y=20 then y=90;
if y=13 then y=83;
end;
run;

**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
gunit=pct
fby=hwcgm001
hby=3

ftext=hwcgm001
htext=3
dev=CGMOFML
gsfmode=replace
gsfname=outplot
hsize=3in vsize=2.32in;
**Make sure to set up the symbol statements to correspond to the annotated legend you just made!;

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;
plot concentration*time=period/nolegend
vaxis=axis1
haxis=axis2
anno=annolegend;
run;
quit;

filename outplot clear;

See Also:
1. Good explanation of annotate facility

Tuesday, April 20, 2010

Embedded Graph


********************************************
Embed a smaller graph into the graph window
of a larger graph using Proc Greplay
*******************************************


****Data used is the average concentration at each timepoint, grouped by dose****

***plot data on linear scale, main graph;

goptions
reset=ALL
gunit=pct
colors=(black)
ftext=hwcgm001
ftitle=hwcgm009
htext=3
noborder
maxcolors=255
device=CGMOFML
hsize=10 in vsize=8;

symbol1 value=diamond i=j color=black h=2.5;
symbol2 value=triangle i=j color=black h=2.5;
symbol3 value=circle i=j color=black h=2.5;
symbol4 value=square i=j color=black h=2.5;
symbol5 value=dot i=j color=black h=2.5;
symbol6 value=diamond i=j color=black h=2.5;
symbol7 value=triangle i=j color=black h=2.5;
symbol8 value=circle i=j color=black h=2.5;

axis1 value=(a=90 h=3pct "0" "200" "400" "600" "800") order=(0 to 800 by 200) minor=none label=(a=90 color=black "Mean Concentration") offset=(1);

axis2 order=(0 to 80 by 20) minor=none label=(color=black "Time (hr)") offset=(1);

legend1 down=11 position=(right bottom inside) mode=share label=none value=(justify=left "10mg " "30mg " "90mg " "200mg " "500mg " "1000mg " "1500mg " "2000mg " );
*down=11 will push the legend up from it's original position at the bottom right;

proc gplot data=graphdata;
plot concentration*time=dose/
vaxis=axis1
haxis=axis2
legend=legend1;
run;
quit;

***2nd plot in log scale to be embedded;

symbol1 value=diamond i=j color=black h=2.5;
symbol2 value=triangle i=j color=black h=2.5;
symbol3 value=circle i=j color=black h=2.5;
symbol4 value=square i=j color=black h=2.5;
symbol5 value=dot i=j color=black h=2.5;
symbol6 value=diamond i=j color=black h=2.5;
symbol7 value=triangle i=j color=black h=2.5;
symbol8 value=circle i=j color=black h=2.5;

axis1 order=(.5,5,50,500) logbase=10 logstyle=expand label=none;

axis2 order=(0 to 80 by 20) minor=none label=none;

goptions hsize=5 in vsize=3 in noborder htext=3;

proc gplot data=graphdata;
plot concentration*time=dose/nolegend noframe
vaxis=axis1
haxis=axis2;
run;
quit;

*******Embed the second graph into the upper right area of the first graph;

filename outplot 'Z:\Embedded\Embeddedgraph.cgm';

goptions hsize=10 in vsize=8 in display gsfmode=replace gsfname=outplot;

proc greplay igout=work.gseg nofs;
tc work.templts;
tdef newtemp

1/llx=1 lly=1
ulx=1 uly=99
urx=99 ury=99
lrx=99 lry=1

2/llx=50 lly=50
ulx=50 uly=94.0
urx=98.2 ury=94.0
lrx=98.2 lry=50
clip;

template newtemp;
treplay 1:gplot 2:gplot1;
*make sure graphs are numbered properly from output screen;
run;
quit;

filename outplot clear;

**See also:
1. SAS Example