Java Mailing List Archive

http://www.r-help.com/

Home » R Help for Statistical Computing »

[R] lattice: plot alignment fails

Hofert Marius

2010-08-24

Replies: Find Java Web Hosting

Author LoginPost Reply
Dear expeRts,

I would like to have four plots appearing in one figure. The minimal example shows this. However, the four figures are not properly aligned. Why? If I comment out the scales=... arguments, then it works, but I would like to use this... :-)

Cheers,

Marius



library(lattice)
set.seed(1)

## plot: xyplot, histogram, qqplot, acf
res.plots <- function(x){
 
  x.max <- max(abs(min(x)),abs(max(x)))

  ## xyplot
  x0 <- 1
  x1 <- length(x)
  y0 <- -x.max
  y1 <- x.max
  xy <- xyplot(x~(1:length(x)),xlim=c(x0,x1),ylim=c(y0,y1),
          aspect=1,scales=list(relation="free",alternating=c(1,1),tck=c(1,0)))
 
  ## histogram
  x0 <- -x.max
  x1 <- x.max
  h <- histogram(x,xlim=c(x0,x1),aspect=1,type="density",
            scales=list(relation="free",alternating=c(1,1),tck=c(1,0)))

  ## qqplot
  y0 <- -x.max
  y1 <- x.max
  qq <- qqmath(x,ylim=c(y0,y1),aspect=1,scales=list(tck=c(1,0),y=list(rot=90)),
          panel=function(...){
             panel.qqmath(...)
             panel.qqmathline(x)
          })

  ## ACF
  myacf <- acf(x,plot=F,lag.max=24)
  x0=0
  x1=24
  y0=-1
  y1=1
  ACF <- xyplot(0~0,xlim=c(x0,x1),ylim=c(y0,y1),aspect=1,  
           scales=list(tck=c(1,0),y=list(rot=90)),
           panel=function(...){
              for(i in 1:24){
                panel.segments(i,0,i,myacf$acf[i])
              }
              panel.abline(h=0)
           })
 
  ## plot function
  plot.list <- list(xy,qq,h,ACF)
  counter <- 1
  trellis.device(pdf,paper="special",width=6,height=6,file="myplot.pdf")
  for(i in 1:2){
    for(j in 1:2){        
       if(i==2 && j==2) print(plot.list[[counter]], split=c(j,i,2,2))
       else print(plot.list[[counter]], split=c(j,i,2,2), more=TRUE)
       counter <- counter + 1
    }
  }
  dev.off()

}


x <- rexp(1000)
res.plots(x)

______________________________________________
R-help@(protected)
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.
©2008 r-help.com - Jax Systems, LLC, U.S.A.