Question

I have a vector of characters:

x <- c("species1", "species2", "species3")

I would like to add each of these elements in a plot, as a legend, for instance, like this:

legend("topleft", x[1], bty="n")

But here comes my problem: it needs to be in italic.

What I do is to try this:

legend("topright",expression(italic(x[1])), bty="n")

However, instead of getting it in italics the value in x[1] (in this case: "species1"), what I get in italics is the "x[1]" itself!

Is there any way to solve this?

Was it helpful?

Solution

You can use substitute to swap out variables in an expression. This should work

legend("topright",legend=substitute(italic(x), list(x=x[1])), bty="n")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top