Greek letters in ggplot2 titles and legends within knitr

Is there an easier way to include a Greek letter mu in a ggplot2 figure legend (or title) within a knitr document? The usual knitr $\\mu$ is printed literally (without the duplicated backslash) within the ggplot2 figure. I also tried defining a phrase within the code block and then using that phrase within the ggplot function. But again the the $\\mu$ was printed literally (removing the duplicated backslash), not translated to the Greek mu. So apparently the $\\mu$ method doesn't work within a code block. I found a method that works defining a phrase within the code block using
expression(paste("Baseline insulin (", mu, "U/mL)"))
but this method would be cumbersome if one needed to use multiple Greek letters. Is there an easier way?
Thanks in advance for help with this!
Larry Hunsicker

You can use bquote()

library(ggplot2)

ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
    geom_point() + 
    labs(
        title = bquote(Baseline~insulin(mu*U/mL))
        )

Created on 2020-06-21 by the reprex package (v0.3.0)

That does it. Thanks, andesrcs!

bquote is part of the base package. I found the documentation to be pretty obscure. It seems to assume an understanding of lisp. I found a clearer explanation of how this works at https://trinkerrstuff.wordpress.com/2018/03/15/2246/
This post also refers to plotmath {grDevices}.
Maybe this will help others trying to enter math symbols/language into r figure legends.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.