[R] How to make two side-by side trellis plots same size

Dieter Menne dieter.menne at menne-biomed.de
Thu Jan 26 18:34:13 CET 2006


Deepayan Sarkar <deepayan.sarkar <at> gmail.com> writes:

> > Documentation said that using panel.width with non-standard aspect (which
> > must
> > be 1) leads to undefined effects.
> 
> Yes, because panel.width and panel.height together define the aspect,
> and will override whatever the 'aspect' argument says. It's fine to
> have aspect="fill" (the default), or even 1 probably, and then control
> the aspect using panel.width and panel.height.

Thanks, Deepayan,
I got it now: I have to set BOTH width and height, and aspect set before will 
effectively get overridden. I tried before to set panel.width only, assuming 
that panel.height would follow according to aspect. Here the working solution.

library(lattice)
n1 = 20
# I cannot rbind df1 and df2, because the x-dimensions are
# different and must be scaled individually
df1 = data.frame(y=rnorm(4*n1),x=rep(1:n1,4),
   facA=rep(c("A","B"),each=2*n1),facB=rep(c("C","D"),each=n1))

# I add a dummy facB here, to make sure panels have same structure
df2 = data.frame(y=rnorm(2*n1),z=rep(100*(1:n1),2),
   facA=rep(c("A","B"),each=n1),facB="C")

# Note: aspect = 1 is required later, but settings aspect here
# does not make sense when we work with panel.height later
p1 = xyplot(y~x|facA*facB,data=df1,main="Plot1",
  between=list(x=2))
p2 = xyplot(y~z|facA*facB,data=df2,main="Plot2",layout=c(1,2))

pw = list(x=3,units="cm")
print(p1,position=c(0,0,2/3,1),more=T,panel.width=pw,panel.height=pw)
print(p2,position=c(2/3,0,1,1),more=F,panel.width=pw,panel.height=pw)




More information about the R-help mailing list