[R] Using plotmath expressions in lattice key text
Sundar Dorai-Raj
sundar.dorai-raj at pdf.com
Thu Nov 15 20:57:08 CET 2007
Bert Gunter said the following on 11/15/2007 1:12 PM:
> Folks:
>
> delta <- 1:5
>
> I would like to put 5 separate lines of text of the form "10 %+-% delta[i]"
> into a lattice key legend, where ""%+-%" is the plotmath plus/minus symbol
> and delta[i] is the ith value of delta.
>
> The construct:
>
> lapply(delta,function(d)bquote(10%+-%.(d)))
>
> appears to produce a list of expressions of the correct form, and, indeed,
> if I assign the above to (a list!) test,
>
> plot(0:1,0:1)
> text(.5,.5,test[[1]])
>
> produces the correctly formatted plotmath expression. However, note that I
> have to use test[[1]] to extract the expression; test[1] doesn't work (it is
> a list containing an expression, not an expression) -- and therein may lie
> the problem. For if I try to use the above expression as the lab component
> of the text component in key, e.g. by
>
> xyplot(....,
> key = list( text = list(lab =
> lapply(delta,function(d)bquote(10%+-%.(d))),...),...)
>
>
> I get an error:
>
> Error in fun(key = list(text = list(lab = list(10 %+-% 1, 10 %+-% 2 :
> first component of text has to be vector of labels
>
>
> So how should I do this?? I suspect it's simple, but I just can't figure it
> out.
>
> Note: I'd be happy to supply reproducible code if needed. Just complain and
> I'll do so.
>
> Thanks.
>
>
> Bert Gunter
> Genentech Nonclinical Statistics
Hi, Bert,
This is how I've done it in the past. Perhaps there's a better way:
library(lattice)
delta <- 1:5
expr <- paste("10%+-%", delta, sep = "", collapse = ",")
expr <- paste("expression(", expr, ")", sep = "")
expr <- eval(parse(text = expr))
xyplot(0 ~ 0 | factor(delta),
key = list(text = list(expr)),
## bonus: adding it to the strip
strip = function(factor.levels, ...) {
strip.default(factor.levels = expr, ...)
})
Thanks,
--sundar
More information about the R-help
mailing list