splom, plotmath: how to add three lines of information with alignment?
Dear Baptiste,
thank you for your help. I tried to built in your suggestions into the splom.
Below is the result. I decided to create the plotmath-expressions outside the
function splom2, this will allow me later to horizontally shift (via phantom,
for example) the table entries so that they will be vertically aligned according
to the "=" signs. Although an array is allowed to contain expressions, I could
not manage to create it properly. Do you know a solution?
Cheers,
Marius
library(lattice)
library(grid)
library(gridExtra)
## splom with customized lower.panel
## x: data
## expr.arr: array of expressions [(i,j,) entry contains expressions which are
## plotted in a grid table in the lower panel (i,j)]
splom2 <- function(x, expr.arr){
## function for creating table
table.fun <- function(vec){ # single values for one panel
grid.table(vec,
parse=TRUE, # parse labels as expressions
theme=theme.list(
gpar.corefill=gpar(fill=NA, col=NA), # make bg transparent
core.just="left") # 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(expr.arr[i,j,])
}, ...)
})
}
## create data and array of expressions
d <- 4
x <- matrix(runif(d*1000), ncol=d)
expr.arr <- array(, dim=c(d,d,3), dimnames=c("i","j","val")) # d x d x 3 elements
for(i in 1:d){
for(j in 1:d){
# expr.arr[i,j,] <- expression(italic(a)==0, italic(bbb)==0, italic(c)==0) # does no work
expr.arr[i,j,] <- c(bquote(italic(a)==.(0)), bquote(italic(bbb)==.(0)), bquote(italic(c)==.(0))) # same here
}
}
## plot
splom2(x, expr.arr)
On 2011-04-20, at 01:33 , baptiste auguie wrote:
Hi,
You may want to wait advice from someone who actually understands (the
labyrinth that is) lattice's help for splom, but the following might
be a start. I didn't understand what values you actually wanted
displayed in the lower triangle panels, so I made up some random ones
in a 3x3 matrix of 3-vectors.
library(lattice)
library(gridExtra)
info <- function(x){
grid.table(c(bquote(italic(a)==.(x[1])),
bquote(italic(b)==.(x[2])),
bquote(italic(c)==.(x[3]))),
core.just="left",
parse=TRUE)
}
U <- matrix(runif(3000), ncol=3)
splom(U,
superpanel=function(z, ...){
## dummy 3x3 matrix of 3 values to diplay
values <- replicate(9, round(rnorm(3), 3), simplify=FALSE)
dummy <- matrix(values, ncol=3, byrow=F)
panel.pairs(z, upper.panel=panel.splom,
lower.panel=function(i, j, ...){
print(paste(i,j)) # current panel indices
info(dummy[i,j] [[1]]) # access the list elements
}, ...)
})
HTH,
baptiste
On 20 April 2011 10:17, Marius Hofert <m_hofert at web.de> wrote:
Dear Baptiste,
there is one tricky part left: how can I create a matrix with the grid.table()
objects as output? Is this possible? If not, maybe one can try to work with
panel.splom (which can address single panels and thus call info() for each
row-column index pair (i,j)), but I'm not sure if this will work.
Cheers,
Marius
library(lattice)
library(gridExtra)
splom2 <- function(x, a, b, c){
## function for the additional information
info <- function(a., b., c.){ # single values for one panel
grid.table(c(bquote(italic(a)==.(a.)),
bquote(italic(b)==.(b.)),
bquote(italic(c)==.(c.))
),
parse=TRUE, # parse labels as expressions
theme=theme.list(
gpar.corefill=gpar(fill=NA, col=NA), # make bg transparent
core.just="right") # justification of labels
)
}
labs <- matrix(, nrow=ncol(x), ncol=ncol(x)) # should be a matrix of grid.table() objects
for(i in 1:ncol(x)) for(j in 1:ncol(x)) labs[i,j] <- info(a.=a[i,j], b.=b[i,j], c.=c[i,j])
## splom
splom(x, superpanel=function(z,...){
df=data.frame(rows=as.vector(row(a)),
columns=as.vector(col(a)), labs=as.vector(labs))
df=subset(df,columns<rows) # subset for lower left triangle
with(df,{
panel.text(x=rows, y=columns, labels=labs)
})
panel.pairs(z, upper.panel=panel.splom, lower.panel=function(...){}, ...)
})
}
## generate data
U <- matrix(runif(3000), ncol=3)
## build information
a <- cor(U, method="kendall")
b <- diag(ncol=3, nrow=3)
c <- diag(ncol=3, nrow=3)
## plot with information
splom2(U, a, b, c)