Skip to content

plotmath: paste string and expression [from a vector of expressions]

8 messages · Uwe Ligges, Dennis Murphy, David Winsemius +2 more

#
Dear all,

I have a vector of expressions and would like to "paste" some string to it before using it in a plot:

vars <- vector("expression", 2)
vars[1] <- expression(alpha)
vars[2] <- expression(beta)
plot(0, 0, main=substitute(bold("Foo" ~~ VAR), list(VAR=vars[2]) ))

Although I tried hard, I just can't figure out how to solve this. The title should be "Foo <theta>", where <theta> is the greek letter. I tried some constructions with bquote but that wasn't successful... I also looked in the mailing list but couldn't find anything helpful [I am sure I overlooked something].

Cheers,

Marius
#
On 02.06.2011 20:43, Marius Hofert wrote:
plot(0, 0, main=expression("Foo" ~~ theta))

Uwe Ligges
#
Dear Uwe,

thanks for your help. Actually, I first thought about writing your solution in the email in order to make clear that it is not the solution I'm looking for :-) My goal is to work with the vector "vars" of expressions. The example is only a minimal example and for that your solution is perfectly fine, but my original problem is more complicated and there it makes sense to work with a vector of expressions. Do you know a solution to that? I tried many things... the obvious plot(0, 0, main=substitute(bold("Foo" ~~ VAR), list(VAR=vars[2]) )) did not work...

Cheers,

Marius
On 2011-06-02, at 22:14 , Uwe Ligges wrote:

            
#
Hi:

This seems to work:

vars2 <- c(quote(alpha), quote(beta))   # returns a list of mode call
plot(0, 0, main = bquote(bold('Foo '~.(vars2[[2]]))))

Expressions are only evaluated once, which means that inner
expressions are not evaluated. You need a call object rather than an
expression inside of bquote().

HTH,
Dennis
On Thu, Jun 2, 2011 at 11:43 AM, Marius Hofert <m_hofert at web.de> wrote:
#
On Jun 2, 2011, at 4:19 PM, Marius Hofert wrote:

            
> vars <- vector("expression", 2)
 > vars[[1]] <- quote(alpha)
 > vars[[2]] <- quote(beta)
 > plot(0, 0, main= bquote( paste("Foo ", .(vars[[2]] )) ) )
#
Dear Dennis, Dear Uwe, Dear David,

many thanks for helping. Dennis and David, your solutions seemed perfectly fine, but when I applied it to my original problem, it did not show a title. Below is a (longer) minimal example (the first part is from the help page of bbmle). Is this a bug in bbmle? Hmmm...

library(bbmle)

x <- 0:10
y <- c(26, 17, 13, 12, 20, 5, 9, 8, 5, 4, 8)
d <- data.frame(x,y)

## in general it is best practice to use the `data' argument,
##  but variables can also be drawn from the global environment
LL <- function(ymax=15, xhalf=6)
    -sum(stats::dpois(y, lambda=ymax/(1+x/xhalf), log=TRUE))

## uses default parameters of LL
(fit <- mle2(LL))
ml <- mle2(LL, fixed=list(xhalf=6))
mlp <- profile(ml)
 
vars <- c(quote(theta), quote(beta))
plot(mlp, main=bquote(bold("Foo"~.(vars[[2]]))))

Cheers,

Marius
On 2011-06-02, at 22:23 , Dennis Murphy wrote:

            
#
Use VAR=vars[[2]] (double brackets) there.  You can see the
difference if you look at the output of your call to substitute.
[[ gives you an element of the expression and [ gives you
an expression containing an element:

  > substitute(bold("Foo" ~~ VAR), list(VAR=vars[[2]]) )
  bold("Foo" ~ ~beta)
  > substitute(bold("Foo" ~~ VAR), list(VAR=vars[2]) )
  bold("Foo" ~ ~expression(beta))

The same holds for the bquote() solution that David W. suggested.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
#
On Jun 2, 2011, at 5:07 PM, Marius Hofert wrote:

            
I do not have experience with that package but my guess is that the  
plot.mle2 (or whatever its name might be... )  function does something  
different. class(mlp) returns "profile.mle2". Looking at the  
documentation you see that it is using S4 methods and using Methods()  
one sees that there is a 'plot' method for 'profile.mle2' objects.  
That's about as far as I go.

<whine-mode on>
  Navigating S4 methods is an arcane art into which I have not  
initiated myself. Unlike S3 methods where you just type the function  
name and it's easy to figure out what the names will be, there are  
several levels of specification and the help pages for "Methods' ....  
are not, ... "helpful" to one who approaches it without more  
experience than I have. The help page regarding acceptable expressions  
to pass to "main" arguments is not particularly helpful, either. I  
would advise asking the package author or maintainer.

Before anyone berates me for insufficient effort at self-learning, I  
swear that my copy of Chambers (2008) arrived this week. I do think it  
would be "helpful" to have a worked example near the top of examples  
on help(Methods) that shows HOW_TO get at the functional machinery for  
a plot method when one knows the class of an object.

After some further experimentation I have a theory that the 'main'  
argument will not accept a language object but that one can coerce to  
an expression object and succeed. (Didn't I go through this once  
before? Maybe this was what Hadley was trying to teach me about a  
month ago.)
<whine-mode off>

# -----Answer

plot(mlp, main=as.expression(bquote(bold(Foo~.(vars[[2]])  )) ) )

# --- back your regularly scheduled programming -------