Re: [R] grid.layout?

From: Paul Murrell <p.murrell_at_auckland.ac.nz>
Date: Wed, 05 Mar 2008 08:48:38 +1300

Hi

Patrick Connolly wrote:
> platform x86_64-unknown-linux-gnu
> arch x86_64
> os linux-gnu
> system x86_64, linux-gnu
> status
> major 2
> minor 6.2
> year 2008
> month 02
> day 08
> svn rev 44383
> language R
> version.string R version 2.6.2 (2008-02-08)
>
> The following code does almost everything I want except the red
> rectangle should be aligned flush with square. There's something I
> haven't understood about the grid.layout function. The second time I
> use it, it does what I expect, but there's something wrong with the
> first one.

The crucial misunderstanding is that you push a viewport with a layout THEN you push other viewports that occupy parts of that layout.

> require(grid)
> N <- 5
> variates <- LETTERS[1:N]
> grid.newpage()
> plot.square <- 2 * length(variates)# cm
> text.wid <- 3 # cm
> vp <- grid.layout(nrow = 1, ncol = 2,
> widths = c(text.wid, plot.square),
> heights = plot.square, default.units = "cm")

This is wrong ...

> pushViewport(viewport(layout = vp, layout.pos.row = 1,
> layout.pos.col = 2))

... should be ...

 pushViewport(viewport(layout = vp)) # this viewport defines the layout

> ##
> vpp <- grid.layout(nrow = length(variates),
> ncol = length(variates),
> widths = 2, heights = 2,
> default.units = "cm")

Here is where you specify a position within the previous layout, NOT ...

> pushViewport(viewport(layout = vpp))

... BUT ...   pushViewport(viewport(layout = vpp,

                        layout.pos.row = 1,   # this viewport is placed
                        layout.pos.col = 2))  # within the above layout



> for(i in seq(variates)){
> for(j in seq(variates)){

Again, this guy should just specify a position within the parent layout ...

> pushViewport(viewport(layout = vpp, layout.pos.col = j,
> layout.pos.row = i))

... no need for the layout here ...

      pushViewport(viewport(layout.pos.col = j,  # No need for layout
                            layout.pos.row = i))



> grid.rect(gp=gpar(lty='dashed'))
> popViewport(1)
> }
> }
> popViewport(1)
> pushViewport(viewport(layout = vp, layout.pos.col = 1,
> layout.pos.row = 1))

BTW, did you mean lwd ... ?

> grid.rect(gp=gpar(col = "red", width = 3))

  grid.rect(gp=gpar(col = "red", lwd = 3)) # 'lwd' not 'width'

Paul

> ylab.pos <- seq(variates)/length(variates)
> ylab.pos <- ylab.pos - ylab.pos[1]/2
> for(i in seq(variates)){
> grid.text(rev(variates)[i], x = 0.9, gp=gpar(cex = .8),
> y = ylab.pos[i], default.units = "npc",
> just="right")
> }
>
>
> Suggestions about the more intelligent use of pushViewport and
> popViewport would also be appreciated.
>
> Thank you.
>

-- 
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/

______________________________________________
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.
Received on Tue 04 Mar 2008 - 19:59:36 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 04 Mar 2008 - 20:30:19 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive