5

I'd like to create latex-style math in plot titles in R. The plotmath tools have a useful but limited subset of expressions they can display, and use non-latex syntax and style.

For instance, I would like to print the equation $\mathrm{d} \mathbf{x} = a [\theta - \mathbf{x}] \mathrm{d} t$ in the title of the plot, and still evaluate the value of certain variables. The best solution I have is the very cumbersome:

lambda <- 4
plot(1:10,1:10)

mtext(bquote(paste(d*bolditalic(x)[italic(t)] == alpha*(theta - bolditalic(x)[italic(t)] )*d*italic(t) +  .(lambda) * d * italic(B)[italic(t)] )), line=2.25,cex=2) 

or

require(ggplot2)
qplot(1:10, 1:10) + opts(title=bquote(paste(d*bolditalic(x)[italic(t)] == alpha*(theta - bolditalic(x)[italic(t)] )*d*italic(t) +  .(lambda) * d * italic(B)[italic(t)] )), line=2.25,cex=2)

This question almost solves this, but I loose the ability of bquote to display the numerical value of a variable (lambda in my example). Is there a way to combine these so I can have italic greek letters (the standard format for variables, e.g. as done by tex) and evaluate at specific values?

Is there a much better way where I can simple write tex-syntax for equations and add them to my graphs?

Community
  • 1
  • 1
cboettig
  • 12,377
  • 13
  • 70
  • 113
  • I'd also like to display square as in my latex style equation, instead of parentheses, but cannot see how to do this either. – cboettig Nov 18 '11 at 22:54
  • You can use this CRAN package that translates LaTeX into plotmath: https://github.com/stefano-meschiari/latex2exp – Stefano Meschiari Jul 19 '19 at 17:54

2 Answers2

13

You may want to check out the tikzDevice package (or take a look at its vignette first), which provides a natural way to write LaTeX math in R graphics.

I also have a simple example here.

And here is another example:

library(tikzDevice)
library(ggplot2)
tikz('math.tex', standAlone = TRUE, width = 4, height = 4)
qplot(1:10, 1:10, 
  main = '$\\mathrm{d} \\mathbf{x} = a [\\theta - \\mathbf{x}] \\mathrm{d} t$')
dev.off()

which produces this if you run pdflatex math.tex:

latex math in ggplot2

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
  • I saw that example and thought it was quite kewl, however, my efforts to apply it to this example failed. – IRTFM Nov 18 '11 at 23:49
  • 1
    I have edited the answer a little bit. You can play with your own math expression. What you need to give `tikzDevice` is just a plain text string, so if you want your lambda to be a number, you can use code like `sprintf('\\alpha + %s + \\beta', 1.234)`; no hacks with `bquote()` or `substitute()` or `expression()` required. – Yihui Xie Nov 19 '11 at 00:17
  • I guess this demonstrates a difference in out TeX setups. I get this error message from the TeXworks processor (same as I did earlier): This is pdfTeX, Version 3.1415926-1.40.10 (TeX Live 2009) entering extended mode (./math.tex ! Undefined control sequence. l.3 \documentclass [10pt]{article} ? – IRTFM Nov 19 '11 at 00:23
  • Yes. pdflatex generates the plot seen above. – IRTFM Nov 19 '11 at 01:41
  • Thanks, this is just what I was looking for! Nice to note that the sprintf solution with tikzDevice can work with a variable value, just as bquote/substitute do (perhaps that was obvious). – cboettig Nov 22 '11 at 02:05
  • I have put this example in the manual of a new package that I have been working on: https://github.com/downloads/yihui/knitr/knitr-manual.pdf – Yihui Xie Nov 22 '11 at 02:32
4

You should edit your question rather than putting sub-comments (I'll see what I can do about the box.). I sometimes find that bquote only reaches down so deep and that you need to use substitute. This works:

 lambda <- 4
 plot(1:10,1:10)

mtext(substitute(paste(d*bolditalic(x)[italic(t)] == 
                       alpha*group("[", (theta - bolditalic(x)[italic(t)] )*d*italic(t) + 
                        lambda, "]") * d * italic(B)[italic(t)] ), list(lambda=lambda)), 
      line=2.25,cex=2)

It also works with ggplot and the title placement looks a lot better:

 qplot(1:10, 1:10) + opts(title=substitute(paste(d*bolditalic(x)[italic(t)] == 
                        alpha*(theta - bolditalic(x)[italic(t)] )*d*italic(t) + 
                         lambda * d * italic(B)[italic(t)] ), list(lambda=lambda)), 
                       line=2.25,cex=2)

enter image description here

The "d" is "square" at least as I understand your meaning after referring to a set of Latex examples online at:

http://www.personal.ceu.hu/tex/cookbook.html

(I thought you wanted the http://en.wikipedia.org/wiki/D%27Alembert_operator and was not able to find that yet.)

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • 1
    I think the OP wants square brackets, rather than parentheses. For that, you can use `group`, like this: `group("[", list(theta, bolditalic(x)[italic(t)] ), "]")`. (Got that from the 4th or 5th page of `demo(plotmath)`). – Josh O'Brien Nov 19 '11 at 02:37
  • Let's see. The term "square-bracket" I would have understood. – IRTFM Nov 19 '11 at 02:58
  • 1
    Agreed. 'instead of parentheses' is the only clue I'm going on here, (and I could easily be wrong about what's being asked for). – Josh O'Brien Nov 19 '11 at 03:00
  • 1
    My apologies for the question not being clear - I was looking for italic greek letters, as well as square brackets, in the main title, and preferably the use of latex style input, which is more consise than adding the italic commands manually, and generally more flexible than plotmath options. The chosen answer his every one of these. – cboettig Nov 22 '11 at 02:10