How to make two side-by side trellis plots same size

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

How to make two side-by side trellis plots same size

Dieter Menne
Dear Latticers,

I want to position two trellis plots of different forms
side-by-side. The plot types are slightly different,
aspect=1 required, but panels should look the same in
both plots. Current workaround uses a guessed factor.
Any way to improve this?

Dieter


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
p1 = xyplot(y~x|facA*facB,data=df1,main="Plot1",aspect=1,
  between=list(x=2))
p2 = xyplot(y~z|facA*facB,data=df2,main="Plot2",aspect=1,layout=c(1,2))

wi = 0.61 # this is trial-and-error
print(p1,position=c(0,0,wi,1),more=T)
print(p2,position=c(wi,0,1,1),more=F)

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: How to make two side-by side trellis plots same size

Deepayan Sarkar
On 1/26/06, Dieter Menne <[hidden email]> wrote:

> Dear Latticers,
>
> I want to position two trellis plots of different forms
> side-by-side. The plot types are slightly different,
> aspect=1 required, but panels should look the same in
> both plots. Current workaround uses a guessed factor.
> Any way to improve this?
>
> Dieter
>
>
> library(lattice)
> n1 = 20
> # I cannot rbind df1 and df2, because the x-dimensions are
> # different and must be scaled individually

If that's the only reason, I would suggest rbind-ing them and then use

scales = list(x = "free")

If you want the first two columns to have the same x-limits, you can specify

xlim = list(c(0,25), c(0,25), c(0, 2500))

etc. Otherwise, if you want the panels to have the same physical
dimensions, look at the panel.width and panel.height arguments in
?print.trellis. I can't think of any other way of guaranteeing it.

Deepayan

> 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
> p1 = xyplot(y~x|facA*facB,data=df1,main="Plot1",aspect=1,
>   between=list(x=2))
> p2 = xyplot(y~z|facA*facB,data=df2,main="Plot2",aspect=1,layout=c(1,2))
>
> wi = 0.61 # this is trial-and-error
> print(p1,position=c(0,0,wi,1),more=T)
> print(p2,position=c(wi,0,1,1),more=F)

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: How to make two side-by side trellis plots same size

Dieter Menne
Thanks, Deepayan

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

>
> If that's the only reason, I would suggest rbind-ing them and then use
>
> scales = list(x = "free")

I probably will go for this.
 
> If you want the first two columns to have the same x-limits, you can specify
>
> xlim = list(c(0,25), c(0,25), c(0, 2500))
>
> etc. Otherwise, if you want the panels to have the same physical
> dimensions, look at the panel.width and panel.height arguments in
> ?print.trellis. I can't think of any other way of guaranteeing it.

Documentation said that using panel.width with non-standard aspect (which must
be 1) leads to undefined effects.

Dieter

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: How to make two side-by side trellis plots same size

Deepayan Sarkar
On 1/26/06, Dieter Menne <[hidden email]> wrote:

> Thanks, Deepayan
>
> Deepayan Sarkar <deepayan.sarkar <at> gmail.com> writes:
>
> >
> > If that's the only reason, I would suggest rbind-ing them and then use
> >
> > scales = list(x = "free")
>
> I probably will go for this.
>
> > If you want the first two columns to have the same x-limits, you can
> specify
> >
> > xlim = list(c(0,25), c(0,25), c(0, 2500))
> >
> > etc. Otherwise, if you want the panels to have the same physical
> > dimensions, look at the panel.width and panel.height arguments in
> > ?print.trellis. I can't think of any other way of guaranteeing it.
>
> 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.

Deepayan

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: How to make two side-by side trellis plots same size

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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html