[R] Rotated viewports in Grid
Deepayan Sarkar
deepayan.sarkar at gmail.com
Thu Oct 25 10:23:11 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 ...
I used essentially the problem for a guest lecture once, and the
solution is outlined in
http://www.stat.wisc.edu/~deepayan/771/rgraphics.pdf
YMMV.
-Deepayan
More information about the R-help
mailing list