[R] grid.table + splom: how to nicely align panel entries

Marius Hofert m_hofert at web.de
Wed Apr 20 11:16:26 CEST 2011


Dear expeRts,

is there a way to get the entries in each panel correctly aligned according to the 
equality signs?

Here is the "wish-list":
(1) the equality signs in each panel should be vertically aligned  
(2) the numbers should be aligned on the decimal point

One could adjust the phantom()-arguments by hand to achieve (1), but is there a 
simpler solution? For (2) I have no idea.

Cheers,

Marius


library(lattice) 
library(grid)
library(gridExtra)

## splom with customized lower.panel
## x: data
## arr: array of containing expressions which are plotted in a grid table in the 
##      lower panel (i,j)]
splom2 <- function(x, arr){
    ## function for creating table 
    table.fun <- function(vec){ # vector containing lines for table for *one* panel
        grid.table(matrix(vec, ncol=2, byrow=TRUE),
                   parse=TRUE, # parse labels as expressions
                   theme=theme.list(
                   gpar.corefill=gpar(fill=NA, col=NA), # make bg transparent
                   core.just="left", padding.h=unit(0,"mm")) # justification of labels
                   ) 
    }
    ## splom
    splom(x, varname.cex=1.4,
          superpanel=function(z, ...){
              panel.pairs(z, upper.panel=panel.splom, lower.panel=function(i,j){
                  table.fun(arr[i,j,])
              }, ...)
          })
}

## create data and array of expressions
d <- 4
x <- matrix(runif(d*1000), ncol=d) # data to be plotted with splom
arr <- array(list(rep(NA, 3*2)), dim=c(d,d,3*2), dimnames=c("i","j","val")) # array containing the table entries per panel
f <- function(i,j) (i+j)*10+0.1 # dummy function
for(i in 1:d){
    for(j in 1:d){
	arr[i,j,] <- c("alpha==phantom()", round(pi,4),
                       "italic(bbb)==phantom()", round(pi,6),
                       "gamma==phantom()", f(i,j))
    }
}

## plot
splom2(x, arr)



More information about the R-help mailing list