# [R] Legend containing maths symbol and values of variables

Uwe Ligges ligges at statistik.tu-dortmund.de
Tue Mar 24 12:24:22 CET 2009

```So what about

threshold <- 0.5
plot(NA, xlab="", ylab="", main="", axes=F, xlim=c(0,1),
ylim=c(0,1), xaxs="i", yaxs="i")
legend1 <- substitute("Up (" * phantom() >= threshold * ")",
list(threshold=threshold))
legend(x=0, y=1, fill=c("orange", "white", "green", "gray90"),
cex=0.8, pt.cex=1.8, x.intersp=0.6, bg = "white", bty = "n",
legend =
do.call("expression", list(legend1, "Normal", "Down", "NA")))

Daren Tan wrote:
> I tried sprintf and paste, which solves one of my problem i.e.
> replacing the variable name by its value. However, >= is not renderend
> correctly as the inequality symbol.
>
> On Tue, Mar 24, 2009 at 12:24 PM, Gabor Grothendieck
> <ggrothendieck at gmail.com> wrote:
>> For the legend argument try this:
>>
>> leg = as.expression(c(sprintf("Up (>= %d)", threshold),
>>        "Normal", "Down", "NA"))
>>
>> or use paste in place of sprintf.
>>
>> On Mon, Mar 23, 2009 at 11:53 PM, Daren Tan <darentan76 at gmail.com> wrote:
>>> I need to have the maths symbol for >= in the legend, and to
>>> substitute threshold variable with its value. Somehow, various
>>>
>>> threshold <- 0.5
>>>
>>> plot(NA, xlab="", ylab="", main="", axes=F, xlim=c(0,1), ylim=c(0,1),
>>> xaxs="i", yaxs="i")
>>> legend(x=0, y=1, fill=c("orange", "white", "green", "gray90"),
>>> cex=0.8, pt.cex=1.8, x.intersp=0.6, bg = "white",
>>>           legend=c(expression("Up (" >= threshold ")"), "Normal",
>>> "Down", "NA"), bty = "n")
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help