By Brani


2010-11-29 09:27:25 8 Comments

How can I combine text and math expressions in a plot's title. If I use paste the expression is converted to character. For example I want something like this as a title

$ARL_1$ curve for $S^2$

Thank you

3 comments

@loki 2018-02-02 15:54:30

You can also use latex2exp::TeX to convert TeX to expressions autmatically:

plot(cars, main = TeX("$ARL_1$ curve for $S^2$"))

@fabians 2010-11-29 14:10:01

You can also use bquote(paste(...)), which is a little more flexible than expression: you can include variable values (say, the value of x) in the labels with .(x). For example:

x<- 232323
plot(1:10, main = bquote(paste(ARL[1], " curve for ", S^2, "; x=",.(x))))

@42- 2010-11-29 15:16:34

Yes although I generally find that using the plotmath paste function confuses more than it helps. Try > x<- 232323 > plot(1:10, main = bquote(ARL[1]~"curve for"~S^2~";"~x==.(x)))

@Reinstate Monica - G. Simpson 2010-11-29 15:59:11

+1 Dwin - introducing paste is a pain in the but if you get your , or "" in the wrong places...

@fabians 2010-11-29 16:24:35

@GavinSimpson Yeah, your answer is nicer. But I thought it would be good to point out a more flexible solution in case somebody comes here looking for a more general solution.

@Reinstate Monica - G. Simpson 2010-11-29 16:41:29

+1 from me. That comment wasn't a criticism of the substantive point of your answer (bquote()), just a point about introducing paste in there if you can avoid it. bquote() is underused and very powerful!

@42- 2010-11-29 22:16:04

.. another 1+ from me. I thought bquote was an excellent suggestion and only added a very minor demurral to avoid paste when possible, because it often gets confused with that "other paste". People don't realize it's a different function with different sematics and with no sep argument.

@Reinstate Monica - G. Simpson 2010-11-29 09:34:51

You want to read ?plotmath to see how to do this sort of thing. Here is an example:

plot(1:10, main = expression(ARL[1] ~ "curve for" ~ S^2))

The [.] is subscript, whilst ^ gives superscript. The ~ spaces out the parts of the expression as if there were literal spaces.

Edit: normally I would have done:

plot(1:10, main = expression(ARL[1] ~ curve ~ for ~ S^2))

but that throws an error because for is being interpreted as the start of a for() loop call.

@Marek 2010-11-29 10:32:38

Second will work if you use expression(ARL[1] ~ curve ~ "for" ~ S^2).

@Reinstate Monica - G. Simpson 2010-11-29 11:56:03

@Marek: Oh, yes, agreed. But if you are going to do "for" you might as well do "curve for", unless ~ and " " are different amounts of space...?

@Marek 2010-11-29 12:28:35

Of course. It matters in more complicated cases when one needs to mixed more strings. I add it as completeness ;) On other hand you could also use back-ticks `.

Related Questions

Sponsored Content

23 Answered Questions

[SOLVED] How to make a great R reproducible example

  • 2011-05-11 11:12:02
  • Andrie
  • 277703 View
  • 2474 Score
  • 23 Answer
  • Tags:   r r-faq

3 Answered Questions

[SOLVED] data.table vs dplyr: can one do something well the other can't or does poorly?

  • 2014-01-29 15:21:45
  • BrodieG
  • 113012 View
  • 717 Score
  • 3 Answer
  • Tags:   r data.table dplyr

9 Answered Questions

[SOLVED] Grouping functions (tapply, by, aggregate) and the *apply family

3 Answered Questions

[SOLVED] How to make Regular expression into non-greedy?

1 Answered Questions

[SOLVED] regular expression in main title with `bquote`?

  • 2015-04-09 15:27:45
  • Giuseppe
  • 200 View
  • 0 Score
  • 1 Answer
  • Tags:   r plot expression

3 Answered Questions

[SOLVED] How to use more than one expression in a row

1 Answered Questions

[SOLVED] Substitute plotmath character string into expression

3 Answered Questions

[SOLVED] Include text control characters in plotmath expressions

1 Answered Questions

[SOLVED] Strings & Expressions in Plot Titles, Labels etc

  • 2013-08-25 02:25:20
  • Bryan Hanson
  • 334 View
  • 3 Score
  • 1 Answer
  • Tags:   r plotmath

Sponsored Content