BookmarkSubscribeRSS Feed
jessicasahut
Calcite | Level 5

SAS version: 9.04.01M1P120413

Problem:

datalabels on scatter plot colliding.  Is there a way to move the labels, while keeping the dimensions of the graph as is?  For instance, can I make it so one label is above the point and the other label is below the point?  Or better yet, is there a way to click into the graph and manually move the labels?

Parameters:

1. cannot rely on legend to identify points.  Points must be identified on the graph itself

2. cannot change dimensions.  it has to stay 5 by 3.

Example data set and plot code:

data fake;

input ID served SRVs UL LL;

if ID=1000087 then do;

label="really long name that was replaced because confidential";

end;

else if ID=1004937 then do;

label="another really long name replaced because confidential";

end;

datalines;

1 6 0 . .

3 6 0 . .

4 6 0 . .

5 6 0 . .

1000027 12 0 . .

1000129 11 0 . .

1000132 8 0 . .

1000132 10 0 . .

1000335 8 0 . .

1005796 7 0 . .

1005855 12 0 . .

2 4 1 . .

1000022 9 1 . .

1005725 13 1 . .

50 5 2 . .

67 6 2 . .

1000087 7 4 . .

1004937 4 4 . .

0 0 . 2.95692 0

0 2 . 2.97338 0

0 4 . 2.99016 0

0 6 . 3.00729 0

0 8 . 3.02478 0

0 10 . 3.04265 0

0 12 . 3.0609 0

0 14 . 3.07957 0

;

ods graphics on /height=5in width=3in border=off;

proc sgplot data=fake;

title "Trends in SRVs for small providers";

band x=served upper=UL lower=LL / LEGENDLABEL= "typical range";

scatter x=served y=SRVs /  datalabel=Label LEGENDLABEL= "# SRVs for provider";

yaxis min=0 max=5 label='Number of SRVs' grid;

xaxis min=3 max=14 label='Number of consumers served' grid;

run;

quit;


exampleplot.png
1 REPLY 1
DanH_sas
SAS Super FREQ

By default, data labels in scatter plots use a collision-avoidance algorithm. In this case, the labels were so long that they did not fit in the graph. Therefore, they chose a default labeling position. You cannot specify the position of each label yourself, but here are some options to consider:

1. Choose a smaller font size via DATALABELATTRS.

2. Sometime using a higher image DPI (> 100) can give you enough space. Specify IMAGE_DPI on ODS destination you have open.

2. Specify a user-defined format to map the long string into a shorter name for labeling (this mapping could also be done in a data step).

Hope this helps!

Dan

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 1928 views
  • 3 likes
  • 2 in conversation