## Can I generate a jittered scatterplot with tight jittering and an added regression line?

Solved
Occasional Contributor
Posts: 15

# Can I generate a jittered scatterplot with tight jittering and an added regression line?

I’m using SAS 9.4.

Please run  the attached code. I would like to add the regression line to the third of the three scatterplots. But, so far, I haven’t been able to find an easy way to do that.

Of course, I can use the SG Annotation facility to add the line, but that’s inconvenient because it requires explicit computation and specification of the attributes of the line.

It’s statistically sensible to add the line to the scatterplot because the scatterplot reflects a real situation in experimental research. This is the situation where the predictor variable is discrete (by virtue of the fact that this variable was MANIPULATED to have discrete values by the experimenter) but the underlying DIMENSION of the predictor variable is continuous. Therefore, adding the regression line to the scatterplot is reasonable as a way of (roughly) illustrating through interpolation what the values of the response variable do between the two used values of the predictor variable on the horizontal axis of the scatterplot. Also, the regression line helps the viewer to recognize the continuous nature of the relationship between the response and predictor variables.

Is there an easy way to add the line?

I understand that scatterplots with “dodging” points are difficult to program, and they may run into situations where the points from different values on the horizontal axis collide with each other. But that doesn’t mean that the scatterplots shouldn’t be available for the many cases in which the points don’t collide and thus the scatterplots are useful.

Presumably the programmer could build a map in memory of the array of points that are lying on the surface of the scatterplot. And if the array is too tight and therefore the points start to collide with each other, then the procedure could try to make the points smaller. And if that doesn’t work, the procedure could stop with an error message that clearly explains the problem.

Note that I’ve got SAS to generate the tightly dodged points on the third scatterplot by specifying that the horizontal axis is discrete. However, that gives rise to another serious problem if the variable plotted on the horizontal axis has values that aren’t equally spaced. In that case, if I specify that the horizontal axis is discrete, then the values on the horizontal axis will equally spaced on the axis even though in reality they should be spaced according to the values.

For me, the ideal solution to this problem would be to have an option on the SCATTER statement that permits “dodging” of points in the horizontal direction, where “dodging” means that the points are only offset enough to avoid collisions, and not randomly offset as they are with the “jitter” option. I suppose that dodging in the vertical direction or dodging in both directions might also sometimes be useful.

Don Macnaughton

Accepted Solutions
Solution
‎02-06-2015 11:17 AM
SAS Super FREQ
Posts: 1,265

## Re: Can I generate a jittered scatterplot with tight jittering and an added regression line?

Dan and I have looked into this and the solution depends on the maintenance level of the SAS9.4 release.  So, it helps if you include the "full" release number.  :-)

1. If you have SAS 9.4M2, you can use JITTERWIDTH option on the SCATTER statement.  JitterWidth=0.1 works.
2. If you have SAS 9.4M1 or higher, you can use this "trick".  Create a dummy column Grp='a'.  Then assign this to the scatter option GROUP and set GroupDisplay=Cluster as follows.  This also does the non-random jitter.  See graph below:

scatter x=Codeine y=Relief / group=Grp groupdisplay=cluster jitter;

3.  If you have SAS 9.4M0, then you will need to use GTL with similar code.  Jitterwidth option is supported on the SCATTERPLOT statement in GTL at SAS 9.4M0.  You can get GTL code out of SGPLOT by using the TMPLOUT option on the proc statement.

All Replies
Solution
‎02-06-2015 11:17 AM
SAS Super FREQ
Posts: 1,265

## Re: Can I generate a jittered scatterplot with tight jittering and an added regression line?

Dan and I have looked into this and the solution depends on the maintenance level of the SAS9.4 release.  So, it helps if you include the "full" release number.  :-)

1. If you have SAS 9.4M2, you can use JITTERWIDTH option on the SCATTER statement.  JitterWidth=0.1 works.
2. If you have SAS 9.4M1 or higher, you can use this "trick".  Create a dummy column Grp='a'.  Then assign this to the scatter option GROUP and set GroupDisplay=Cluster as follows.  This also does the non-random jitter.  See graph below:

scatter x=Codeine y=Relief / group=Grp groupdisplay=cluster jitter;

3.  If you have SAS 9.4M0, then you will need to use GTL with similar code.  Jitterwidth option is supported on the SCATTERPLOT statement in GTL at SAS 9.4M0.  You can get GTL code out of SGPLOT by using the TMPLOUT option on the proc statement.

Occasional Contributor
Posts: 15

## Re: Can I generate a jittered scatterplot with tight jittering and an added regression line?

Sanjay,

Thank you.  I have SAS 9.4M2.  Curiously, your first solution still causes the two points for Codeine level 1 at Relief level 0.4 to be overlaid.  However, your second solution works perfectly.

I hope that SAS will provide an easier way to get the desired behavior, perhaps with a "dodge" option for the SCATTER statement.

I tried to find the way to specify that my question is answered, but I couldn't find the way.

Don Macnaughton

SAS Super FREQ
Posts: 1,265