118

I can't find a way how to write subscripts in the title or the subtitle in R. How can I write v 1,2 with 1,2 as subscripts?

Thanks for your help!

jeffrey
  • 2,026
  • 5
  • 28
  • 42

6 Answers6

163

expression is your friend:

plot(1,1, main=expression('title'^2))  #superscript
plot(1,1, main=expression('title'[2])) #subscript
Tom Kelly
  • 1,458
  • 17
  • 25
smu
  • 8,757
  • 2
  • 19
  • 14
  • 3
    How to load "2" from the variable? I have a loop and need to plot x_[1] x_[2] x_[3] ... – 0x2207 Dec 11 '14 at 11:38
  • 8
    You can use `bquote` when working with subscripted variables. Say, `nIter <- 2`, then `plot(1, 1, main = bquote(title[.(nIter)]))` is exactly what you need (taken from the [R-help mailing list](https://stat.ethz.ch/pipermail/r-help/2005-May/070670.html)). – fdetsch Jan 07 '15 at 09:55
141

If you are looking to have multiple subscripts in one text then use the star(*) to separate the sections:

plot(1:10, xlab=expression('hi'[5]*'there'[6]^8*'you'[2]))
Cyrille
  • 3,427
  • 1
  • 30
  • 32
  • 6
    Wow, a two-day old answer to a two-year old question, that turns out to be what I needed. Thanks! – Michael May 24 '14 at 00:25
  • 3
    And if you want the subscript to be a string, just put it in quotes: `plot(1:10, xlab=expression('hi'[5]*'there'[6]^8*'you'['down here']*'and'^'up'*'there'))` – Stewart Macdonald Sep 11 '14 at 07:28
  • 1
    That's a good tip as you can put `[digits]` or `[characters]` or even `[a5]` in the subscript but not `[5a]` or `[a a]`. I recently discovered this for: `expression('x'['10sdt'])` – Cyrille Sep 19 '14 at 13:26
34

See ?expression

plot(1:10,main=expression("This is a subscript "[2]))

enter image description here

Chase
  • 67,710
  • 18
  • 144
  • 161
13

A subscript and referring to a stored value...

a <- 10
plot(c(0,1), c(0,1), type = 'n', ann = FALSE, xaxt = 'n', yaxt = 'n')
text(0.2, 0.6, cex = 1.5, bquote(paste('S'['f']*' = ', .(a))))

enter image description here

Tony Ladson
  • 3,539
  • 1
  • 23
  • 30
7

Another example, expression works for negative superscripts without the need for quotes around the negative number:

title(xlab=expression("Nitrate Loading in kg ha"^-1*"yr"^-1))

and you only need the * to separate sections as mentioned above (when you write a superscript or subscript and need to add more text to the expression after).

user29609
  • 1,991
  • 18
  • 22
7

As other users have pointed out, we use expression(). I'd like to answer the original question which involves a comma in the subscript:

How can I write v 1,2 with 1,2 as subscripts?

plot(1:10, 11:20 , main=expression(v["1,2"]))

Also, I'd like to add the reference for those looking to find the full expression syntax in R plotting: For more information see the ?plotmath help page. Running demo(plotmath) will showcase many expressions and relevant syntax.

Remember to use * to join different types of text within an expression.

Here is some of the sample output from demo(plotmath):

enter image description here

Megatron
  • 15,909
  • 12
  • 89
  • 97