[R] lattice: plot alignment fails

Marius Hofert m_hofert at web.de
Wed Sep 1 22:41:32 CEST 2010


Okay, I solved it. After using the same arguments for "scales" (in particular: relation="free"), the plots are perfectly aligned.

Cheers,

Marius

On 2010-08-24, at 20:39 , Marius Hofert wrote:

> 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)
> 



More information about the R-help mailing list