R: bold numbers in plot annotation created by plotmath - r

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!

Related

using greek letters in tmap (in R)

I have been trying to include greek letters in my legend title when using functions in the tmap r-library. I have tried several different things, all resulting in mixed results, none of which produce a useable product. Attempts include:
The Tex('$\\mu$') function in the latex2exp r-library
bquote(mu)
finally olde but goodie expression(paste(mu,"g",sep=""))
All of these attempts either result in a line of text not including the greek letter or some variant with strange formatting.
Has anyone ran into this issue? If so could you please share your resources?
Edits/Additions:
Thanks to #James input unicode characters works perfectly in tmap. The next question is how to do superscript or subscripts without using the expression or paste functions since these functions are not 100% compatible with tmap.
Is there a way to have a superscript/subscript in a legend title for tmap? As you can tell units mean a lot to me (*wink).

In R plots, how to have symbol for the set of real numbers with font similar to LaTeX's \mathbb?

Here is what I am trying to achieve.
When designing a plot in R-project, I would like to include in the plot's legend/axes the "R" symbol for the set of real numbers with font similar to that of LaTeX's \mathbb command (i.e. the stylized way to write uppercase letters when referring to sets of real numbers, natural numbers, etc).
Would that be possible to achieve somehow? I found older questions and answers on how to use OS fonts in parts of R plots, but this alone does not solve the issue.

R ylab line modified with italic

I have trouble making my plots looking similar because my labels (both xlab and ylab) move if I use italic. Consider the following short example:
plot(1:10,cex.lab=1.25,ylab="p-value")
plot(1:10,cex.lab=1.25,ylab=expression(italic(p)~"-value"))
The problem is that "p-value" is slightly nearest from the axis than "p-value". I believe it's because the p tail defines the writing line which is considered different without italic. How can I fix that easily?
I frequently use both strings and expressions for my plots and that would be complicated to use mtext (with the line argument) to manage each label for each plot.
I found a workaround playing with unicode characters.
p is \U1D631 and P is \U1D617
plot(1:10,cex.lab=1.25,ylab="p-value")
plot(1:10,cex.lab=1.25,ylab="\U1D631-value")
This solved my problem.

R How to insert zapf dingbats font characters in text for axis or legend label

I'm using the PDF device in R and I would like if possible to incorporate some characters from the zapf dingbats font in my legend lables (specifically the large open and filled circles for cloud situations of overcast and clear sky).
I found the extrafont package https://cran.r-project.org/web/packages/extrafont/extrafont.pdf but that seems to change the overall font. I also know how to use expression for greek and math symbols, but that doesn't seem to include these symbols.
I attach a plot that I made with NCL to show the kind of lable I am trying to recreate in my R script.
You just specify the appropriate Unicode value.
plot(iris[,3:4])
legend("topleft", legend=c("\U2600", "\U2601", "\U2602", "\U2603"))

Spacing in axis label when using expression(paste(...))

Consider the following example:
plot(c(2,4,6)~c(1,2,3),xlab="x",
ylab=expression(paste('flux (g ',CO[2]~m^{-2}~h^{-1},')')))
Obviously I want a full space between "g" and "CO", but for some reason I get a smaller (with some labels even zero) space in the graph label.
The problem is even more obvious, if I do it like this:
plot(c(2,4,6)~c(1,2,3),xlab="x",
ylab=expression(paste('flux (g C',O[2]~m^{-2}~h^{-1},')')))
Am I doing something wrong? Is there a way to fix the spacing or even a better way to create labels with lots of sub/superscripts and greek letters?
In all likelihood you are getting a typographically correct "space", in the font your OS uses for non-serif display. You can change fonts or you can insert blank space that is sufficient to hold a particular character string with plotmath phantom():
plot(c(2,4,6)~c(1,2,3),xlab="x",
ylab=expression(paste('flux',phantom(x),'(g ',CO[2]~m^{-2}~h^{-1},')')))
Or as #baptiste points out this can be done without plomath paste using ordinary plotmath separators because a tilde in a true R expression gets handled as a "space":
ylab=expression(flux*phantom(x)*(g~CO[2]~m^{-2}~h^{-1})))

Resources