[R] Graphic legend with mathematical symbol, numeric variable and character variable
Peter Ehlers
ehlers at ucalgary.ca
Wed Mar 21 20:27:17 CET 2012
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, "ECOTIÈRE 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, "ECOTIÈRE 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
>>
>>
More information about the R-help
mailing list