[R] Transferring commas in character vector to expression

Bert Gunter gunter.berton at gene.com
Sun Jul 7 06:17:44 CEST 2013


I have not followed this thread, so I am only responding to your last
remark, and therefore the following may be useless to you (how's that
for for a caveat!!) ....  but maybe

plot(1:10,main=expression(delta*","*alpha))

might be helpful.

Alternatively:

x <- "delta"
y <- "alpha"
plot(1:10,   main = bquote(.(as.name(x))*","*.(as.name(y)))  )

produces the same result.

Again, if this is all irrelevant, just ignore.
If not, ?bquote and ?plotmath  contain all necessary explanation.

Cheers,
Bert


On Sat, Jul 6, 2013 at 6:57 PM, Eric Archer - NOAA Federal
<eric.archer at noaa.gov> wrote:
> Duncan,
>
> Thanks for the suggestion, but that won't work for my situation. I'm trying
> to use a character vector to label some axis ticks. There are some elements
> in the vector that have either a comma, or both Greek symbols and a comma,
> like the the third and fourth elements in x.lab below:
>
>> x <- 1:4
>> x.lab <- c("a*a", "bbb", "c,cc*c", "d,dd")
>> x.lab <- gsub("\\*", "*Delta*", x.lab)
>> x.lab <- parse(text = x.lab)
> Error in parse(text = x.lab) : <text>:3:2: unexpected ','
> 2: bbb
> 3: c,
>    ^
>> dotchart(x, labels = x.lab)
>
> The root problem that I'm stumped on is how to either:
> 1) insert a comma into an expression and have it be read as a valid
> character, or
> 2) replace the comma in the character string with 'list(a, b, c)' as in the
> help for plotmath and have it interpreted correctly.
>
> Cheers,
> eric
>
>
> On Sat, Jul 6, 2013 at 3:33 PM, Duncan Mackay <mackay at northnet.com.au>wrote:
>
>> Hi Eric
>>
>> I have not been following the thread but following on what David has said
>> on previous occasions
>>
>> try for example
>>
>> plot(1,1, ylab =  expression("aa aaa,aa bb"*Delta*"b cccc"*Delta*"cc, c") )
>>
>> Below is from a partly saved previous post of David's several months ago
>> which may give you some ideas
>>
>> DATA_names<-c(
>> "A mg kg",
>> "B mg kg",
>> "C mg kg",
>> "D mg kg",
>> "E mg kg",
>> "F mg kg",
>> "G mg kg",
>> "H mg kg")
>>
>> pos <- barplot(1:length(DATA_names))
>> text(x=pos,y=-1, xpd=TRUE, srt=45,
>>                    labels= sapply( gsub("mg kg", "(mg kg)^-1", DATA_names),
>>                                    as.expression))
>>
>> HTH
>>
>> Duncan
>>
>>
>> Duncan Mackay
>> Department of Agronomy and Soil Science
>> University of New England
>> Armidale NSW 2351
>> Email: home: mackay at northnet.com.au
>>
>>
>>
>>
>> At 07:47 6/07/2013, you wrote:
>>
>>> I'm trying to format a given character vector as an expression with Greek
>>> symbols to be used in labeling axis ticks. Thanks to some help from David
>>> Winsemius, I've learned how to make the substitution and place the Greek
>>> symbols in, however I've run into another problem: Some of my labels have
>>> commas in them, so when the parse command is executed, there is an
>>> unexpected symbol error. For example:
>>>
>>> > x <- c("aa", "aaa,aa", "bb*Delta*b", "cccc*Delta*cc,c")
>>> > parse(text = x)
>>> Error in parse(text = x) : <text>:2:4: unexpected ','
>>> 1: aa
>>> 2: aaa,
>>>      ^
>>>
>>> I've tried various iterations of wrapping the commas in interior quotes
>>> ("aaa\",\"aa"), but then the error shifts to the quote. I see in plotmath
>>> that 'list(a,b,c)' gives me comma separated values, but I haven't been
>>> able
>>> to work out how to get this construction for elements that have a comma.
>>>
>>> Is this possible?
>>>
>>> --
>>>
>>> Eric Archer, Ph.D.
>>> Southwest Fisheries Science Center
>>> NMFS, NOAA
>>> 8901 La Jolla Shores Drive
>>> La Jolla, CA 92037 USA
>>> 858-546-7121 (work)
>>> 858-546-7003 (FAX)
>>>
>>> Marine Mammal Genetics Group: swfsc.noaa.gov/prd-mmgenetics
>>> ETP Cetacean Assessment Program: swfsc.noaa.gov/prd-etp
>>>
>>> "The universe doesn't care what you believe.
>>>  The wonderful thing about science is that it
>>>    doesn't ask for your faith, it just asks
>>>    for your eyes."  - Randall Munroe
>>>
>>> "Lighthouses are more helpful than churches."
>>>    - Benjamin Franklin
>>>
>>>    "...but I'll take a GPS over either one."
>>>        - John C. "Craig" George
>>>
>>>         [[alternative HTML version deleted]]
>>>
>>> ______________________________**________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help>
>>> PLEASE do read the posting guide http://www.R-project.org/**
>>> posting-guide.html <http://www.R-project.org/posting-guide.html>
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>>
>>
>
>
> --
>
> Eric Archer, Ph.D.
> Southwest Fisheries Science Center
> NMFS, NOAA
> 8901 La Jolla Shores Drive
> La Jolla, CA 92037 USA
> 858-546-7121 (work)
> 858-546-7003 (FAX)
>
> Marine Mammal Genetics Group: swfsc.noaa.gov/prd-mmgenetics
> ETP Cetacean Assessment Program: swfsc.noaa.gov/prd-etp
>
> "The universe doesn't care what you believe.
>  The wonderful thing about science is that it
>    doesn't ask for your faith, it just asks
>    for your eyes."  - Randall Munroe
>
> "Lighthouses are more helpful than churches."
>    - Benjamin Franklin
>
>    "...but I'll take a GPS over either one."
>        - John C. "Craig" George
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm



More information about the R-help mailing list