[R] Properly Parsing Pre-Superscripts & Displaying Them With grid.text

Bryan Hanson hanson at depauw.edu
Sat Aug 2 21:54:46 CEST 2008


Thanks Gavin, that nicely solved one problem.  On a fresh look at the
archives, I see my other problem was trying to paste expressions, a bad
idea.  So, I'm writing each line separately.  All problems are fixed!

By the way, I discovered from the archives that to get a % in the final
output, you have to quote it in the expression: "%" which I suppose is a
general feature.  I may have missed it, but that behavior doesn't seem to be
mentioned in the plotmath help page - perhaps it's too obvious?

Thanks, Bryan 


On 8/2/08 2:19 PM, "Gavin Simpson" <gavin.simpson at ucl.ac.uk> wrote:

> On Fri, 2008-08-01 at 17:23 -0400, Bryan Hanson wrote:
>> Hi all... I¹m making a chart dealing with frequencies of isotopes of various
>> elements.  For instance, I'd like the following text to appear on a chart
>> with the "35" and "37" as superscripts:
>> 
>> Based upon:
>> 35Cl: 75%
>> 37Cl: 25%
>> 
>> I am having problems properly parsing the superscript that preceeds the
>> "Cl", since there is no character ahead of the superscript (I saw examples
>> in the archives where there was a preceeding character).  Also, the
>> construction of the string seems to not be working as I expect either.  So,
>> I think there are two problems here.  Here is a sample of what doesn't quite
>> work:
> 
> expression(phantom()^{35}*Cl[1])
> 
> works if I understand what you want. phantom() is documented
> on ?plotmath (?phantom is an alias for this help page also) and allows
> you to leave space as though argument was there, but I use it here with
> no object so no space left but this has the side effect of allowing the
> superscript for this "space".
> 
> Note that you need to wrap multiple character superscripts in {} ([] for
> subscripts). Also, you need to produce a valid expression so the *
> achieves this between the two components (the phantom()^{35} and the
> Cl[1] bits). You could also achieve the same result by pasting the bits
> together:
> 
> expression(paste(phantom()^{35}, Cl[1]))
> 
> but the former seems more familiar and intuitive to me now after
> grappling with plotmath for a while.
> 
> G
> 
>> 
>> Cl1 <- rbinom(1000, size = 1, prob = 0.25)
>> pCl1 <- histogram(Cl1, main = expression(Cl[1]), xlab = "", ylab = "",
>> scales = list(draw = FALSE), ylim = c(0:80))
>> plot(pCl1)
>> # This works fine but doesn't have everything I want:
>> leg.txt1 <- paste("Based upon:\n", ": 75%\n", ": 25%", sep = "")
>> grid.text(leg.txt1, 0.5, 0.5)
>> # This paste doesn't work due to the expression statements:
>> leg.txt2 <- paste("Based upon:\n", expression(^35*Cl), ": 75%\n",
>> expression(^37*Cl), ": 25%", sep = "")
>> # This doesnt' produce an error, but doesn't produce what is wanted either,
>> # as the expression is taken (almost) literally:
>> leg.txt3 <- paste("Based upon:\n", expression(""^35*Cl), ": 75%\n",
>> expression(""^37*Cl), ": 25%", sep = "")
>> grid.text(leg.txt3, 0.5, 0.3)
>> 
>>> From watching the help list, I know parsing things can be tricky.
>> 
>> TIA, Bryan
>> 
>>> sessionInfo()
>> R version 2.7.1 (2008-06-23)
>> i386-apple-darwin8.10.1
>> 
>> locale:
>> en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8
>> 
>> attached base packages:
>> [1] datasets  grid      grDevices graphics  stats     utils     methods
>> [8] base     
>> 
>> other attached packages:
>>  [1] fastICA_1.1-9         DescribeDisplay_0.1.3 ggplot_0.4.2
>>  [4] RColorBrewer_1.0-2    reshape_0.8.0         MASS_7.2-42
>>  [7] pcaPP_1.5             mvtnorm_0.9-0         hints_1.0.1-1
>> [10] mvoutlier_1.3         robustbase_0.2-8      lattice_0.17-8
>> [13] rggobi_2.1.9          RGtk2_2.12.5-3
>> 
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> 



More information about the R-help mailing list