Example 7.5: Replicating a prettier jittered scatterplot

July 15, 2009
By

(This article was first published on SAS and R, and kindly contributed to R-bloggers)

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 his blog: SAS and R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: ,

Comments are closed.