Re: [R] Graphic legend with mathematical symbol, numeric variable and character variable

From: Peter Ehlers <ehlers_at_ucalgary.ca>
Date: Wed, 21 Mar 2012 12:27:17 -0700

Hi David,

It's best to keep the mailing list in the loop so I'm cc-ing to R-help.

I've rethought your problem and propose a different solution.

The main problem is to create a vector of expressions to be used in the legend.
(see https://stat.ethz.ch/pipermail/r-help/2011-February/270106.html)

   tau.vals <- c(51,88,200)
   types <- c("B&K UA 1404","01dB BAP 21","RION WS3")

   leg <- vector("expression", 3)
   for(i in 1:3) leg[i] <-
   substitute(expression(

                           TYPE ~ group("(", tau == tauval, ")")
                        ),
              list(TYPE=types[i], tauval=tau.vals[i])
             )[2]

   plot(0);legend(x="topright",legend=leg)

The idea is to create an expression vector and then fill its components appropriately. We need the second component of the substitute call.

Peter Ehlers

On 2012-03-21 06:49, "ECOTIRE David (Responsable d'activit) - CETE Est/LRPC de Strasbourg/6 Acoustique" wrote:
> Well, I have some problems with this solution:
>
> Here is my (real) code:
>
> tau<-c(51,88,200)
> types<-c("B&K UA 1404","01dB BAP 21","RION WS3")
> types<-gsub(pattern=" ", replacement="~",x=types)
> leg<-parse(text=paste(types,"(","tau==",tau,"min)", sep='~'))
> plot(0)
> legend(x="topright",legend=leg)
>
>
> I've got an error after the 'parse' command :
>
> Erreur dans parse(text = paste(types, "(", "tau==", tau, "min)", sep =
> "~")) :
> <text>:2:3: symbole inattendu(e)
> 1: B&K~UA~1404~(~tau==~51~min)
> 2: 01dB
> ^
>
> But no error if "01dB" is replaced by "dB01" (which is not what I want
> of course). It's like if it was impossible to have a letter that follows
> number.
>
> Any idea ?
>
> Thank you
>
> David
>
> Le 21/03/2012 12:57,> Peter Ehlers (par Internet) a crit :
>> On 2012-03-20 06:09, "ECOTIRE David (Responsable d'activit) - CETE
>> Est/LRPC de Strasbourg/6 Acoustique" wrote:
>>> Hi,
>>>
>>> I'd like to make a legend with a mix of mathematical symbol (tau),
>>> numeric variable and character variables.I have tried :
>>>
>>>
>>> types<-c("Type 1","Type 2","Type 2")
>>> tau<-c(1,3,2)
>>> legend(x="topright",legend=paste(types,"tau=",expression(tau)))
>>>
>>>
>>>
>>> but it doesn't work: the 'tau' symbol is not written in its 'symbol
>>> style' but as 'tau'
>>>
>>> Any (good) idea ?
>>> Thank you in advance !
>>>
>>> David
>>
>> Does this do what you want:
>>
>> types<- paste('Type', 1:3, sep='~')
>> mytau<- 4:6
>> leg<- parse(text=paste(types, "~~tau==", mytau, sep='~'))
>> plot(0)
>> legend('topright', legend=leg)
>>
>> The '~' symbols generate spaces; use more if you want more spacing.
>>
>>
>> Peter Ehlers
>>
>>



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. Received on Wed 21 Mar 2012 - 19:38:03 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 21 Mar 2012 - 20:00:21 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive