Search code examples
rggplot2plotmath

Plotmath expression with mathematical operators in ggplot2


I am still struggling with correct labels of my plots. I managed to get this to work.

df <-data.frame(a=seq(1,20),b=seq(1,20))
ggplot(df, aes(x = a, y = b)) + geom_point() +
  ylab(expression(paste(CH[4]~" formation rate"~"(",mu, mol, day^{-1},")")))

What it is missing is the operator sign " * " between mumol and day^-1. It should look like this: enter image description here Thank you for your help.


Solution

  • I prefer not to use the paste style for expressions but to use ~ and * to separate terms with or without a space respectively. If you want an *, put it in quotes.

    ggplot(df, aes(x = a, y = b)) + 
      geom_point() + 
      ylab(expression(CH[4]~formation~rate~(mu*mol~'*'~day^{-1})))