Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
645 views
in Technique[技术] by (71.8m points)

r - Italic greek letters / latex-style math in plot titles

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 [heta - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...