Wolfgang Viechtbauer
2004-Jan-28 22:21 UTC
[R] Math Expression and Variable Value in Title
Hello All, I am trying to put a math expression into a plot title and at the same time, I want a value in the title to depend on a variable that I set earlier. Simple Example: n <- 20 plot(0, 0) title(expression(paste(n[i], " = ", n))) Obviously, I want "n_i = 20". How can I get that? Thanks in advance, -- Wolfgang Viechtbauer
Use substitute() n <- 20 plot(0, 0, main = substitute(paste(n[i], " = ", k), list(k = n))) -roger Wolfgang Viechtbauer wrote:> Hello All, > > I am trying to put a math expression into a plot title and at the same > time, I want a value in the title to depend on a variable that I set > earlier. > > Simple Example: > > n <- 20 > plot(0, 0) > title(expression(paste(n[i], " = ", n))) > > Obviously, I want "n_i = 20". How can I get that? > > Thanks in advance, >
>>>>> "Roger" == Roger D Peng <rpeng at jhsph.edu> >>>>> on Wed, 28 Jan 2004 19:35:28 -0500 writes:Roger> Use substitute() Yes! Roger> n <- 20 Roger> plot(0, 0, main = substitute(paste(n[i], " = ", k), list(k = n))) but even better is plot(0, 0, main = substitute(n[i] == k, list(k = n))) (note the "==" !) Martin
An alternative but also higly recommendable way is the use of the psfrag-package in your LaTeX-file. It is furthermore very simple in use. Type plot(1:10, main="test") in your LaTeX-file you insert in your figure-environment the command \psfrag{test}{$n_i=20$} before you call the \includegraphics{} best regards yours s. Wolfgang Viechtbauer wrote:> Hello All, > > I am trying to put a math expression into a plot title and at the same > time, I want a value in the title to depend on a variable that I set > earlier. > > Simple Example: > > n <- 20 > plot(0, 0) > title(expression(paste(n[i], " = ", n))) > > Obviously, I want "n_i = 20". How can I get that? > > Thanks in advance, >-- Salvatore Barbaro University of Goettingen Department of Public Economics Platz der Goettinger Sieben 3 D-37073 Goettingen Phone: +49 551 3919704
Note that this will circumvent all the careful work in R to align the plotmath text correctly. In your case that includes having space for the subscript. It may not matter for main titles, but it certainly does for ylab or axis annotation or .... People might be producing PDF or Windows metafiles. I often produce PDF figures for inclusion in my PDF lecture slides. On Thu, 29 Jan 2004, Salvatore Barbaro wrote:> An alternative but also higly recommendable way is the use of the > psfrag-package in your LaTeX-file. It is furthermore very simple in use. > > Type > > plot(1:10, main="test") > > in your LaTeX-file you insert in your figure-environment the command > \psfrag{test}{$n_i=20$} > before you call the \includegraphics{} > > best regards > > yours > > s. > > > > > > > > > > > > > > Wolfgang Viechtbauer wrote: > > Hello All, > > > > I am trying to put a math expression into a plot title and at the same > > time, I want a value in the title to depend on a variable that I set > > earlier. > > > > Simple Example: > > > > n <- 20 > > plot(0, 0) > > title(expression(paste(n[i], " = ", n))) > > > > Obviously, I want "n_i = 20". How can I get that? > > > > Thanks in advance, > > > > >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595