2

UPDATE: I actually found the solution myself, see below.

In R I want to add a label to a plot containing both subscript and normal text. To be more precise, I would like to use mtext() (or any other method that does the trick) to add a text below a plot. The text should look like this:

The label
This can easily done in latex with $B\pm t_{a/2}SE(B)$

In R I come as far as mtext(expression(B%+-%t[a/2])), which does print

r so far

But the difficulty is in geting the SE(B) part after it, because of expression treating SE(B) as a function. I've tried several combinations with paste, but to no avail. I'm sure there must be a simple solution to this, but I wasn't able to find one after quite a long search.


UPDATE:

Wow, found the solution myself. As I said I have tried combinations of expression and paste and was sure I tried this before, but apparently, I did not. The solution is this:

mtext(expression(paste(B%+-%t[a/2],"SE(B)")))
Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
SeeDoubleYou
  • 1,253
  • 2
  • 15
  • 25

1 Answers1

7

I see you have solved this, but your final solution is much more nicely and succinctly handled by dropping the use of paste() and using the ~ operator to add spacing:

expression(B %+-% t[a/2] ~ SE(B))

e.g.:

plot(1:10, xlab = expression(B %+-% t[a/2] ~ SE(B)))

which gives

enter image description here

You can add extra spacing by using multiple ~: ~~~ for example. If you just want to juxtapose two parts of an equation, using the * operator, as in:

plot(1:10, xlab = expression(B %+-% t[a/2] * SE(B)))

which gives:

enter image description here

It isn't immediately clear from your Q which one is preferable.

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
  • +1 for illustrating proper use of `*` in plotmath expressions. So many people use `paste` when `*` is the more compact and "express"-ive choice. – IRTFM Sep 28 '11 at 21:50