[R] trellis/lattice: How to plot 4 panels with the same x-scale but different y-scales?

Marius Hofert m_hofert at web.de
Thu May 6 17:52:38 CEST 2010


Dear Deepayan,

Thank you very much for your quick help.

I played a bit around and came up with two methods of plotting
a "matrix of plots" on a single page (see the code below). The first you know
from my earlier postings. For this method I have the following questions:
1) Is it possible to have different x- and y-labels for each of the panels 
(as in the second plot)?
2) Is it possible to rotate the y-axis ticks in each panel by -90 degrees 
(as in the second plot)?
For the second method:
3) Is it possible reduce/control the space between the panels?
4) Is it possible to align the panels according to their "frame"? [the panels of
the variables 2 and 4 are not properly aligned as the labels for the latter need
more space than the ones of the former.]

Many thanks,

Marius


library(lattice)
library(grid)

## panel function
library(lattice)
x=c(1,2,1,2,1,2,1,2)
y=c(1,2,2,1,1,-1,-2000,2000)
z=c(1,1,2,2,3,3,4,4) # panel number
xyplot(y~x|z,type="l",aspect=1,layout=c(2,2),
       scales=list(relation="free",alternating=c(1,1),tck=c(1,0)),
       par.settings = list(layout.widths = list(axis.panel = 2),
       layout.heights = list(axis.panel = 2)),
       panel=function(...){
               panel.xyplot(...,col=1)
       }    
)

## grid idea
myplot <- function(x,y,z,xlab,ylab){
        xyplot(y~x|z,type="l",aspect=1,
                xlab=xlab,ylab=ylab,
                strip = function(...) strip.default(...),
                scales=list(alternating=c(1,1),tck=c(1,0))
                )
}
grid.newpage()
pushViewport(viewport(layout=grid.layout(2,2)))
pushViewport(viewport(layout.pos.row=1,layout.pos.col=1))
    print(myplot(1:2,1:2,"variable 1","x label","y label"),newpage=FALSE)
popViewport(1)
pushViewport(viewport(layout.pos.row=1,layout.pos.col=2))
  print(myplot(1:2,2:1,"variable 2","x label","y label"),newpage=FALSE)
popViewport(1)
pushViewport(viewport(layout.pos.row=2,layout.pos.col=1))
    print(myplot(1:2,c(1,-1),"variable 3","x label","y label"),newpage=FALSE)
popViewport(1)
pushViewport(viewport(layout.pos.row=2,layout.pos.col=2))
  print(myplot(1:2,c(-2000,2000),"variable 4","x label","y label"),newpage=FALSE)
popViewport(1)



On 2010-05-06, at 15:09 , Deepayan Sarkar wrote:

> On Thu, May 6, 2010 at 6:24 PM, Marius Hofert <m_hofert at web.de> wrote:
>> Thanks, Deepayan.
>> 
>> Is it at least possible to increase the space between the panels in the following plot?:
>> 
>> library(lattice)
>> x=rep(c(1,2,3),4) # x values
>> y=1:12 # y values
>> z=rep(1:4,each=3) # panel number
>> xyplot(y~x|z,type="l",aspect=1,layout=c(2,2),
>>       scales=list(relation="free",alternating=c(1,1),tck=c(1,0)),
>>       panel=function(...){
>>               panel.xyplot(...,col=1)
>> 
>>       }
>> )
> 
> Sure, add
> 
> par.settings = list(layout.widths = list(axis.panel = 2))
> 
> (similarly layout.heights for vertical gap).
> 
> -Deepayan
> 
>> 
>> On 2010-05-06, at 13:53 , Deepayan Sarkar wrote:
>> 
>>> On Thu, May 6, 2010 at 12:16 PM, Marius Hofert <m_hofert at web.de> wrote:
>>>> Dear R experts,
>>>> 
>>>> I have four plots I would like to plot attached to each other (in panels).
>>>> All plots have the same x-scale, but different y-scales. The example below shows
>>>> pretty much what I would like to have, except that for the two plots in the
>>>> "second column" (of this 2x2 plot-matrix), I would like to have the y-ticks
>>>> shown on the right side such that the four plot panels are attached to each other/glued
>>>> together. How can I achieve this?
>>> 
>>> There is no easy way.
>>> 
>>> For the first step of a difficult way, see ?axis.default
>>> 
>>> -Deepayan
>>> 
>>> 
>>>> I found a solution for a 2x1 matrix in the R-help archive, but this problem here is different.
>>>> 
>>>> Cheers,
>>>> 
>>>> Marius
>>>> 
>>>> Ps: I guess the "scales=..." should somehow go inside the panel function, so that
>>>> the panel number tells the "alternating"-argument where it has to print the ticks.
>>>> 
>>>> library(lattice)
>>>> x=rep(c(1,2,3),4) # x values
>>>> y=1:12 # y values
>>>> z=rep(1:4,each=3) # panel number
>>>> xyplot(y~x|z,type="l",aspect=1,layout=c(2,2),
>>>>        scales=list(y=list(relation="free"),alternating=c(1,1),tck=c(1,0)),
>>>>        panel=function(...){
>>>>                panel.xyplot(...,col=1)
>>>> 
>>>>        }
>>>> )
>>>> ______________________________________________
>>>> 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