"ECOTIÈRE David (Responsable d'activité) - CETE Est/LRPC de Strasbourg/6 Acoustique"
2012-Mar-20 13:09 UTC
[R] Graphic legend with mathematical symbol, numeric variable and character variable
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
Patrick Breheny
2012-Mar-20 19:04 UTC
[R] Graphic legend with mathematical symbol, numeric variable and character variable
There are a few different ways to do this; see the examples in ?plotmath under the heading "How to combine 'math' and numeric variables". -- Patrick Breheny Assistant Professor Department of Biostatistics Department of Statistics University of Kentucky On 03/20/2012 09:09 AM, "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 !
Peter Ehlers
2012-Mar-21 11:57 UTC
[R] Graphic legend with mathematical symbol, numeric variable and character variable
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 ! > > DavidDoes 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
Peter Ehlers
2012-Mar-21 19:27 UTC
[R] Graphic legend with mathematical symbol, numeric variable and character variable
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 >> >>