Search code examples
graphsasannotatesgplot

SAS - Possible to display text using annotate at the same position whithin the graph whichever the size of legend and size of graph?


In SAS, I'm trying to set a 'fixed' position of an arrow to be displayed on a graph. I would like the arrow to be always displayed at the same position, whichever the size of the graph. For now, I'm using annotate to display the arrow, its coordinates x1 and y1 expressed as a percentage of the graph area (DRAWSPACE='GRAPHPERCENT'). Below is my code to generate the graph, and the annotate dataset:

DATA anno_test;
    length function $10 label $20;
    retain y1 15 drawspace 'GRAPHPERCENT';
    function='ARROW';x1=15;x2=10;y2=15;linethickness=1;shape="FILLED";OUTPUT;
    function='ARROW';x1=93;x2=98;y2=15;linethickness=1;shape="FILLED";OUTPUT;
RUN;

ODS GRAPHICS ON BORDER=OFF;
PROC SGPLOT DATA=_cumul sganno=anno_test NOBORDER;
    STYLEATTRS DATALINEPATTERNS=(1 15 2 8 4 41);
    STEP X=score Y=cum_pct/GROUP=newgroup lineattrs=(thickness=1.5);
    YAXIS LABEL="Cumulative percentage of subjects" VALUES=(0 TO 100 BY 10) VALUEATTRS=(Size=9pt) LABELATTRS=(Size=10pt Weight=bold);
    XAXIS LABEL="Score" VALUES=(-60 TO 60 BY 20) VALUEATTRS=(Size=9pt) LABELATTRS=(Size=10pt Weight=bold);
    KEYLEGEND / TITLE=" " NOBORDER VALUEATTRS=(Size=7);
    REFLINE 0 / AXIS=X LINEATTRS=(Pattern=34 Thickness=0.6);
    REFLINE 50 / AXIS=Y LINEATTRS=(Pattern=34 Thickness=0.6);
RUN;
ODS GRAPHICS OFF;

The issue I have is that, depending on the size of the legend and the size of the graph, the arrow can overlapped the x-axis. I'm not familiar with the annotate, but I tried different options and drawspace but I cann't display it perfectly regardless of the size of the legend.

Does anyone have an idea? Or should I locked the size of the graph to avoid this "issue"? Thanks!


Solution

  • Try using DATAPERCENT

    data have;
      call streaminit(2020);
      do score = -50 to 50 by 5;
        z = 100;
        do newgroup = 'A', 'B', 'C';
          cum_pct = rand('integer',floor(z/2),z);
          output;
          z = z - cum_pct;
        end;
        newgroup = 'D';
        cum_pct = z;
        output;
      end;
    run;
    
    DATA anno_test;
        length function $10 label $20;
        retain y1 15 DRAWSPACE 'DATAPERCENT';
        function='ARROW';x1=10;x2=  0;y2=15;linethickness=1;shape="FILLED";OUTPUT;
        function='ARROW';x1=90;x2=100;y2=15;linethickness=1;shape="FILLED";OUTPUT;
    RUN;
    

    Arrows pointing to edge of data area

    enter image description here

    Same arrows when data area is a wider X axis

    enter image description here

    Same arrows when annotation y values are y1 = -10; y2 = -10;

    enter image description here