1

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

plannapus
  • 18,529
  • 4
  • 72
  • 94
fstevens
  • 1,287
  • 1
  • 17
  • 28

1 Answers1

4

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

Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168