R: bold numbers in plot annotation created by plot

2019-08-02 20:23发布

问题:

I build graphics in which I use regular and bold font for titles and/or labels. They contain symbols, text and numbers which I combine using ?plotmath.

I noticed that using bold within the expression changes only character strings to bold face, but not numbers which are part of the expression. See this minimal example:

x <- y <- 1:10
plot(x, y, xlab=bquote(bold(foo[bar]^2==.(mean(x)))))

In this example, there is a numeric superscript on the LHS, and an evaluated expression on the RHS of the equality sign. Both of which are not printed in bold face as suggested by using bold. When I change these to character strings, the plot shows them in bold.

plot(x, y, xlab=bquote(bold(foo[bar]^"2"==.(as.character(mean(x))))))

Now, for me it is not always desirable to work with character input in these expressions. I would like to find a way of setting the numeric values in bold without converting them to character strings. (I'm also not sure what to expect of the equality sign.)

Additionally, I use Cairo to select specific fonts for my graphics. In my actual graphics, where I also plot greek letters, I use it to set the "symbol" fonts to bold. By setting the "regular" font to bold, I could do the same for numbers, but that would affect all remaining text as well.

Therefore, I would appreciate if there is a solution that doesn't require to change the default font in the Cairo device.

EDIT:

As mentioned in the comments, plotmath does not apply bold to numeric constants as in the example above. I would still like to see if there is a solution that allows to set numbers in lables/titles to bold without changing the rest of the plot (as for example Cairo would).

Thanks!