# [R] Legend/Substitute/Plotmath problem

Martin Maechler maechler at stat.math.ethz.ch
Mon Oct 25 09:56:35 CEST 2004

```>>>>> "Johannes" == Johannes Graumann <graumann at caltech.edu>
>>>>>     on Sat, 23 Oct 2004 11:04:25 -0700 writes:
>>>>> "Johannes" == Johannes Graumann <graumann at caltech.edu>
>>>>>     on Sat, 23 Oct 2004 11:04:25 -0700 writes:

Johannes> Hello,

Johannes> I seem unable to construct a legend which contains
Johannes> a substitution as well as math symbols. I'm trying
Johannes> to do the following:

>> strain2 <- "YJG48"

>> legend.txt <- c(
>> 	substitute(
>> 		strain *
>> 		%==% *
>> 		"YJG45, rpn10" *
>> 		%Delta%,
>> 		list(strain=strain2)
>> 	),
>> 	"Verhulst/Logistic",
>> 	"Malthus"
>> )

Johannes>     .....................

Do try to break down a problem into simple things --
particularly when you have problems!

This substitute() call is simply invalid:

> ss <- substitute( strain * %==% * "YJG45, rpn10" * %Delta%, list(strain=strain2) )
Error: syntax error

and the 'syntax error' should give you a clue:
The first argument of substitute must be a syntactically correct
R expression.

Now you try more and more simple things till you 'see it' :

Why should I expect  'A * %==% B'  to be valid syntax?
Both '*' and '%==%' are (diadic) operators: You can't juxtapose
them, as well as you can't write  'A * = B'.
Then, '%Delta%' (like any other '%foo%' !!) is a diadic operator
too and hence can't be juxtaposed to '*'. But I'm pretty sure
you rather mean (greek) 'Delta'.

Hence:
ss <- substitute( strain %==% "YJG45, rpn10" * Delta, list(strain=strain2) )

---

Once you have the expression you can go further;
still step by step :

> c(ss, "Verhulst")
[]
"YJG48" %==% "YJG45, rpn10" * Delta

[]
 "Verhulst"

Hmm, a list; that won't work.
You do need to pass either a "character" vector or an
expression, i.e., an expression of length 3 in our case.
We must build the expression somewhat manually:

> e <- expression(1, "Verhulst", "Malthus")# '1' is a place holder
expression(1, "Verhulst", "Malthus")
> e[] <- ss  ## that's the trick!

> str(e)
expression("YJG48" %==% "YJG45, rpn10" * Delta, "Verhulst", "Malthus")

> plot(1); legend(1,1, leg = e)

---

Maybe something to be added as an example to help(legend) or rather
to help(expression) ?

HTH,
Martin Maechler, ETH Zurich

```