1

I want to store formatted legend texts in an array.

normFactor= c(1e-1, 1e-2, 1e-3)

legend_text[1] = bquote(mu ~ "=" ~ .(normFactor[1]))
legend_text[2] = bquote(mu ~ "=" ~ .(normFactor[2]))
legend_text[3] = bquote(mu ~ "=" ~ .(normFactor[3]))

The bquote-output seems to be of the type language:

str(bquote(mu ~ "=" ~ .(normFactor[3])))

language mu ~ "=" ~ 0.001

So I thought I could set up an array of language-Elements:

legend_text = language()

Unfortuantely this does not work, and I'm wondering how I can store those elements in a array,...

R_User
  • 10,682
  • 25
  • 79
  • 120

2 Answers2

2

You can store them in a list, or even better create them like this:

lapply(seq_along(normFactor),
         function(i)bquote(mu ~ "=" ~ .(normFactor[i])))

[[1]]
mu ~ "=" ~ 0.1

[[2]]
mu ~ "=" ~ 0.01

[[3]]
mu ~ "=" ~ 0.001

bquote create a language object may in fact be converted to and from lists using as.list and as.call. So the solution a bove can be modified for exmaple:

> ll <- lapply(seq_along(normFactor),
+        function(i)as.list(bquote(mu ~ "=" ~ .(normFactor[i]))))
> str(ll)
List of 3
 $ :List of 3
  ..$ : symbol ~
  ..$ : language mu ~ "="
  ..$ : num 0.1
 $ :List of 3
  ..$ : symbol ~
  ..$ : language mu ~ "="
  ..$ : num 0.01
 $ :List of 3
  ..$ : symbol ~
  ..$ : language mu ~ "="
  ..$ : num 0.001

Then coercing the result to a list of calls:

> lapply(ll,as.call)
[[1]]
mu ~ "=" ~ 0.1

[[2]]
mu ~ "=" ~ 0.01

[[3]]
mu ~ "=" ~ 0.001
agstudy
  • 119,832
  • 17
  • 199
  • 261
1

language is not an atomic data type, so the outcome will be a list rather than a vector. Dynamic typing is your friend here:

legend_text<- c(
 bquote(mu ~ "=" ~ .(normFactor[1])),
 bquote(mu ~ "=" ~ .(normFactor[2])),
 bquote(mu ~ "=" ~ .(normFactor[3]))
)

I don't know if this os what you actually need for your legend, though.

  • This works, if you souround the bquotes with `as.expression`. See: http://stackoverflow.com/questions/15288900/use-sub-superscript-and-special-characters-in-legend-texts-of-r-plots/15289043?noredirect=1#15289043 – R_User Mar 08 '13 at 07:51