## Scatterplot with negative data (SGPLOT)

Solved
Frequent Contributor
Posts: 146

# Scatterplot with negative data (SGPLOT)

I have a scatterplot with mostly positive data, but some negative Y values. I want to distinguish between the positive and negative values. I'm currently drawing the axes with a refline statement at 0.

One option would be to have the x-axis labels next to this refline - emphasising that this where the x-axis lies. Can this be done?

Another would be to shade the positive area different from the negative area (sounds more difficult).

Any suggestions?

Accepted Solutions
Solution
‎06-09-2016 10:33 PM
SAS Super FREQ
Posts: 1,047

## Re: Scatterplot with negative data (SGPLOT)

This can also be done with band plots when you know the range of your data.

data example;
do t=1 to 50;
x=ranuni(123);
if (t <= 25) then
y = -ranuni(456);
else
y = ranuni(456);
output;
end;
run;

proc sgplot data=example;
band x=x upper=1 lower=0 / fillattrs=(color=yellow);
band x=x upper=0 lower=-1 / fillattrs=(color=lavender);
refline 0 / lineattrs=(thickness=3);
scatter x=x y=y;
run;

All Replies
Super User
Posts: 9,227

## Re: Scatterplot with negative data (SGPLOT)

Hi,

Could you mock up a quick picture to explain what it looks like.  Normally you would have xaxis and yaxis defined by those statements in you sgplot call, they then have options to control where labels go and such like.  There would then be the four quadrants which highlight where the data fall.  Have you removed the axis lines and are trying to draw them yourself?

SAS Super FREQ
Posts: 4,124

## Re: Scatterplot with negative data (SGPLOT)

You can use the BLOCK statement in PROC SGPLOT to make the background of the positive and negative regions different shades of gray (or color).  I have an example in the article "Overlay categories on a histogram," which you ought to be able to modify for your case.

Solution
‎06-09-2016 10:33 PM
SAS Super FREQ
Posts: 1,047

## Re: Scatterplot with negative data (SGPLOT)

This can also be done with band plots when you know the range of your data.

data example;
do t=1 to 50;
x=ranuni(123);
if (t <= 25) then
y = -ranuni(456);
else
y = ranuni(456);
output;
end;
run;

proc sgplot data=example;
band x=x upper=1 lower=0 / fillattrs=(color=yellow);
band x=x upper=0 lower=-1 / fillattrs=(color=lavender);
refline 0 / lineattrs=(thickness=3);
scatter x=x y=y;
run;
Frequent Contributor
Posts: 146

## Re: Scatterplot with negative data (SGPLOT)

[ Edited ]

Thanks for the suggestions. The band option handles the shading issue well. But here is my output so far. I'm actually trying to represent the values of three variables: M on the x-axis, T on the y-axis and D=M+T - represented by the diagonal contours. (The points are different countries).  Having the x-axis labels at the bottom is confusing if you are trying to read of the values of D (you need to read them off where the line crosses the x-axis).

Frequent Contributor
Posts: 146

## Re: Scatterplot with negative data (SGPLOT)

I think I've settled on the below as my solution. Moving the axis labels up to the x-axis would be too cluttered. Thanks for the 'band' suggestion. (I didn't test the 'block' suggestion) (The lineparam statement is how I plot the contour lines).

proc sgplot noautolegend aspect=1;
band x=M upper=0 lower=-0.1 / transparency=0.8 fillattrs=(color=grey);
scatter x=M y=T /datalabel=ID markerattrs=(size=10) ;
lineparm x=x y=y slope=slope ;
xaxis grid max=0.6 offsetmin=0 ;
yaxis grid min=-0.1 max=0.5 offsetmin=0 values=(-0.1 to 0.5 by 0.1)
Label = "T (also D = M+T diagonally)" labelpos=top;
refline 0 /axis=x ;
refline 0 /axis=y ;
run;

This produces

🔒 This topic is solved and locked.