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.
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))
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.
a <- 2
b <- 5
plot(a,b)
legend("topleft", legend = c(as.expression(bquote(italic("Italic: "*.(a)*" + "*.(b)))),
as.expression(bquote("Non Italic: "*.(a)*" + "*.(b)))) )
To modify your example
a <- 2
b <- 5
plot(a,b)
l1 <- bquote(Italic ~ .(a) + .(b))
l2 <- bquote(a + b)
legend("topleft", legend = c(as.expression(bquote(italic(.(l1)))),
as.expression(bquote(.(l2))) ))
您可以使用text.font = 3
,说:
legend(x='topleft',legend = c(bquote((.(l1))),l2), text.font=c(3,1))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.