[R] how to make plotmath expression work together with paste

Gabor Grothendieck ggrothendieck at gmail.com
Mon Mar 6 06:08:56 CET 2006


Try bquote (I may not have followed precisely what you
want but hopefully this gives the idea).  Note that you
may need to use a few judiciously placed ~ and
phantom(), as shown, in order to maintain syntax:

plot(NA, xlim = c(0, 100), ylim = c(0, 100))

avar1 <- 10
amath1 <- bquote(slope == frac(partialdiff * f(hat(theta)),
	partialdiff * hat(theta)) ~ phantom() == .(avar1))
text(10, 60, amath1)
text(20,30, bquote(a ~ word ~ .(avar1) ~ partialdiff ))
tetext(50,80, bquote(slope == frac(partialdiff *
                f(hat(theta)), partialdiff * hat(theta)) ~ .(avar1)))
n <- 12
text(12, 15, labels = bquote(bar(x) == sum(frac(x[i], .(n)), i==1) ~
	gamma == .(avar1)), cex = 0.8)



On 3/5/06, Paul Johnson <pauljohn32 at gmail.com> wrote:
> Recent questions about using plotmath have renewed my interest in this question
>
> I want to have expressions take values of variables from the
> environment. I am able to use expressions, and I am able to use paste
> to put text and values of variables into
> plots.  But the two things just won't work together.
>
> Here is some example code that shows what I mean.
>
>
> plot(NA,xlim=c(0,100),ylim=c(0,100))
>
> #show plot math works
> text(16, 22, expression(slope == frac(partialdiff * f(hat(theta)),
>     partialdiff * hat(theta))))
>
> # I want to put values of variables into the middle of expressions
>  avar1 <- 10
>
> # prove I am able to use paste!
> text(40,40, paste("a word",avar1,"other words")
>
> # But I'm completely frustrated by the problem of making paste and
> # expression work together.
>
> amath1 <- expression(slope == frac(partialdiff * f(hat(theta)),
> partialdiff * hat(theta)))
>
> # still works
> text(10,60,amath1)
>
>
> # paste breaks math output
> text(60,60, paste (amath1, "=" , avar1) )
>
>
> # Can't paste expression
> text(20,30, paste("a word",avar1, expression( partialdiff )))
>
>
>
> # paste does not work anymore--value of avar1 not replaced
> text(50,80, expression(paste("slope" == frac(partialdiff *
> f(hat(theta)),partialdiff * hat(theta)), avar1)))
>
> n<-12
>
> # This does get the avar1 value in the string and put it in, but it
> # piles the labels on top of each other. Wish they went side by side
>  text(12, 15, labels=c(expression(bar(x) == sum(frac(x[i], n), i==1,
> )), paste("gamma =",avar1)),cex = .8)
>
>
>
>
> --
> Paul E. Johnson
> Professor, Political Science
> 1541 Lilac Lane, Room 504
> University of Kansas
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list