Antonio Manco
Antonio Manco

Reputation: 229

How can I use bold and expression in ggplot with special characters?

I think that is very simple.

I'm using bold in the expression function in ggplot2. It's all clear for me and it works. But when I use it with special characters like mu*mol to have μmol or H[2]*O to have 2 as subscript, it doesn't work. Practically it doesn't make bold the special characters.

I tried to use the bold function that I know with expression in ggplot.

I show you only the string of the plot code that I use to set label.

scale_y_continuous(sec.axis = sec_axis(trans = ~ ./5, name = expression(bold(atop(H[2]*O,(mu*mol~m^bold("-2")~s^bold("-1"))))),breaks=c(-3,-1.5,0,1.5,3)),breaks=seq(-10,20,10))

In the figure you can see that μ is not bold and the subscript 2 of H2O is not bold.

Upvotes: 5

Views: 3354

Answers (1)

Arienrhod
Arienrhod

Reputation: 2581

It's enough to use a single bold statement in your expression, but then you have to surround every subscript and superscript with quotation marks. This would still leave your greek letter mu without bold face, b/c plotmath doesn't have bold symbol fonts (see here). However, you can circumvent this by replacing mu with the unicode character (you can find them here).

Here I've just used the standard mtcars data set.

library(ggplot2)
ggplot(mtcars, aes(x = wt, y = qsec)) +
geom_line() +
scale_y_continuous(sec.axis = sec_axis(trans = ~ ./5, 
                                       name = expression(bold(atop(H["2"]*O,("\u03bc"*mol~m^"-2"~s^"-1"))))))

enter image description here

Upvotes: 6

Related Questions