Subscripts in R when adding other text

10,579

Solution 1

You do not need paste (or quotes for that matter) at all:

expression( A[2]~and~B[2] )

Test:

plot(1,1, main=expression( A[2]~and~B[2] ) )

The syntactic principle is that tildes (which creates a space) and asterisks (non-space plotmath separator) are used to separate items and that no quotes are needed unless you are using a plotmath function name .... such as wanting the word "paste" or "sqrt" to appear in the displayed version of the expression.

Solution 2

Just paste them together:

expression(paste("A"[2], " and B"[2])

Share:
10,579
Damien
Author by

Damien

Updated on June 21, 2022

Comments

  • Damien
    Damien about 2 years

    How would you add a subscript to one particular word of a title in R? For example, suppose the title is "A_2 and B_2." How would you add these two subscripts? I know that expression("A"[2]) and expression("B"[2]) individually add subscripts to these letters.

  • blmoore
    blmoore over 11 years
    plot(x, main=expression(paste("A"[2], " and B"[2], sep=""))) (where x is your data...) doesn't work? What device + plot function are you using?
  • Ricardo Saporta
    Ricardo Saporta over 11 years
    @Damien, this works using the method described as well as > plot(x) > title(expression(paste("A"[2], " and B"[2], sep=""))) Please doublecheck your implementation
  • IRTFM
    IRTFM over 11 years
    WILL PEOPLE PLEASE STOP USING sep= IN PLOTMATH paste? (It is not the same function as base::paste.)