From: Alex Brown <alex_at_transitive.com>

Date: Thu 19 Oct 2006 - 09:27:09 GMT

# some data

R-help@stat.math.ethz.ch mailing list

https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Thu Oct 19 19:37:06 2006

Date: Thu 19 Oct 2006 - 09:27:09 GMT

Hi everyone,

I'm trying to plot density curves on the axes of a plot, in a similar way to the 'rug' function.

I have had a look at a few approaches and libraries, including:

- layout http://addictedtor.free.fr/graphiques/RGraphGallery.php? graph=78
- fancygraph http://addictedtor.free.fr/graphiques/RGraphGallery.php? graph=81
- ggplot - gave up since it needs R 2.3.0 and today I need to run on less than that
- grid - probably powerful, good approach, but too much effort for a midnight hack
- rug - since it does something similar
- lattice - it has multiple graphs, but I think they may need to be homogeneous
- plot + lines
- plot + plot

And here is the solution I am using at the moment, based upon layout.

Can anyone suggest a better way to do this?

-Alex Brown

# some data

x1 = rnorm(100)

y1 = rnorm(100)

# setup the layout

opar = par(no.readonly=TRUE) omar = par("mar") l = layout(matrix(c(2,0,1,3),2),c(1,4),c(4,1))par(cex=1,bty="n")

# first plot : main plot

par(mar=omar * c(0,0,1,1)) # just top and right margins
plot(x1, y1, axes=FALSE, main="Density Margins",
xlab="", ylab=""

)

# vars

usepoly <- TRUE

plott = ifelse(usepoly,"n","l")

par(col="blue")

# second plot : y axis density

par(mar=omar * c(0,1,1,0))

par(xpd=NA)

yd = density(y1,from=min(y1),to=max(y1))
ydd= data.frame(x=-yd$y, y=yd$x)

plot(ydd, xaxt="n",xlim=c(0,min(ydd$x)),type= plott,bty="n",ylab="y
(density)",xlab="")

if(usepoly) {

ydd=rbind(ydd, c(0,max(ydd$y)), c(0,min(ydd$y)))
polygon(ydd,col="grey",border=NA)

}

# third plot : x axis density

par(mar=omar * c(1,0,0,1))

par(xpd=NA)

xd = density(x1,from=min(y1),to=max(y1))
xdd= data.frame(x=xd$x, y=-xd$y)

plot(xdd, yaxt="n",ylim=c(0,min(xdd$y)),type= plott,xlab="x
(density)",ylab="")

if(usepoly) {

xdd=rbind(xdd,c(max(xdd$x),0), c(min(xdd$x),0))
polygon(xdd,col="grey",border=NA)

}

par(opar)

[[alternative HTML version deleted]]

R-help@stat.math.ethz.ch mailing list

https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Thu Oct 19 19:37:06 2006

Archive maintained by Robert King, hosted by
the discipline of
statistics at the
University of Newcastle,
Australia.

Archive generated by hypermail 2.1.8, at Thu 19 Oct 2006 - 10:30:12 GMT.

*
Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help.
Please read the posting
guide before posting to the list.
*