Skip to content
Prev 161273 / 398500 Next

[newbie] scatterplot with marginal histograms (done) and axes labels

Hello,

I am stuck when I want to add axes labels to my scatterplot with histograms.
I guess it must be something  with par(mar=) or so, but could someone give
me a hint?

Here is what I got so far:

# adapted from
http://www.stat.ucl.ac.be/ISdidactique/comment/fichiers/r/scatterhist.rs and
from ?layout
#
# see also: 
#     - http://biom1.univ-lyon1.fr/ADE-4/ade4-html/s.hist.html (a function
called s.hist in package ade4)
#     - http://www.statmethods.net/graphs/scatterplot.html (shows how to do
some nice scatterplots, 
#       e.g. with the function scatterplot in package car)

scatterhist=function(x,y, xlab="", ylab=""){
 #oldpar = par(no.readonly = TRUE) # save default, for resetting...
 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, xlab=xlab, ylab=ylab)
 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(oldpar) # reset to default
}

Now if I do
there are no axes labels.....
I also wonder why R complains if I uncomment the first and last line in the
function ("invalid value
for parameter fig specified").

Any hint appreciated,
Karsten.