[R] Rotated viewports in Grid

Paul Murrell paul at stat.auckland.ac.nz
Wed Oct 24 14:42:55 CEST 2007


Hi


Gustaf Rydevik wrote:
> Hi all,
> 
> I'm trying to generate a plot containing a scatterplot, with marginal
> densityplots for x and y.
> However, when I try to generate a vertical densityplot, I get the
> message "warning: can't clip to rotated viewport", and nothing shows
> up. I'm probably misunderstanding how viewports are meant to be used,
> so if someone could give me a hint, I'd be most grateful.
> my system is R 2.5.0 (stuck, and can't upgrade for now) and windoms xp.


The warning says it all unfortunately;  R only clips to rectangular 
regions, so if you take a rectangle (viewport) and rotate it, and try to 
clip to it, R can't cope (it's lattice that is trying to clip to some of 
its viewports BTW).

Of course, R could be slightly smarter and check whether the rotated 
rectangle is still a rectangle (aligned with the x- and y-axes).  I will 
look at a fix for this in the development version of R.

Unfortunately, there are still some problems with your example, because ...

pushViewport(viewport(layout.pos.row=2,layout.pos.col=2,angle=270))

... will not do what you want.  The layout.pos.row/col mean that angle 
(along with x, y, width, and height) is ignored.  Instead you could try ...

pushViewport(viewport(layout.pos.row=2,layout.pos.col=2))
pushViewport(viewport(width=convertUnit(unit(1, "npc"),
                         "npc", "y", "dimension", "x"),
                       height=convertUnit(unit(1, "npc"),
                         "npc", "x", "dimension", "y"),
                         angle=270))

... (all that conversion is necessary because otherwise the rotated 
viewport is NOT the size you probably want).

However, even then the result is not very nice because you have a lot of 
work still to do to make the x- and y-scales on the three plots line up 
(not just a matter of controlling the scales on each plot, but also a 
matter of getting the plot margins the same size).

If it was me, I would be starting to think about writing something from 
scratch in grid at this point ...

Paul


> many thanks in advance,
> 
> Gustaf
> 
> example:
> --------------------------
> library(lattice)
> library(grid)
> x<-rnorm(20)
> y<-rnorm(20)
> graph.layout<-grid.layout(nrow=2,ncol=2,widths=unit.c(unit(2,"null"),unit(1,"null")),heights=unit.c(unit(1,"null"),unit(2,"null")))
> graph.view<-viewport(layout=graph.layout,name="root")
> grid.newpage()
> pushViewport(gap.view)
> pushViewport(viewport(layout.pos.row=2,layout.pos.col=1))
> print(xyplot(x~y),newpage=FALSE)
> popViewport()
> pushViewport(viewport(layout.pos.row=1,layout.pos.col=1))
> print(densityplot(x),newpage=FALSE)
> popViewport()
> pushViewport(viewport(layout.pos.row=2,layout.pos.col=2,angle=270))
> print(densityplot(y),newpage=FALSE) ### I Get the warning message here.
> ----------------------------------
> 


-- 
Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
Auckland
New Zealand
64 9 3737599 x85392
paul at stat.auckland.ac.nz
http://www.stat.auckland.ac.nz/~paul/



More information about the R-help mailing list