Reputation: 12005
I can't seem to pass math symbols to a contour label. Is this possible?
Example:
image(volcano)
contour(volcano, level=150, add=TRUE, label = "150") # works
contour(volcano, level=120, add=TRUE, label = expression(alpha)) # doesn't work
contour(volcano, level=110, add=TRUE, label = bquote(alpha)) # doesn't work
text(0.5,0.5, labels = bquote(alpha == .(120))) # works
text(0.2,0.5, labels = expression(paste(alpha, "= 150"))) # works
Upvotes: 2
Views: 139
Reputation: 50718
Here is a solution:
image(volcano);
levels <- c(150, 120, 110);
for (i in 1:length(levels)) {
contour(
volcano, add = TRUE,
level = levels[i],
label = sprintf("a = %i", levels[i]),
vfont = c("sans serif symbol", "plain"), labcex = 1);
}
The key here is to use vfont
to force Hershey vector fonts instead of the current font family. You can then use the "HersheySansSymbol" font family to typeset "a" as "α", by using the contour
function argument vfont = c("sans serif symbol", "plain")
. It's a bit hidden, but more details on the Hershey fonts can be found in ?Hershey
.
PS. You can also use a seriffed bold font with vfont = c("serif symbol", "bold")
.
Upvotes: 3