# [R] math symbol + value of a variable in legend.

David Winsemius dwinsemius at comcast.net
Wed Aug 26 00:16:20 CEST 2009

```On Aug 25, 2009, at 5:51 PM, David Winsemius wrote:

>
> On Aug 25, 2009, at 4:30 PM, Kenneth Roy Cabrera Torres wrote:
>
>> Hi R users:
>>
>> I will like to have a legend with math symbols and also with
>> the value of a variable.
>>
>> But I cannot obtain both at the same time (symbol + value of a
>> variable):
>>
>> Here is a reproducible example:
>>
>> m1<-5
>> m2<-12
>
> I think I am violating a fortune but this "worked":
>
> plot(1:5,1:5,type="n")
> legend
> ("topleft",legend=c(eval(substitute( expression(paste(mu,"=",m1)),
> list(m1=m1) )) , eval(substitute( expression(paste(mu,"=",m2)),
> list(m2=m2) ) )), lty=1:2)
>
> And efforts at simplification were at least partly successful:
>
> legend("topleft",legend=c(eval(substitute( expression(mu == m1),
> list(m1=m1) )) ,
>                          eval(substitute( expression(mu == m2),
> list(m2=m2) ) )),
>                  lty=1:2)

And this adds subscripts to the mu's:

plot(1:5,1:5,type="n");
legend("topleft",
legend=c( eval(substitute( expression(mu[i] == m1),
list(i=1, m1=m1) )) ,
eval(substitute( expression(mu[i] == m2),
list(i=2, m2=m2) ))  ),
lty=1:2)

>
>
>> plot(1:5,1:5,type="n")
>> legend
>> ("topleft
>> ",legend
>> =
>> c(paste(expression(mu),"=",m1),expression(paste(mu,"=",m2))),lty=1:2)
>>
>> Thank you for your help.
>>
>> Kenneth
> --

David Winsemius, MD
Heritage Laboratories
West Hartford, CT

```