I want to add a legend to a plot in which only one of the legend names is italicized. I'm using plot()
in base R.
- However, I need that italicized line to contain variable numbers, so I use
bquote
.
What I've tried does not work:
a <- 2 ; b <- 5
plot(a,b)
l1 <- bquote(Italic ~ .(a) + .(b))
l2 <- bquote(a + b)
legend(x='topright',legend = bquote(italic(.(l1))))
legend(x='topleft',legend = c(bquote(italic(.(l1))),l2))
- Notice that when I leave the expression on its own (RIGHT LEGEND), it italicizes correctly. However, when I add the text for other legend lines, it messes up (LEFT LEGEND).
How do I properly do this?
Note: I'd rather have a solution that doesn't require multiple calls to legend()
.
You should use
as.expression
to coerce everything into an expression object.To modify your example
You could use
text.font = 3
, say: