[R] legend(): how to put variable in subscript?

Peter Dalgaard p.dalgaard at biostat.ku.dk
Sun May 1 09:25:14 CEST 2005


Aleksey Naumov <naumov at buffalo.edu> writes:

> Dear List,
> 
> I would like to plot a simple legend with two math expressions, e.g.
> 
> plot(0)
> legend(1, 0.5, expression(sigma[i], sigma[j]))
> 
> The difficulty is that i and j should be variables rather than strings "i" and 
> "j". In other words I'd like to do something like:
> 
> i = "A"
> j = "B"
> legend(1, 0.5, expression(sigma[i], sigma[j]))
> 
> and have "A" and "B" as the actual subscripts. I can substitute the variable 
> in the expression e.g.:
> 
> legend(1, 0.5, substitute(sigma[i], list(i='A', j='B')))
> legend(1, 0.5, bquote(sigma[.(i)]))
> 
> however, this gives me just one of the two entries in the legend. I cannot 
> figure out how to include both sigmas in the legend.
> 
> What would be the best way to do something like this? Thank you for your ideas 
> or suggestions.

Ick. One of those cases that suggests that our current substitute
mechanisms don't quite cut it... However, try

 i <- "A"; j <- "B"
 e <- bquote(expression(sigma[.(i)],sigma[.(j)]))
 plot(0)
 legend(1,.5,eval(e))
 legend(1,-.5,e) # for comparison

Thing is, substitute(expression(....),...) returns a call to the
"expression" constructor, rather than the expression itself, so you
need the eval().


-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list