3

Here is a simple example:

ylab<-expression(paste(beta, "/ml"))
plot(1:10, ylab=ylab)

enter image description here

but if I index beta using another variable:

unit<-beta
ylab<-expression(paste(unit, "/ml"))
plot(1:10, ylab=ylab)

enter image description here

My question is how to index these Greek symbols in R expression?

David Z
  • 6,641
  • 11
  • 50
  • 101
  • If you didn't get an error with the second three commands then it means you have a data-object in your workspace named `beta`. Are you actually trying to create text labels at tick marks? Otherwise I'm not seeing anything that I would call "indexing". – IRTFM Aug 25 '16 at 20:07
  • @42- not necessarily because beta is a base function in base; so no error expected – Chris Holbrook Aug 25 '16 at 20:12
  • Oh, right. But still seems unlikely that the OP actually wanted to create a function named `unit` that requires two arguments. My request for clarification of the goals remains. – IRTFM Aug 25 '16 at 20:14
  • see http://stackoverflow.com/questions/4973898/combining-paste-and-expression-functions-in-plot-labels for related question dealing with `paste` and `expression` – Chris Holbrook Aug 25 '16 at 20:24
  • @42, can you reproduce the issue at your end? if not, maybe the issues would be at my local. – David Z Aug 25 '16 at 20:44
  • Probably not an answer, but R has quite fair unicode support. You can be pragmatic sometimes and do: symbols <- c("α", "β", "γ", "δ", "ε", "ζ"); ylab <- symbols[2]; plot(1:10, ylab=ylab). Though the formatting might be different. – haddr Aug 25 '16 at 23:50

2 Answers2

3

first off beta should be double-quoted so it's a string not the base function, as pointed out by @42-

otherwise it seems you can paste a string together and then evaluate it like this

unit<-"beta" #note double quotes
txt <- paste0("expression(",unit,"~'/ml')") #built expression as string
ylab<-eval(parse(text=txt)) #evaluate the expression with parse
plot(1:10, ylab=ylab)

enter image description here

Chris Holbrook
  • 2,531
  • 1
  • 17
  • 30
3

Update: Appears that there was a desire to pass a variable from outside an expression into an expression-classed variable constructed outside the plot call. This seems much less complex than using an eval(parse(text=paste( ... )) construction:

unit=quote(beta)
ylab<-bquote(.(unit)/ml)
plot(1:10, ylab=ylab)

The lesson being illustrated here is using quote to prevent evaluation of a symbol and then to evaluate to a name inside an expression.

Could also have used either as.name or as.symbol:

 unit=as.name('beta')

Answer to the question I originally suspected is being asked, since it's one (actually two) I struggled with in my early years of R. I'm guessed that you want to construct tick labels rather than axis labels and that you want these to appear as greek letters or other plotmath-y constructions, but with ascending "indexing" values substituted. The second request requires using either substitute or bquote, while the first request require remembering to suppress the y-axis ticks and lables with a par argument and then using axis.

plot(1:10, ylab=ylab,yaxt="n")
# Now create "horizontally aligned" (using las=2)  beta-value tick labels
axis(2, at=1:10, 
        labels=as.expression(lapply( 1:10, function(x) bquote( beta == .(x)))) ,
        las=2)

The as.expression wrapper around a list of bquote-ed-results is something I found in an earlier answer (by me) and I thought of just labeling as a duplicate, but it didn't involve any plotting so I decided to consider it a "partial overlap".

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • i like use of `quote` and `bquote` for this. how could you modify for case when `unit` is a vector of symbols? (e.g., beta, mu, phi) – Chris Holbrook Aug 26 '16 at 18:43
  • `unit <- expression(beta, alpha, mu) ; as.expression(lapply( 1:3, function(x) bquote( .(unit[[x]]) == .(x))))` returns: expression(beta == 1L, alpha == 2L, mu == 3L) – IRTFM Aug 26 '16 at 23:50