Eric Archer - NOAA Federal
2013-Jul-05 21:47 UTC
[R] Transferring commas in character vector to expression
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]]
Duncan Mackay
2013-Jul-06 22:33 UTC
[R] Transferring commas in character vector to expression
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 >PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.
Eric Archer - NOAA Federal
2013-Jul-07 05:54 UTC
[R] Transferring commas in character vector to expression
Duncan, Thanks! That was the tip I needed. With that, I was able to get this to work perfectly: x.lab <- c("a*a", "bbb", "c,cc*c", "d,dd") x.lab <- gsub("\\*", "*Delta*", x.lab) x.lab <- gsub(",", "*symbol(\"\\\\54\")*", x.lab) dotchart(1:length(x.lab), labels = parse(text = x.lab)) On Sat, Jul 6, 2013 at 9:38 PM, Duncan Mackay <mackay@northnet.com.au>wrote:> Eric > > How does this look - (you might have to add a few symbol("\54") where > needed for me this give a comma on windows7 ver 3.1 > > xyplot(1:4 ~ 1:4, scales = list(x=list(at = 1:4, labels > c(expression(a*a),expression(bbb),expression(c*Delta*cc*c),expression(d*Delta*symbol("\54")*dd) > ) ) ) ) > > I mostly use lattice and have forgotten how to do labels in basic plot so > have used lattice. should be similar and you can modify to suit. > > It was trial and error in going up through the numbers from about 38 > > > Duncan > > At 11:57 7/07/2013, you 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@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@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@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. > > > > > > > -- > > 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 > >-- 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]]