Paul Johnson
2006-Mar-06 01:30 UTC
[R] how to make plotmath expression work together with paste
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
Charles Annis, P.E.
2006-Mar-06 02:42 UTC
[R] how to make plotmath expression work together with paste
Hi, Paul: By my lights "paste" and "expression" work counter-intuitively. But as has been said here often, you CAN do it in R. Figuring out how might be a challenge, but you CAN do it (whatever it is). Here is some code that does what I think you intended. I also added some phantom characters because I thought the spacing looked too cramped otherwise. (The phantoms are cool since you can adjust their width according to the character you *don't* print.) Anyway, here's the code. I hope you can see how the paste and expression are worked kind of inside out. ######################### dev.off() plot(NA,xlim=c(0,100),ylim=c(0,100)) amath1 <- expression(paste("slope = ", frac(partialdiff * phantom(.)*f(phantom(.)*hat(theta)*phantom(.)), partialdiff * phantom(.)* hat(theta))),sep="") text(10,70,amath1, adj=0) text(20, 50, expression(slope == frac(partialdiff * phantom(.)*f(phantom(.)*hat(theta)*phantom(.)), partialdiff * phantom(.)* hat(theta))), adj=0) avar1 <- 10 text(10,30,bquote(paste("slope = ", frac(partialdiff * phantom(.)*f(phantom(.)*hat(theta)*phantom(.)), partialdiff * phantom(.)* hat(theta)),sep="")==.(avar1)), adj=0) ######################### Note that these look much the same but how they're constructed is rather different. Also, have a look at ?substitute and ?bquote Best Wishes, Charles Annis, P.E. Charles.Annis at StatisticalEngineering.com phone: 561-352-9699 eFax: 614-455-3265 http://www.StatisticalEngineering.com -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Paul Johnson Sent: Sunday, March 05, 2006 8:31 PM To: r-help at stat.math.ethz.ch Subject: [R] how to make plotmath expression work together with paste 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
Gabor Grothendieck
2006-Mar-06 05:08 UTC
[R] how to make plotmath expression work together with paste
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 >
Uwe Ligges
2006-Mar-06 07:39 UTC
[R] how to make plotmath expression work together with paste
Paul Johnson 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.See my article "R Help Desk: Automation of Mathematical Annotation in Plots" in R News 2(3), 32-34. Uwe Ligges> 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