Search code examples
rggplot2superscript

How to get a subscript into an axis title in `ggplot`


I want to have -1 in an axis title with the -1 being superscript. The title on my y axis should read "Ba:Ca (µmol:mol-1)" with the -1 as superscript. I have tried ^ before the -1 and also the expression function to no avail. R Rookie, please help.


Solution

  • Is this something you want?

    set.seed(4)
    x <- runif(20, 5, 10)
    y <- runif(20, 15, 20)
    
    df <- data.frame(x,y)
    
    ggplot()+
      geom_point(data=df, aes(x,y))+
      labs(y = expression(paste('Ba:Ca (µmol:mol'^-1,')')), x = "x axis")
    
    

    Output