如何在带有特殊字符的ggplot中使用粗体和表达式?

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

我觉得很简单

我在 ggplot2 的表达式函数中使用粗体。 这对我来说很清楚并且有效。 但是当我将它与特殊字符一起使用时,比如 mu*molμmolH[2]*O2 作为下标,它不起作用。 实际上它不会使特殊字符变粗。

我尝试在 ggplot 的表达式中使用我知道的粗体函数。

我只给你看我用来设置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))

图中可以看到μ不是粗体,H2O的下标2也不是粗体。

在表达式中使用单个 bold 语句就足够了,但是您必须用引号将每个下标和上标括起来。这仍然会让你的希腊字母 mu 没有粗体,b/c plotmath 没有粗体符号字体(参见 here). However, you can circumvent this by replacing mu with the unicode character (you can find them here)。

这里我只使用了标准的 mtcars 数据集。

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"))))))