Re: [R] Convert string for expression in plot

From: Richard Longland <longland_at_physics.unc.edu>
Date: Tue, 29 Jan 2008 15:33:15 -0500

Thanks for the help. This substitute command might be what I'm after, but, the whole string is being read in from a file. So I can't explicitly write the thing out as you did in your example, as with your example in R-news.

My rules are:
Convert any ",g" or "g," to ",gamma" or "gamma,"   (This comma search avoids converting the Mg to Mgamma!) Make ANY number a superscript.

Currently, this is what I do:

# Read in the reaction name, not very elegant I know ReacName <-
as.character(read.table("file.dat",skip=0,header=FALSE,nrows=1)$V1)
print(ReacName)

# convert and numbers and g

ReacName <- sub(",g",",gamma",ReacName)
ReacName <- sub("g,","gamma,",ReacName)
ReacName <- gsub("([1-9])","phantom()^\\1",ReacName)
print(ReacName)

This results in:
25Mg(p,g)
phantom()^2phantom()^5Mg(p,gamma)

Just to clarify quickly, The text will always be different. The Mg could be Al, S etc. The p could be different also. So the whole name has to be automated into the plot title.

I imagine it's possible to split this string into pieces... but there must be a better and easier way to do this.

Thanks again for the help,
Richard

On Tue, 2008-01-29 at 18:54 +0100, Uwe Ligges wrote:
>
> Richard Longland wrote:
> > Hi,
> >
> > Although I understand how to use expression and paste to add
> > superscripts and symbols to my graphs, I have a problem with adding
> > superscripts to an existing string.
> >
> > For example, I read in the following from a separate file:
> > 25Mg(p,g)
> >
> > I want to convert that to superscripts etc. (in LaTex format):
> > $^{25}$Mg(p,$\gamma$)
> >
> > This needs to then be put into a graph title.
> >
> > Any ideas? I know that I can just write it in using expression and
> > paste, but the title is read in from a separate file.
>
>
> If you explain what the rules are to use superscript or greek letters
> (why is the p not converted to pi but the g to gamma, etc?), we ca help,
> as an example if you read just the number from a file:
>
> #number <- scan(that_file)
> # say you read
> number <- 25
>
> plot(1:10, main=substitute(phantom()^number * Mg(p, gamma),
> list(number=number)))
>
> See ?plotmath and for automating things like this my article in R News
> might help:
> Ligges, U. (2002): R Help Desk: Automation of Mathematical Annotation in
> Plots. R News 2 (3), 32-34.
>
> Best,
> Uwe Ligges
>
>
>
>
> > Cheers for the help,
> > Richard longland
> >
> > ______________________________________________
> > 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.
>
>



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. Received on Tue 29 Jan 2008 - 20:36:25 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 29 Jan 2008 - 22:30:10 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive