[R] Using grid graphics (hexbin) in pairs() plot problem

Steffen Neumann (on the road) sneumann at ipb-halle.de
Fri Oct 19 08:00:01 CEST 2007


Hi,

I am trying to create a plot with pairs()
using a gplot.hexbin() for each pair.

For pairs I can provide a custom upperPanel function:

    pairs(iris[1:4], panel=mypanel)

and mypanel() calls plot.hexbin():

    library(hexbin) # Bioconductor
    mypanel <- function(x, ...){
        hb <- hexbin(x)
        plot(hb)
    }

The problem is that gplot.hexbin() is based
on the grid-graphics, and each hexplot
goes over my complete graphics device.

I suspect that I can use the gridBase package
to put the two together, but I am somehow lost.

The gridBase documentation contains a sample
which has a normal ("base") plot with two columns,
and some code to use grid commands to paint rectangles.

      library(grid)
      library(gridBase)
      par(oma=rep(1, 4), mfrow=c(1, 2), xpd=NA)
      plot(1:10)
      vps <- baseViewports()
      pushViewport(vps$inner)
      grid.rect(gp=gpar(lwd=3, col="red"))
      pushViewport(vps$figure)
      grid.rect(gp=gpar(lwd=3, col="green"))
      pushViewport(vps$plot)
      grid.rect(gp=gpar(lwd=3, col="blue"))
      grid.points(1:10, 10:1)

if I continue with a second plot (analogous to what pairs() 
would be doing) things are fine:

      popViewport()
      popViewport()
      popViewport()

      plot(10:100)      # Second plot
      vps <- baseViewports()
      pushViewport(vps$inner)
      pushViewport(vps$figure)
      pushViewport(vps$plot)
      grid.rect(gp=gpar(lwd=3, col="yellow"))
      popViewport()
      popViewport()
      popViewport()

But it doesn't work if I stuff mypanel() with these push/pop's :

      mypanel <- function(x, ...){
        vps <- baseViewports()
        pushViewport(vps$inner)
        pushViewport(vps$figure)
        pushViewport(vps$plot)

        hb <- hexbin(x)
        plot(hb)

        popViewport()
        popViewport()
        popViewport()
     }

I even get a problem with apparently unmatched push/pops:

       Error in grid.Call.graphics("L_unsetviewport", as.integer(n)) :
       Cannot pop the top-level viewport (grid and graphics output
mixed?)

Any clues ?

Thanks for any help in advance,
Yours,
Steffen


-- 
IPB Halle                    AG Massenspektrometrie & Bioinformatik
Dr. Steffen Neumann          http://www.IPB-Halle.DE
Weinberg 3                   http://msbi.bic-gh.de
06120 Halle                  New Phone number !
                             Tel. +49 (0) 345 5582 - 1470
sneumann(at)IPB-Halle.DE     Fax. +49 (0) 345 5582 - 1409



More information about the R-help mailing list