Hofert Jan Marius
2012-Oct-04 14:27 UTC
[R] How to convert array dim names to expressions for plotting?
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()
Bert Gunter
2012-Oct-04 14:42 UTC
[R] How to convert array dim names to expressions for plotting?
It is not clear to me what you mean by "expressions" in this context. Dimension names but be character strings -- see ?dimnames, which says: "The dimnames of a matrix or array can be NULL or a list of the same length as dim(x). If a list, its components are either NULL or a character vector with positive length of the appropriate dimension of x. " If somehow you expect plotmath type drawn objects or R expression objects, then you are confused. If I am just misunderstanding what you want, then I am confused. -- Bert On Thu, Oct 4, 2012 at 7:27 AM, Hofert Jan Marius <marius.hofert at math.ethz.ch> wrote:> 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() > > ______________________________________________ > 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.-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
Hofert Jan Marius
2012-Oct-04 15:29 UTC
[R] How to convert array dim names to expressions for plotting?
Dear Bert,
thanks, I was confused (of course). So the right question would have been: How
to convert strings to expressions (plotmath expressions). But that I could
easily find, thanks.
Cheers,
Marius
Here is the working example (showing plotmath and just-character labels):
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))
dimnames(a)$tau[1] <- "tau==0.25" ## dimnames(a)$tau <-
paste0("tau==", tau)
dimnames(a)
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(parse(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(parse(text=dimnames(a)[["tau"]][2]), x=0.5, y=0.5)
upViewport()
________________________________________
From: Bert Gunter [gunter.berton at gene.com]
Sent: Thursday, October 04, 2012 4:42 PM
To: Hofert Jan Marius
Cc: mailman, r-help
Subject: Re: [R] How to convert array dim names to expressions for plotting?
It is not clear to me what you mean by "expressions" in this context.
Dimension names but be character strings -- see ?dimnames, which says:
"The dimnames of a matrix or array can be NULL or a list of the same
length as dim(x). If a list, its components are either NULL or a
character vector with positive length of the appropriate dimension of
x. "
If somehow you expect plotmath type drawn objects or R expression
objects, then you are confused. If I am just misunderstanding what you
want, then I am confused.
-- Bert
On Thu, Oct 4, 2012 at 7:27 AM, Hofert Jan Marius
<marius.hofert at math.ethz.ch> wrote:> 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()
>
> ______________________________________________
> 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.
--
Bert Gunter
Genentech Nonclinical Biostatistics
Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm