Search code examples
rtitleplotmath

title and text formatting in r - use simultaneously variables and superscript


I would like to use simultaneously the value of a variable and math formatting in the title of a classic (no ggplot2) figure in R.

I found a solution to have the content of the variable in the title, but not the superscript.

number <- c('first','second','third')
plot(1:10,1:10)
title(main=paste(substitute(x,list(x=number[1])),' plot, units are in km m-3'))

I also found a solution to do the contrary :

plot(1:10,1:10)
title(main=expression(paste(number[1],' plot, units are in km ',m^{-3})))

However, it is very empirical, because my brain is completely messed up with all this notions of expressions, parsing, quoting, plotmath, substitute, ...

If you underdtantd this better than I do, would you propose a simple solution ?

Thank you,

François


Solution

  • A possible solution with bquote:

    plot(1:10, 1:10)
    title(main = bquote(.(number[1]) ~ "plot, units are in km" ~ m^-3))
    

    enter image description here