# Example 7.5: Replicating a prettier jittered scatterplot

[This article was first published on

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

**SAS and R**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The scatterplot in section 7.4 is a plot we could use repeatedly. We demonstrate how to create a macro (SAS, section A.8) and a function (R, section B.5) to do it more easily.

**SAS**

%macro logiplot(x=x, y=y, data=, jitterwidth=.05, smooth=50);

data lp1;

set &data;

if &y eq 0 or &y eq 1;

jitter = uniform(0) * &jitterwidth;

if &y eq 1 then yplot = &y - jitter;

else if &y eq 0 then yplot = &y + jitter;

run;

axis1 minor=none label = ("&x");

axis2 minor=none label = (angle = 270 rotate = 90 "&y");

symbol1 i=sm&smooth.s v=none c=blue;

symbol2 i=none v=dot h=.2 c=blue;

proc gplot data=lp1;

plot (&y yplot) * &x / overlay haxis=axis1 vaxis=axis2;

run;

quit;

**R**

logiplot <- function(x, y, jitamount=.01, smoothspan=2/3,

xlabel="X label", ylabel="Y label") {

jittery <- jitter(y, amount=jitamount/2)

correction <- ifelse(y==0, jitamount/2, -jitamount/2)

jittery <- jittery + correction

plot(x, y, type="n", xlab=xlabel, ylab=ylabel)

points(x, jittery, pch=20, col="blue")

lines(lowess(x, y, f=smoothspan), col="blue")

}

We’ll load the example data set from the book via the web (section 1.1.6), then make a plot of the real data.

**SAS**

filename myurl

url 'http://www.math.smith.edu/sasr/datasets/help.csv' lrecl=704;

proc import

datafile=myurl

out=ds dbms=dlm;

delimiter=',';

getnames=yes;

run;

%logiplot(x = mcs, y = homeless, data=ds, smooth=40);

**R**

ds <- read.csv("http://www.math.smith.edu/sasr/datasets/help.csv")

logiplot(ds$mcs, ds$homeless, jitamount=.05, smoothspan=.3,

xlabel="mcs", ylabel="homeless")

The resulting plots are quite similar, but still differ with respect to the smoother and the exact jitter applied to each point.

(Click for a bigger picture.)

To

**leave a comment**for the author, please follow the link and comment on their blog:**SAS and R**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.