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

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

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

KarstenW
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
> x=rnorm(50)
> y=rnorm(50)
> scatterhist(x,y,xlab="x", ylab="y")

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.
Reply | Threaded
Open this post in threaded view
|

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

KarstenW
Hello,
sometimes it helps if I formulate my problem. I tried the following after I posted my previous post.

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)
 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)
 mtext(ylab, side=2, line=1, outer=TRUE, adj=0)
 #par(oldpar) # reset to default
}

This works quite nice in the R Gui (the labels appear, but it is not easy for me to center them). However, when I use the function in a Sweave context, i.e.

\begin{Scode}{fig=true, echo=false, results=hide}
print(scatterhist(x,y, "x", "y"))
\end{Scode}

the labels still do not appear...

Kind regards,
Karsten.

KarstenW wrote
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
> x=rnorm(50)
> y=rnorm(50)
> scatterhist(x,y,xlab="x", ylab="y")

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.