[R] Rotated viewports in Grid

Gustaf Rydevik gustaf.rydevik at gmail.com
Wed Oct 24 15:28:13 CEST 2007

On 10/24/07, Paul Murrell <paul at stat.auckland.ac.nz> wrote:
> 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

Thank you very much for your kind and quick help!

It was quite a bit more difficult than I thought to get R(grid) to
flip graphics output, and
the graph isn't really worth writing it's own function from scratch
(though I might try it in the future).
I think I'll use a barplot as histogram (with horiz=TRUE)  instead-
good enough for now.

best regards,


Gustaf Rydevik, M.Sci.
tel: +46(0)703 051 451
address:Essingetorget 40,112 66 Stockholm, SE

More information about the R-help mailing list