**SAS and R**, and kindly contributed to R-bloggers)

The scatterplot is one of the most ubiquitous, and useful graphics. It’s also very basic. One of its shortcomings is that it can hide important aspects of the marginal distributions of the two variables. To address this weakness, you can add a histogram of each margin to the plot. We demonstrate using the `SF-36 MCS` and `PCS` subscales in the HELP data set.

**SAS**

SAS provides code to perform this using `proc template` and `proc sgrender`. These procedures are not intended for casual or typical SAS users. Its syntax is, to our eyes, awkward. This is roughly analogous to R functions that simply call C routines. Nonetheless, it’s possible to adapt code that works. The code linked above was edited to set the transparency to 0 and to change the plotted symbol size to 5 from 11px. These options appear in the `scatterplot` statement about midway through the code.

Once the edited code is submitted, the following lines produce the plot shown above.

proc sgrender data="C:\book\help.sas7bdat" template=scatterhist;

dynamic YVAR="mcs" XVAR="pcs"

TITLE="MCS-PCS Relationship";

run;

**R**

For R, we adapted some code found in an old R-help post to generate the following function. The `mtext()` function puts text in the margins and is used here to label the axes. The `at` option in that function centers the label within the scatterplot data using some algebra.

scatterhist = function(x, y, xlab="", ylab=""){

zones=matrix(c(2,0,1,3), ncol=2, byrow=TRUE)

layout(zones, widths=c(4/5,1/5), heights=c(1/5,4/5))

xhist = hist(x, plot=FALSE)

yhist = hist(y, plot=FALSE)

top = max(c(xhist$counts, yhist$counts))

par(mar=c(3,3,1,1))

plot(x,y)

par(mar=c(0,3,1,1))

barplot(xhist$counts, axes=FALSE, ylim=c(0, top), space=0)

par(mar=c(3,0,1,1))

barplot(yhist$counts, axes=FALSE, xlim=c(0, top), space=0, horiz=TRUE)

par(oma=c(3,3,0,0))

mtext(xlab, side=1, line=1, outer=TRUE, adj=0,

at=.8 * (mean(x) - min(x))/(max(x)-min(x)))

mtext(ylab, side=2, line=1, outer=TRUE, adj=0,

at=(.8 * (mean(y) - min(y))/(max(y) - min(y))))

}

The results of the following code are shown below.

ds = read.csv("http://www.math.smith.edu/r/data/help.csv")

with(ds, scatterhist(mcs, pcs, xlab="MCS", ylab="PCS"))

**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 on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...