[R] shared axes in multipanel plot

Gabor Grothendieck ggrothendieck at gmail.com
Fri Dec 11 18:18:22 CET 2009


Try this using xyplot.zoo in the zoo package.  We define the baseline
and a panel function.   The panel function just performs the default
action to display the graphs and adds the baseline.   The screens
variable is 1,1,2,2,3,3,4,4.  We create a zoo object from dat and use
screens to name the columns according to their group.  Finally we call
xyplot.zoo passing it screens so that the successive columns go in the
indicated panels and also passing the other items.  See ?xyplot.zoo in
zoo and ?xyplot in lattice.

library(zoo)
library(lattice)

baseline <- 1:nrow(dat)/nrow(dat)
pnl <- function(x, ...) {
	panel.plot.default(x, ...)
	panel.lines(x, baseline, lwd = 2, col = grey(0.5))
}
nc <- ncol(dat)
screens <- rep(1:(nc/2), each = 2)
z <- zoo(dat)
colnames(z) <- paste("Group", screens)
xyplot(z, screens = screens , layout = c(2, 2), col = "black", lty =
2, scales = list(y = list(relation = "same")), panel = pnl)


On Fri, Dec 11, 2009 at 10:02 AM, Jennifer Young
<Jennifer.Young at math.mcmaster.ca> wrote:
> Hello
>
> I've created a function to make a plot with multiple pannels from columns
> of data that are created in a previous function.  In the example below the
> number of columns is 8, giving 4 pannels, but in general it takes data
> with any number of columns and figures out a nice layout.
>
> The panels all have the same axes, and so I wonder what functions are
> avialable to create axes only on the left and bottom of the whole plot
> rather than each pannel.
> I'd really like a generic way to do this for any number of plots, but was
> even having trouble figuring out how to do it manually for this example;
> How are pannels referred to, in a layout context?
> That is, how do I say,
>
> if(current.pannel==4) {do stuff}
>
> Here's a simple version of the code.
>
> baseline <- (1:20)/20    #example data
> dat1 <- matrix(baseline,20,8)
> dat <- dat1+matrix(rnorm(20*8)/30, 20,8)
>
> nstrat <- ncol(dat)
> rows <- ceiling(nstrat/4)
> layout(matrix(1:(rows*2), rows, 2, T))
> par(oma=c(4,4,3,1))
> par(mar=c(1,1,0,1))
> for(i in which(1:nstrat%%2!=0)){
>    plot(baseline, type="l", col="grey", lwd=2,
>            xlab="", ylab="", ylim=c(0,1), xaxt='n', yaxt='n')
>    axis(1, labels=F); axis(2, labels=F)
>    points(dat[,i], type="l", lty=2)
>    points(dat[,i+1], type="l", lty=2)
> }
>
>
>
> Thank you muchly
> Jennifer Young
>
> PS: I am a subscriber, but can't for the life of me figure out how to send
> an email while logged in so that the moderators don't have to take the
> time to read it over.  I always get the "please wait while we check it
> over" email.  Likely I'm being dumb.
>
> ______________________________________________
> R-help at r-project.org 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.
>




More information about the R-help mailing list