[R] How to convert array dim names to expressions for plotting?
Hofert Jan Marius
marius.hofert at math.ethz.ch
Thu Oct 4 16:27:38 CEST 2012
Hi,
I would like to use the dim names of an array as labels in a plot. In the case of character labels, this is no problem. However, if I would like to pass expressions through array dim names, they only appear as character labels in the plot and are not evaluated as expressions (see the minimal example below). How can this be done?
Cheers,
Marius
require(grid)
tau <- c(0.25, 0.75)
alpha <- c(0.95, 0.99, 0.999)
a <- array(1:6, dim=c(2,3), dimnames=list(tau=tau, alpha=alpha))
taulabs <- as.expression(lapply(tau, function(t) bquote(tau==.(t))))
dimnames(a)$tau <- taulabs # does not create expressions
dimnames(a) # => no expressions
dimnames(a)$tau[[1]] <- taulabs[1]
dimnames(a)$tau[[2]] <- taulabs[2]
dimnames(a) # => now characters
gl <- grid.layout(1, 2, default.units="inch", widths=c(2,2), heights=c(2,2))
## grid.show.layout(gl)
pushViewport(viewport(layout=gl))
pushViewport(viewport(layout.pos.row=1, layout.pos.col=1))
grid.rect(gp=gpar(col=NA, fill="gray80"))
grid.text(dimnames(a)[["tau"]][1], x=0.5, y=0.5)
upViewport()
pushViewport(viewport(layout.pos.row=1, layout.pos.col=2))
grid.rect(gp=gpar(col=NA, fill="gray50"))
grid.text(dimnames(a)[["tau"]][2], x=0.5, y=0.5)
upViewport()
More information about the R-help
mailing list