Skip to content

Combining math and variables in expression

3 messages · socrates, Greg Snow, David Winsemius

#
I am trying to get the following line in a plot margin using mtext:

100% Area = 120.000 km^2

Where I intend that "100% Area =" is text, 120.000 is a number that varies
according to different data, and km^2 should be a neat
km-with-superscript-2. 
The expression function fails me, since it apparently cannot coerce an
expression when a variable is involved. 
In the R help file there is an example using either bquote or substitute to
accomplish like things, but these are sufficiently different from my
objective and I am becoming more and more confused by every further example
I stumble on. 
This should be easy. Can anyone help me prevent spending anoter 2.5 hours on
the matter? Thanks.
#
Hope this helps,
#
Dear socrates;

It did not take me 2.5 hours but it did take longer than I thought it  
would. I worked off the example given by Henrique Dallazuanna seen at:
http://finzi.psych.upenn.edu/R/Rhelp08/archive/120065.html

  This seems "to work":

mtext(bquote(100~"%" ~ Area == ~ .(text.val)~ Km^2), side=3)

I cannot say the the syntactic rules are entirely clear. The proper  
placement of the tildes is a bit mysterious to me. I am wondering if a  
solution using substitute(expression(.)) might be more clear but my  
efforts in that direction were not successful.

--

David
On Mar 11, 2009, at 10:08 AM, socrates wrote:

            
David Winsemius, MD
Heritage Laboratories
West Hartford, CT