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

Charles Annis, P.E. Charles.Annis at StatisticalEngineering.com
Mon Mar 6 03:42:58 CET 2006


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




More information about the R-help mailing list