Hi,
I have use proc sgplot scatter to create a graph, but I need a line with arrow to point the dots. Here is the example:
I have used function = 'arrow', but I need to make the arrow smaller, which is not possible since I have used the smallest size = 1 and line = 1 to create this arrow. Is there another way to create arrow, since I need it to be smaller?
function='arrow';x1space="datavalue";y1space="datavalue";x2space="datavalue";y2space="datavalue";desc='Arrow';
x1=x_y1;y1=y_y1;
x2=x_y2;y2=y_y2;
line=0.3; size=0.3;
direction='in';linecolor='CXC00000';linepattern='34';linethickness=&t_linethickness.;shape='filled';scale=0.001;
Thanks!
You could try VECTOR statement instead.
data x;
input x y x1 y1;
cards;
0 0 1 0
;
proc sgplot data=x;
vector x=x1 y=y1/xorigin=x yorigin=y ARROWHEADSHAPE=filled LINEATTRS=(color=darkred pattern=dash ) ;
run;
You could try VECTOR statement instead.
data x;
input x y x1 y1;
cards;
0 0 1 0
;
proc sgplot data=x;
vector x=x1 y=y1/xorigin=x yorigin=y ARROWHEADSHAPE=filled LINEATTRS=(color=darkred pattern=dash ) ;
run;
Thank you Ksharp! Is there any condition that I can add? because I would like to have a solid line for certain data label, and dash for others. And this can change depending on situation.
In the code provided by @Ksharp , add additional VECTOR commands (with different data, different colors and different line patterns).
VECTOR plots can be grouped via the GROUP option. Set your data label variable on the GROUP option and you should get the attribute changes on the vectors. If you want only the line pattern to change, I would set ATTRPRIORITY=COLOR on the ODS GRAPHICS statements to ensure that the line pattern changes faster than the color. If you want to control which data label gets a particular line pattern, you can use a discrete attributes map to make that assignment.
Thank you @PaigeMiller , @DanH_sas ! I'm pushing the limit here, can I make the size of the arrowhead smaller? It is because I have the graph on a whole page of the powerpoint, and it looks a bit too big. Is this possible?
You could use SCATTER or TEXT to mock the arrowheader and change its size or color.
data x;
input x y x1 y1 group x2 y2 x3 y3;
cards;
0 0 1 0 1 1 0 . .
0.5 -1 0.5 1 2 . . 0.5 1
;
ods graphics/attrpriority=none;
proc sgplot data=x;
styleattrs datacolors=(red blue) datalinepatterns=(dash solid);
vector x=x1 y=y1/group=group xorigin=x yorigin=y noarrowheads ;
scatter x=x2 y=y2/markerattrs=graphdata1(symbol=trianglerightfilled size=8 );
scatter x=x3 y=y3/markerattrs=graphdata2(symbol=trianglefilled size=20 );
run;
data x;
input x y x1 y1 group x2 y2 ;
cards;
0 0 1 0 1 1 0 . .
0.5 -1 0.5 1 2 0.5 1 ..
;
ods graphics/attrpriority=none;
proc sgplot data=x;
styleattrs datacolors=(red blue) datalinepatterns=(dash solid) datasymbols=(trianglerightfilled trianglefilled );
vector x=x1 y=y1/group=group xorigin=x yorigin=y noarrowheads ;
scatter x=x2 y=y2/group=group markerattrs=( size=6 );
run;
Thanks @Ksharp ! Unfortunately, I need the arrow to follow the direction of the lines, so it is not always horizontal or vertical.
Just FYI...SERIES plot also supports the arrowhead option. Different data structure.
OK. Try SCATTER statement.
data x;
input x y group m;
cards;
0 0 1 1
1 0 1 1
0.5 -1 2 2
0.5 1 2 2
0.5 0.5 3 .1
1 1 3 .1
;
ods graphics/attrpriority=none;
proc sgplot data=x;
styleattrs datacolors=(red blue) datalinepatterns=(dash solid) ;
series x=x y=y/group=group arrowheadpos=end ARROWHEADSHAPE=filled ARROWHEADSCALE=0.1 thickresp=m;
run;
Oh noo... @Ksharp , series connects all dots together, and I don't want that. This is what I want to have:
You can use the GROUP option on the SERIES to group your series points based on the label information you showed (Sherly, Robert, etc.). For the SERIES plot, your data will use an X, Y, and GROUP column. There will be 5 observations for the group "Sherly", and two observation for each of the other groups. Do you want each of the line to have the same properties, or do you want them to be different per group?
Thanks @DanH_sas, @Ksharp ! I tried to do that but I don't know why it is big:
Here is my code:
proc sgplot data=prep_graph_&t_i._graph sganno=anno noautolegend noopaque nowall noborder pad=&t_pad.;
styleattrs datacolors=('CXC00000') datalinepatterns=(dot solid);
series x=x_value y=y_value/
datalabel=t_label_f
datalabelattrs=(size=&t_text_size_pt.pt family="&global_font.")
markerattrs=(symbol=squarefilled size=&t_marker_size.px)
group=t_label arrowheadpos=end ARROWHEADSHAPE=filled ARROWHEADSCALE=0.1 thickresp=m;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.