Subscripts in plots in R

RPlotSubscript

R Problem Overview


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!

R Solutions


Solution 1 - R

expression is your friend:

plot(1,1, main=expression('title'^2))  #superscript
plot(1,1, main=expression('title'[2])) #subscript

Solution 2 - R

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]))

Solution 3 - R

See ?expression

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

enter image description here

Solution 4 - R

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

Solution 5 - R

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).

Solution 6 - R

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjeffreyView Question on Stackoverflow
Solution 1 - RsmuView Answer on Stackoverflow
Solution 2 - RCyrilleView Answer on Stackoverflow
Solution 3 - RChaseView Answer on Stackoverflow
Solution 4 - RTony LadsonView Answer on Stackoverflow
Solution 5 - Ruser29609View Answer on Stackoverflow
Solution 6 - RMegatronView Answer on Stackoverflow