0

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!

AppyPoint
  • 45
  • 6
  • Is the arrow inside your plot space or outside? I'd guess that if you moved it out of annotate to a SCATTER or TEXT statement you'd get consistent results. You can also look into MARKERCHAR, SYMBOLCHAR to add them within SGPLOT. – Reeza Nov 26 '20 at 18:32
  • Thanks Reeaz! I will try by using TEXT statment. The arrows are outside the plot space, just below the x-axis. – AppyPoint Dec 01 '20 at 10:58

1 Answers1

1

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

Richard
  • 25,390
  • 3
  • 25
  • 38
  • Thanks Richard! This solution is working well to display the arrows inside, in the data area. But I was struggling to do the same outside the data area, like just below the x-axis. Depending on the legend size, the area below will not have the same size. I tried LAYOUTPERCENT, it's working but not the best solution as the arrows are still moving (let me know if I'm not clear enough). – AppyPoint Dec 01 '20 at 10:57
  • 2
    Did you know SGPLOT datapercent coordinates allow a negative Y ? Try `y1=-10; y2=-10;`. What are the arrows supposed to be pointing at ? Are you allowing that the legend size and placement can also be specified (i.e. don't rely on automatic layout) – Richard Dec 01 '20 at 12:30
  • Thanks for your response, Richard, you give me the solution: to also specify the placement of the legend, it's a very good idea and should solve my problem! And I used negative coordinates for Y to place the arrows below the X-axis. – AppyPoint Dec 09 '20 at 13:05